diff --git a/core-foundation-sys/src/base.rs b/core-foundation-sys/src/base.rs index 03f81ba..36ba705 100644 --- a/core-foundation-sys/src/base.rs +++ b/core-foundation-sys/src/base.rs @@ -7,6 +7,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use std::cmp::Ordering; use libc::{c_uint, c_long, c_ulong, c_void, c_int}; use string::CFStringRef; @@ -22,6 +23,24 @@ pub type CFOptionFlags = u32; pub type OSStatus = i32; pub type SInt32 = c_int; +#[repr(i64)] +#[derive(Clone, Copy)] +pub enum CFComparisonResult { + LessThan = -1, + EqualTo = 0, + GreaterThan = 1, +} + +impl Into for CFComparisonResult { + fn into(self) -> Ordering { + match self { + CFComparisonResult::LessThan => Ordering::Less, + CFComparisonResult::EqualTo => Ordering::Equal, + CFComparisonResult::GreaterThan => Ordering::Greater + } + } +} + #[repr(C)] #[derive(Clone, Copy)] pub struct CFRange { @@ -93,7 +112,6 @@ extern { //fn CFCopyDescription //fn CFCopyTypeIDDescription - //fn CFEqual //fn CFGetAllocator pub fn CFEqual(cf1: CFTypeRef, cf2: CFTypeRef) -> Boolean; pub fn CFGetRetainCount(cf: CFTypeRef) -> CFIndex; diff --git a/core-foundation-sys/src/number.rs b/core-foundation-sys/src/number.rs index cca4e7a..8ea0d28 100644 --- a/core-foundation-sys/src/number.rs +++ b/core-foundation-sys/src/number.rs @@ -9,7 +9,7 @@ use libc::c_void; -use base::{CFAllocatorRef, CFTypeID}; +use base::{CFAllocatorRef, CFTypeID, CFComparisonResult}; #[repr(C)] pub struct __CFBoolean(c_void); @@ -55,6 +55,6 @@ extern { -> CFNumberRef; //fn CFNumberGetByteSize pub fn CFNumberGetValue(number: CFNumberRef, theType: CFNumberType, valuePtr: *mut c_void) -> bool; - //fn CFNumberCompare + pub fn CFNumberCompare(date: CFNumberRef, other: CFNumberRef, context: *mut c_void) -> CFComparisonResult; pub fn CFNumberGetTypeID() -> CFTypeID; } diff --git a/core-foundation/src/base.rs b/core-foundation/src/base.rs index 5e55b8b..308761c 100644 --- a/core-foundation/src/base.rs +++ b/core-foundation/src/base.rs @@ -38,6 +38,15 @@ impl Clone for CFType { } } +impl PartialEq for CFType { + #[inline] + fn eq(&self, other: &CFType) -> bool { + unsafe { + CFEqual(self.as_CFTypeRef(), other.as_CFTypeRef()) != 0 + } + } +} + impl Drop for CFType { fn drop(&mut self) { unsafe { diff --git a/core-foundation/src/lib.rs b/core-foundation/src/lib.rs index f4ca71d..2c5b90d 100644 --- a/core-foundation/src/lib.rs +++ b/core-foundation/src/lib.rs @@ -45,6 +45,45 @@ macro_rules! impl_TCFType { } } } + + impl Clone for $ty { + #[inline] + fn clone(&self) -> $ty { + unsafe { + $ty::wrap_under_get_rule(self.0) + } + } + } + + impl PartialEq for $ty { + #[inline] + fn eq(&self, other: &$ty) -> bool { + self.as_CFType().eq(&other.as_CFType()) + } + } + + impl Eq for $ty { } + } +} + +#[macro_export] +macro_rules! impl_CFComparison { + ($ty:ident, $compare:ident) => { + impl PartialOrd for $ty { + #[inline] + fn partial_cmp(&self, other: &$ty) -> Option<::std::cmp::Ordering> { + unsafe { + Some($compare(self.as_concrete_TypeRef(), other.as_concrete_TypeRef(), ::std::ptr::null_mut()).into()) + } + } + } + + impl Ord for $ty { + #[inline] + fn cmp(&self, other: &$ty) -> ::std::cmp::Ordering { + self.partial_cmp(other).unwrap() + } + } } } diff --git a/core-foundation/src/number.rs b/core-foundation/src/number.rs index b16d90b..4c31e96 100644 --- a/core-foundation/src/number.rs +++ b/core-foundation/src/number.rs @@ -27,6 +27,7 @@ impl Drop for CFNumber { } impl_TCFType!(CFNumber, CFNumberRef, CFNumberGetTypeID); +impl_CFComparison!(CFNumber, CFNumberCompare); impl CFNumber { #[inline] diff --git a/core-foundation/src/string.rs b/core-foundation/src/string.rs index aa7f313..0415ecd 100644 --- a/core-foundation/src/string.rs +++ b/core-foundation/src/string.rs @@ -23,15 +23,6 @@ use std::ffi::CStr; /// An immutable string in one of a variety of encodings. pub struct CFString(CFStringRef); -impl Clone for CFString { - #[inline] - fn clone(&self) -> CFString { - unsafe { - TCFType::wrap_under_get_rule(self.0) - } - } -} - impl Drop for CFString { fn drop(&mut self) { unsafe {