From bef8cbb950977fdc2c2869f288b3175a3c85e094 Mon Sep 17 00:00:00 2001 From: Aleksey Kuznetsov Date: Sun, 1 Mar 2015 17:15:37 +0500 Subject: [PATCH] Use newtype structs. --- src/array.rs | 16 +++++----------- src/base.rs | 15 +++++---------- src/boolean.rs | 12 +++--------- src/bundle.rs | 12 +++--------- src/data.rs | 18 +++++++----------- src/dictionary.rs | 20 ++++++-------------- src/number.rs | 18 +++++++----------- src/runloop.rs | 35 +++++++++++++++-------------------- src/set.rs | 13 +++---------- src/string.rs | 20 +++++++------------- src/url.rs | 15 ++++++--------- 11 files changed, 67 insertions(+), 127 deletions(-) diff --git a/src/array.rs b/src/array.rs index 987a44c..73295c4 100644 --- a/src/array.rs +++ b/src/array.rs @@ -44,11 +44,7 @@ struct __CFArray; pub type CFArrayRef = *const __CFArray; /// A heterogeneous immutable array. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFArray { - obj: CFArrayRef, -} +pub struct CFArray(CFArrayRef); impl Drop for CFArray { fn drop(&mut self) { @@ -80,7 +76,7 @@ impl<'a> Iterator for CFArrayIterator<'a> { impl TCFType for CFArray { #[inline] fn as_concrete_TypeRef(&self) -> CFArrayRef { - self.obj + self.0 } #[inline] @@ -98,9 +94,7 @@ impl TCFType for CFArray { #[inline] unsafe fn wrap_under_create_rule(obj: CFArrayRef) -> CFArray { - CFArray { - obj: obj, - } + CFArray(obj) } #[inline] @@ -140,7 +134,7 @@ impl CFArray { #[inline] pub fn len(&self) -> CFIndex { unsafe { - CFArrayGetCount(self.obj) + CFArrayGetCount(self.as_concrete_TypeRef()) } } @@ -148,7 +142,7 @@ impl CFArray { pub fn get(&self, index: CFIndex) -> *const c_void { assert!(index < self.len()); unsafe { - CFArrayGetValueAtIndex(self.obj, index) + CFArrayGetValueAtIndex(self.as_concrete_TypeRef(), index) } } } diff --git a/src/base.rs b/src/base.rs index 81f3523..9f86ae4 100644 --- a/src/base.rs +++ b/src/base.rs @@ -35,7 +35,6 @@ impl CFIndexConvertible for usize { pub type CFOptionFlags = u32; -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFRange { @@ -72,15 +71,13 @@ struct __CFType; pub type CFTypeRef = *const __CFType; /// Superclass of all Core Foundation objects. -pub struct CFType { - obj: CFTypeRef, -} +pub struct CFType(CFTypeRef); impl Clone for CFType { #[inline] fn clone(&self) -> CFType { unsafe { - TCFType::wrap_under_get_rule(self.obj) + TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) } } } @@ -88,7 +85,7 @@ impl Clone for CFType { impl Drop for CFType { fn drop(&mut self) { unsafe { - CFRelease(self.obj) + CFRelease(self.as_CFTypeRef()) } } } @@ -156,7 +153,7 @@ pub trait TCFType { impl TCFType for CFType { #[inline] fn as_concrete_TypeRef(&self) -> CFTypeRef { - self.obj + self.0 } #[inline] @@ -172,9 +169,7 @@ impl TCFType for CFType { #[inline] unsafe fn wrap_under_create_rule(obj: CFTypeRef) -> CFType { - CFType { - obj: obj, - } + CFType(obj) } #[inline] diff --git a/src/boolean.rs b/src/boolean.rs index 7f29ae4..9fd052c 100644 --- a/src/boolean.rs +++ b/src/boolean.rs @@ -20,11 +20,7 @@ struct __CFBoolean; pub type CFBooleanRef = *const __CFBoolean; /// A Boolean type. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFBoolean { - obj: CFBooleanRef, -} +pub struct CFBoolean(CFBooleanRef); impl Drop for CFBoolean { fn drop(&mut self) { @@ -37,7 +33,7 @@ impl Drop for CFBoolean { impl TCFType for CFBoolean { #[inline] fn as_concrete_TypeRef(&self) -> CFBooleanRef { - self.obj + self.0 } #[inline] @@ -54,9 +50,7 @@ impl TCFType for CFBoolean { } unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean { - CFBoolean { - obj: obj, - } + CFBoolean(obj) } #[inline] diff --git a/src/bundle.rs b/src/bundle.rs index 42edf88..e6a7937 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -21,11 +21,7 @@ struct __CFBundle; pub type CFBundleRef = *const __CFBundle; /// A Bundle type. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFBundle { - obj: CFBundleRef, -} +pub struct CFBundle(CFBundleRef); impl Drop for CFBundle { fn drop(&mut self) { @@ -38,7 +34,7 @@ impl Drop for CFBundle { impl TCFType for CFBundle { #[inline] fn as_concrete_TypeRef(&self) -> CFBundleRef { - self.obj + self.0 } #[inline] @@ -55,9 +51,7 @@ impl TCFType for CFBundle { } unsafe fn wrap_under_create_rule(obj: CFBundleRef) -> CFBundle { - CFBundle { - obj: obj, - } + CFBundle(obj) } #[inline] diff --git a/src/data.rs b/src/data.rs index 55be5f1..ed411ee 100644 --- a/src/data.rs +++ b/src/data.rs @@ -20,11 +20,7 @@ struct __CFData; pub type CFDataRef = *const __CFData; /// A byte buffer. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFData { - obj: CFDataRef, -} +pub struct CFData(CFDataRef); impl Drop for CFData { fn drop(&mut self) { @@ -37,7 +33,7 @@ impl Drop for CFData { impl TCFType for CFData { #[inline] fn as_concrete_TypeRef(&self) -> CFDataRef { - self.obj + self.0 } #[inline] @@ -54,9 +50,7 @@ impl TCFType for CFData { } unsafe fn wrap_under_create_rule(obj: CFDataRef) -> CFData { - CFData { - obj: obj, - } + CFData(obj) } #[inline] @@ -82,7 +76,9 @@ impl CFData { #[inline] pub fn bytes<'a>(&'a self) -> &'a [u8] { unsafe { - mem::transmute((CFDataGetBytePtr(self.obj), self.len() as usize)) + mem::transmute( + (CFDataGetBytePtr(self.as_concrete_TypeRef()), self.len() as usize) + ) } } @@ -90,7 +86,7 @@ impl CFData { #[inline] pub fn len(&self) -> CFIndex { unsafe { - CFDataGetLength(self.obj) + CFDataGetLength(self.as_concrete_TypeRef()) } } } diff --git a/src/dictionary.rs b/src/dictionary.rs index 3817bfe..b32ce52 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -23,7 +23,6 @@ pub type CFDictionaryHashCallBack = *const u8; pub type CFDictionaryReleaseCallBack = *const u8; pub type CFDictionaryRetainCallBack = *const u8; -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFDictionaryKeyCallBacks { @@ -35,7 +34,6 @@ pub struct CFDictionaryKeyCallBacks { hash: CFDictionaryHashCallBack } -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFDictionaryValueCallBacks { @@ -52,11 +50,7 @@ struct __CFDictionary; pub type CFDictionaryRef = *const __CFDictionary; /// An immutable dictionary of key-value pairs. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that panics due to a Rust compiler bug. -pub struct CFDictionary { - obj: CFDictionaryRef, -} +pub struct CFDictionary(CFDictionaryRef); impl Drop for CFDictionary { fn drop(&mut self) { @@ -69,7 +63,7 @@ impl Drop for CFDictionary { impl TCFType for CFDictionary { #[inline] fn as_concrete_TypeRef(&self) -> CFDictionaryRef { - self.obj + self.0 } #[inline] @@ -86,9 +80,7 @@ impl TCFType for CFDictionary { } unsafe fn wrap_under_create_rule(obj: CFDictionaryRef) -> CFDictionary { - CFDictionary { - obj: obj, - } + CFDictionary(obj) } #[inline] @@ -120,7 +112,7 @@ impl CFDictionary { #[inline] pub fn len(&self) -> usize { unsafe { - CFDictionaryGetCount(self.obj) as usize + CFDictionaryGetCount(self.as_concrete_TypeRef()) as usize } } @@ -132,7 +124,7 @@ impl CFDictionary { #[inline] pub fn contains_key(&self, key: *const c_void) -> bool { unsafe { - CFDictionaryContainsKey(self.obj, key) != 0 + CFDictionaryContainsKey(self.as_concrete_TypeRef(), key) != 0 } } @@ -140,7 +132,7 @@ impl CFDictionary { pub fn find(&self, key: *const c_void) -> Option<*const c_void> { unsafe { let mut value: *const c_void = ptr::null(); - if CFDictionaryGetValueIfPresent(self.obj, key, &mut value) != 0 { + if CFDictionaryGetValueIfPresent(self.as_concrete_TypeRef(), key, &mut value) != 0 { Some(value) } else { None diff --git a/src/number.rs b/src/number.rs index 742b890..04d4bb4 100644 --- a/src/number.rs +++ b/src/number.rs @@ -44,11 +44,7 @@ struct __CFNumber; pub type CFNumberRef = *const __CFNumber; /// An immutable numeric value. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFNumber { - obj: CFNumberRef, -} +pub struct CFNumber(CFNumberRef); impl Drop for CFNumber { fn drop(&mut self) { @@ -61,7 +57,7 @@ impl Drop for CFNumber { impl TCFType for CFNumber { #[inline] fn as_concrete_TypeRef(&self) -> CFNumberRef { - self.obj + self.0 } #[inline] @@ -78,9 +74,7 @@ impl TCFType for CFNumber { } unsafe fn wrap_under_create_rule(obj: CFNumberRef) -> CFNumber { - CFNumber { - obj: obj, - } + CFNumber(obj) } #[inline] @@ -97,7 +91,8 @@ impl CFNumber { pub fn to_i64(&self) -> Option { unsafe { let mut value: i64 = 0; - let ok = CFNumberGetValue(self.obj, kCFNumberSInt64Type, mem::transmute(&mut value)); + let obj = self.as_concrete_TypeRef(); + let ok = CFNumberGetValue(obj, kCFNumberSInt64Type, mem::transmute(&mut value)); if ok { Some(value) } else { None } } } @@ -106,7 +101,8 @@ impl CFNumber { pub fn to_f64(&self) -> Option { unsafe { let mut value: f64 = 0.0; - let ok = CFNumberGetValue(self.obj, kCFNumberFloat64Type, mem::transmute(&mut value)); + let obj = self.as_concrete_TypeRef(); + let ok = CFNumberGetValue(obj, kCFNumberFloat64Type, mem::transmute(&mut value)); if ok { Some(value) } else { None } } } diff --git a/src/runloop.rs b/src/runloop.rs index 067df84..efa58a1 100644 --- a/src/runloop.rs +++ b/src/runloop.rs @@ -19,10 +19,8 @@ use date::{CFAbsoluteTime, CFTimeInterval}; use libc::c_void; use std::mem; -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFRunLoop { - obj: CFRunLoopRef, -} +/// An event processing loop. +pub struct CFRunLoop(CFRunLoopRef); impl Drop for CFRunLoop { fn drop(&mut self) { @@ -35,7 +33,7 @@ impl Drop for CFRunLoop { impl TCFType for CFRunLoop { #[inline] fn as_concrete_TypeRef(&self) -> CFRunLoopRef { - self.obj + self.0 } #[inline] @@ -53,9 +51,7 @@ impl TCFType for CFRunLoop { #[inline] unsafe fn wrap_under_create_rule(obj: CFRunLoopRef) -> CFRunLoop { - CFRunLoop { - obj: obj, - } + CFRunLoop(obj) } #[inline] @@ -89,13 +85,13 @@ impl CFRunLoop { pub fn stop(&self) { unsafe { - CFRunLoopStop(self.obj); + CFRunLoopStop(self.as_concrete_TypeRef()); } } pub fn current_mode(&self) -> Option { unsafe { - let string_ref = CFRunLoopCopyCurrentMode(self.obj); + let string_ref = CFRunLoopCopyCurrentMode(self.as_concrete_TypeRef()); if string_ref.is_null() { return None; } @@ -107,13 +103,15 @@ impl CFRunLoop { pub fn contains_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) -> bool { unsafe { - CFRunLoopContainsTimer(self.obj, timer.obj, mode) != 0 + let timer_obj = timer.as_concrete_TypeRef(); + CFRunLoopContainsTimer(self.as_concrete_TypeRef(), timer_obj, mode) != 0 } } pub fn add_timer(&self, timer: &CFRunLoopTimer, mode: CFStringRef) { unsafe { - CFRunLoopAddTimer(self.obj, timer.obj, mode); + let timer_obj = timer.as_concrete_TypeRef(); + CFRunLoopAddTimer(self.as_concrete_TypeRef(), timer_obj, mode); } } @@ -206,10 +204,8 @@ struct __CFRunLoopTimer; pub type CFRunLoopTimerRef = *const __CFRunLoopTimer; -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFRunLoopTimer { - obj: CFRunLoopTimerRef, -} +/// Represents a specialized run loop source that fires at a preset time in the future. +pub struct CFRunLoopTimer(CFRunLoopTimerRef); impl Drop for CFRunLoopTimer { fn drop(&mut self) { @@ -222,7 +218,8 @@ impl Drop for CFRunLoopTimer { impl TCFType for CFRunLoopTimer { #[inline] fn as_concrete_TypeRef(&self) -> CFRunLoopTimerRef { - self.obj + let CFRunLoopTimer(obj) = *self; + obj } #[inline] @@ -240,9 +237,7 @@ impl TCFType for CFRunLoopTimer { #[inline] unsafe fn wrap_under_create_rule(obj: CFRunLoopTimerRef) -> CFRunLoopTimer { - CFRunLoopTimer { - obj: obj, - } + CFRunLoopTimer(obj) } #[inline] diff --git a/src/set.rs b/src/set.rs index 4432e06..7918324 100644 --- a/src/set.rs +++ b/src/set.rs @@ -21,7 +21,6 @@ pub type CFSetCopyDescriptionCallBack = *const u8; pub type CFSetEqualCallBack = *const u8; pub type CFSetHashCallBack = *const u8; -#[allow(dead_code)] #[repr(C)] #[derive(Clone, Copy)] pub struct CFSetCallBacks { @@ -39,11 +38,7 @@ struct __CFSet; pub type CFSetRef = *const __CFSet; /// An immutable bag of elements. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFSet { - obj: CFSetRef, -} +pub struct CFSet(CFSetRef); impl Drop for CFSet { fn drop(&mut self) { @@ -56,7 +51,7 @@ impl Drop for CFSet { impl TCFType for CFSet { #[inline] fn as_concrete_TypeRef(&self) -> CFSetRef { - self.obj + self.0 } #[inline] @@ -73,9 +68,7 @@ impl TCFType for CFSet { } unsafe fn wrap_under_create_rule(obj: CFSetRef) -> CFSet { - CFSet { - obj: obj, - } + CFSet(obj) } #[inline] diff --git a/src/string.rs b/src/string.rs index 302c1a8..440a9cd 100644 --- a/src/string.rs +++ b/src/string.rs @@ -205,17 +205,13 @@ struct __CFString; pub type CFStringRef = *const __CFString; /// An immutable string in one of a variety of encodings. -/// -/// FIXME(pcwalton): Should be a newtype struct, but that fails due to a Rust compiler bug. -pub struct CFString { - obj: CFStringRef, -} +pub struct CFString(CFStringRef); impl Clone for CFString { #[inline] fn clone(&self) -> CFString { unsafe { - TCFType::wrap_under_get_rule(self.obj) + TCFType::wrap_under_get_rule(self.as_concrete_TypeRef()) } } } @@ -232,7 +228,7 @@ impl Drop for CFString { impl TCFType for CFString { #[inline] fn as_concrete_TypeRef(&self) -> CFStringRef { - self.obj + self.0 } #[inline] @@ -249,9 +245,7 @@ impl TCFType for CFString { } unsafe fn wrap_under_create_rule(obj: CFStringRef) -> CFString { - CFString { - obj: obj, - } + CFString(obj) } #[inline] @@ -287,7 +281,7 @@ impl ToString for CFString { // First, ask how big the buffer ought to be. let mut bytes_required: CFIndex = 0; - CFStringGetBytes(self.obj, + CFStringGetBytes(self.as_concrete_TypeRef(), CFRange::init(0, char_len), kCFStringEncodingUTF8, 0, @@ -301,7 +295,7 @@ impl ToString for CFString { for _ in (0..bytes_required) { buffer.push('\x00' as u8) } let mut bytes_used: CFIndex = 0; - let chars_written = CFStringGetBytes(self.obj, + let chars_written = CFStringGetBytes(self.as_concrete_TypeRef(), CFRange::init(0, char_len), kCFStringEncodingUTF8, 0, @@ -346,7 +340,7 @@ impl CFString { #[inline] pub fn char_len(&self) -> CFIndex { unsafe { - CFStringGetLength(self.obj) + CFStringGetLength(self.as_concrete_TypeRef()) } } } diff --git a/src/url.rs b/src/url.rs index fd95479..6542be4 100644 --- a/src/url.rs +++ b/src/url.rs @@ -24,9 +24,7 @@ struct __CFURL; pub type CFURLRef = *const __CFURL; -pub struct CFURL { - obj: CFURLRef, -} +pub struct CFURL(CFURLRef); impl Drop for CFURL { fn drop(&mut self) { @@ -39,7 +37,7 @@ impl Drop for CFURL { impl TCFType for CFURL { #[inline] fn as_concrete_TypeRef(&self) -> CFURLRef { - self.obj + self.0 } #[inline] @@ -56,9 +54,7 @@ impl TCFType for CFURL { } unsafe fn wrap_under_create_rule(obj: CFURLRef) -> CFURL { - CFURL { - obj: obj, - } + CFURL(obj) } #[inline] @@ -73,7 +69,8 @@ impl fmt::Debug for CFURL { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { unsafe { - let string: CFString = TCFType::wrap_under_get_rule(CFURLGetString(self.obj)); + let obj = self.as_concrete_TypeRef(); + let string: CFString = TCFType::wrap_under_get_rule(CFURLGetString(obj)); write!(f, "{}", string.to_string()) } } @@ -89,7 +86,7 @@ impl CFURL { pub fn get_string(&self) -> CFString { unsafe { - TCFType::wrap_under_get_rule(CFURLGetString(self.obj)) + TCFType::wrap_under_get_rule(CFURLGetString(self.as_concrete_TypeRef())) } } }