Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernization and cleanup #69

Merged
merged 8 commits into from Sep 5, 2015
Merged

Use newtype structs

  • Loading branch information
sfackler committed Aug 30, 2015
commit d816f395d649fe16b4280eb42306b3c2cb99bb8c
@@ -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<CFArrayRef> for CFArray {
#[inline]
fn as_concrete_TypeRef(&self) -> CFArrayRef {
self.obj
self.0
}

#[inline]
@@ -98,9 +94,7 @@ impl TCFType<CFArrayRef> for CFArray {

#[inline]
unsafe fn wrap_under_create_rule(obj: CFArrayRef) -> CFArray {
CFArray {
obj: obj,
}
CFArray(obj)
}

#[inline]
@@ -140,15 +134,15 @@ impl CFArray {
#[inline]
pub fn len(&self) -> CFIndex {
unsafe {
CFArrayGetCount(self.obj)
CFArrayGetCount(self.0)
}
}

#[inline]
pub fn get(&self, index: CFIndex) -> *const c_void {
assert!(index < self.len());
unsafe {
CFArrayGetValueAtIndex(self.obj, index)
CFArrayGetValueAtIndex(self.0, index)
}
}
}
@@ -72,23 +72,21 @@ 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.0)
}
}
}

impl Drop for CFType {
fn drop(&mut self) {
unsafe {
CFRelease(self.obj)
CFRelease(self.0)
}
}
}
@@ -156,7 +154,7 @@ pub trait TCFType<ConcreteTypeRef> {
impl TCFType<CFTypeRef> for CFType {
#[inline]
fn as_concrete_TypeRef(&self) -> CFTypeRef {
self.obj
self.0
}

#[inline]
@@ -172,9 +170,7 @@ impl TCFType<CFTypeRef> for CFType {

#[inline]
unsafe fn wrap_under_create_rule(obj: CFTypeRef) -> CFType {
CFType {
obj: obj,
}
CFType(obj)
}

#[inline]
@@ -22,9 +22,7 @@ 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 +35,7 @@ impl Drop for CFBoolean {
impl TCFType<CFBooleanRef> for CFBoolean {
#[inline]
fn as_concrete_TypeRef(&self) -> CFBooleanRef {
self.obj
self.0
}

#[inline]
@@ -54,9 +52,7 @@ impl TCFType<CFBooleanRef> for CFBoolean {
}

unsafe fn wrap_under_create_rule(obj: CFBooleanRef) -> CFBoolean {
CFBoolean {
obj: obj,
}
CFBoolean(obj)
}

#[inline]
@@ -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<CFBundleRef> for CFBundle {
#[inline]
fn as_concrete_TypeRef(&self) -> CFBundleRef {
self.obj
self.0
}

#[inline]
@@ -55,9 +51,7 @@ impl TCFType<CFBundleRef> for CFBundle {
}

unsafe fn wrap_under_create_rule(obj: CFBundleRef) -> CFBundle {
CFBundle {
obj: obj,
}
CFBundle(obj)
}

#[inline]
@@ -22,11 +22,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) {
@@ -39,7 +35,7 @@ impl Drop for CFData {
impl TCFType<CFDataRef> for CFData {
#[inline]
fn as_concrete_TypeRef(&self) -> CFDataRef {
self.obj
self.0
}

#[inline]
@@ -56,9 +52,7 @@ impl TCFType<CFDataRef> for CFData {
}

unsafe fn wrap_under_create_rule(obj: CFDataRef) -> CFData {
CFData {
obj: obj,
}
CFData(obj)
}

#[inline]
@@ -84,15 +78,15 @@ impl CFData {
#[inline]
pub fn bytes<'a>(&'a self) -> &'a [u8] {
unsafe {
slice::from_raw_parts(CFDataGetBytePtr(self.obj), self.len() as usize)
slice::from_raw_parts(CFDataGetBytePtr(self.0), self.len() as usize)
}
}

/// Returns the length of this byte buffer.
#[inline]
pub fn len(&self) -> CFIndex {
unsafe {
CFDataGetLength(self.obj)
CFDataGetLength(self.0)
}
}
}
@@ -52,11 +52,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 +65,7 @@ impl Drop for CFDictionary {
impl TCFType<CFDictionaryRef> for CFDictionary {
#[inline]
fn as_concrete_TypeRef(&self) -> CFDictionaryRef {
self.obj
self.0
}

#[inline]
@@ -86,9 +82,7 @@ impl TCFType<CFDictionaryRef> for CFDictionary {
}

unsafe fn wrap_under_create_rule(obj: CFDictionaryRef) -> CFDictionary {
CFDictionary {
obj: obj,
}
CFDictionary(obj)
}

#[inline]
@@ -121,7 +115,7 @@ impl CFDictionary {
#[inline]
pub fn len(&self) -> usize {
unsafe {
CFDictionaryGetCount(self.obj) as usize
CFDictionaryGetCount(self.0) as usize
}
}

@@ -133,15 +127,15 @@ impl CFDictionary {
#[inline]
pub fn contains_key(&self, key: *const c_void) -> bool {
unsafe {
CFDictionaryContainsKey(self.obj, key) != 0
CFDictionaryContainsKey(self.0, key) != 0
}
}

#[inline]
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.0, key, &mut value) != 0 {
Some(value)
} else {
None
@@ -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<CFNumberRef> for CFNumber {
#[inline]
fn as_concrete_TypeRef(&self) -> CFNumberRef {
self.obj
self.0
}

#[inline]
@@ -78,9 +74,7 @@ impl TCFType<CFNumberRef> for CFNumber {
}

unsafe fn wrap_under_create_rule(obj: CFNumberRef) -> CFNumber {
CFNumber {
obj: obj,
}
CFNumber(obj)
}

#[inline]
@@ -107,7 +101,7 @@ impl CFNumber {
pub fn to_i64(&self) -> Option<i64> {
unsafe {
let mut value: i64 = 0;
let ok = CFNumberGetValue(self.obj, kCFNumberSInt64Type, mem::transmute(&mut value));
let ok = CFNumberGetValue(self.0, kCFNumberSInt64Type, mem::transmute(&mut value));
if ok { Some(value) } else { None }
}
}
@@ -116,7 +110,7 @@ impl CFNumber {
pub fn to_f64(&self) -> Option<f64> {
unsafe {
let mut value: f64 = 0.0;
let ok = CFNumberGetValue(self.obj, kCFNumberFloat64Type, mem::transmute(&mut value));
let ok = CFNumberGetValue(self.0, kCFNumberFloat64Type, mem::transmute(&mut value));
if ok { Some(value) } else { None }
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.