Auto merge of #123 - ratake:clone-ord-eq, r=jdm
Implement Clone + Eq for all types, Ord for CFNumber <!-- Reviewable:start --> This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/123) <!-- Reviewable:end -->
This commit is contained in:
@@ -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<Ordering> 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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ impl Drop for CFNumber {
|
||||
}
|
||||
|
||||
impl_TCFType!(CFNumber, CFNumberRef, CFNumberGetTypeID);
|
||||
impl_CFComparison!(CFNumber, CFNumberCompare);
|
||||
|
||||
impl CFNumber {
|
||||
#[inline]
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user