From 470f20d404e484943c6fe620edc9e7827686be8d Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 17 Apr 2016 22:23:04 -0700 Subject: [PATCH 1/3] Add sys bindings for CFError --- core-foundation-sys/src/error.rs | 32 ++++++++++++++++++++++++++++++++ core-foundation-sys/src/lib.rs | 1 + 2 files changed, 33 insertions(+) create mode 100644 core-foundation-sys/src/error.rs diff --git a/core-foundation-sys/src/error.rs b/core-foundation-sys/src/error.rs new file mode 100644 index 0000000..981aa1e --- /dev/null +++ b/core-foundation-sys/src/error.rs @@ -0,0 +1,32 @@ +// Copyright 2016 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use libc::c_void; + +use base::{CFTypeID, CFIndex}; +use string::CFStringRef; + +#[repr(C)] +struct __CFError(c_void); + +pub type CFErrorRef = *mut __CFError; + +extern "C" { + pub fn CFErrorGetTypeID() -> CFTypeID; + + pub static kCFErrorDomainPOSIX: CFStringRef; + pub static kCFErrorDomainOSStatus: CFStringRef; + pub static kCFErrorDomainMach: CFStringRef; + pub static kCFErrorDomainCocoa: CFStringRef; + + pub fn CFErrorGetDomain(err: CFErrorRef) -> CFStringRef; + pub fn CFErrorGetCode(err: CFErrorRef) -> CFIndex; + + pub fn CFErrorCopyDescription(err: CFErrorRef) -> CFStringRef; +} diff --git a/core-foundation-sys/src/lib.rs b/core-foundation-sys/src/lib.rs index 1561b94..584d825 100644 --- a/core-foundation-sys/src/lib.rs +++ b/core-foundation-sys/src/lib.rs @@ -16,6 +16,7 @@ pub mod bundle; pub mod data; pub mod date; pub mod dictionary; +pub mod error; pub mod number; pub mod runloop; pub mod set; From 1b3d4982f632c0e2e629f1eba9773551a1527060 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 17 Apr 2016 22:24:49 -0700 Subject: [PATCH 2/3] Fix private type in public API warnings --- core-foundation-sys/src/array.rs | 2 +- core-foundation-sys/src/bundle.rs | 2 +- core-foundation-sys/src/data.rs | 2 +- core-foundation-sys/src/dictionary.rs | 2 +- core-foundation-sys/src/error.rs | 2 +- core-foundation-sys/src/number.rs | 4 ++-- core-foundation-sys/src/runloop.rs | 8 ++++---- core-foundation-sys/src/set.rs | 2 +- core-foundation-sys/src/string.rs | 2 +- core-foundation-sys/src/url.rs | 2 +- 10 files changed, 14 insertions(+), 14 deletions(-) diff --git a/core-foundation-sys/src/array.rs b/core-foundation-sys/src/array.rs index 910030d..987fab4 100644 --- a/core-foundation-sys/src/array.rs +++ b/core-foundation-sys/src/array.rs @@ -34,7 +34,7 @@ pub struct CFArrayCallBacks { } #[repr(C)] -struct __CFArray(c_void); +pub struct __CFArray(c_void); pub type CFArrayRef = *const __CFArray; diff --git a/core-foundation-sys/src/bundle.rs b/core-foundation-sys/src/bundle.rs index 0999a83..7dead25 100644 --- a/core-foundation-sys/src/bundle.rs +++ b/core-foundation-sys/src/bundle.rs @@ -13,7 +13,7 @@ use base::CFTypeID; use string::CFStringRef; #[repr(C)] -struct __CFBundle(c_void); +pub struct __CFBundle(c_void); pub type CFBundleRef = *const __CFBundle; diff --git a/core-foundation-sys/src/data.rs b/core-foundation-sys/src/data.rs index 0e333b5..6a42b2b 100644 --- a/core-foundation-sys/src/data.rs +++ b/core-foundation-sys/src/data.rs @@ -3,7 +3,7 @@ use libc::c_void; use base::{CFAllocatorRef, CFTypeID, CFIndex}; #[repr(C)] -struct __CFData(c_void); +pub struct __CFData(c_void); pub type CFDataRef = *const __CFData; diff --git a/core-foundation-sys/src/dictionary.rs b/core-foundation-sys/src/dictionary.rs index daf22c7..d7631b5 100644 --- a/core-foundation-sys/src/dictionary.rs +++ b/core-foundation-sys/src/dictionary.rs @@ -42,7 +42,7 @@ pub struct CFDictionaryValueCallBacks { } #[repr(C)] -struct __CFDictionary(c_void); +pub struct __CFDictionary(c_void); pub type CFDictionaryRef = *const __CFDictionary; diff --git a/core-foundation-sys/src/error.rs b/core-foundation-sys/src/error.rs index 981aa1e..68097da 100644 --- a/core-foundation-sys/src/error.rs +++ b/core-foundation-sys/src/error.rs @@ -13,7 +13,7 @@ use base::{CFTypeID, CFIndex}; use string::CFStringRef; #[repr(C)] -struct __CFError(c_void); +pub struct __CFError(c_void); pub type CFErrorRef = *mut __CFError; diff --git a/core-foundation-sys/src/number.rs b/core-foundation-sys/src/number.rs index 31b91a0..05758f3 100644 --- a/core-foundation-sys/src/number.rs +++ b/core-foundation-sys/src/number.rs @@ -12,7 +12,7 @@ use libc::c_void; use base::{CFAllocatorRef, CFTypeID}; #[repr(C)] -struct __CFBoolean(c_void); +pub struct __CFBoolean(c_void); pub type CFBooleanRef = *const __CFBoolean; @@ -38,7 +38,7 @@ pub static kCFNumberFloat64Type: CFNumberType = 6; // static kCFNumberMaxType: CFNumberType = 16; #[repr(C)] -struct __CFNumber; +pub struct __CFNumber; pub type CFNumberRef = *const __CFNumber; diff --git a/core-foundation-sys/src/runloop.rs b/core-foundation-sys/src/runloop.rs index 69d7579..86414a5 100644 --- a/core-foundation-sys/src/runloop.rs +++ b/core-foundation-sys/src/runloop.rs @@ -15,17 +15,17 @@ use date::{CFAbsoluteTime, CFTimeInterval}; use string::CFStringRef; #[repr(C)] -struct __CFRunLoop(c_void); +pub struct __CFRunLoop(c_void); pub type CFRunLoopRef = *const __CFRunLoop; #[repr(C)] -struct __CFRunLoopSource(c_void); +pub struct __CFRunLoopSource(c_void); pub type CFRunLoopSourceRef = *const __CFRunLoopSource; #[repr(C)] -struct __CFRunLoopObserver(c_void); +pub struct __CFRunLoopObserver(c_void); pub type CFRunLoopObserverRef = *const __CFRunLoopObserver; @@ -97,7 +97,7 @@ pub struct CFRunLoopTimerContext { pub type CFRunLoopTimerCallBack = extern "C" fn (timer: CFRunLoopTimerRef, info: *mut c_void); #[repr(C)] -struct __CFRunLoopTimer; +pub struct __CFRunLoopTimer; pub type CFRunLoopTimerRef = *const __CFRunLoopTimer; diff --git a/core-foundation-sys/src/set.rs b/core-foundation-sys/src/set.rs index e473da1..0e7e64c 100644 --- a/core-foundation-sys/src/set.rs +++ b/core-foundation-sys/src/set.rs @@ -29,7 +29,7 @@ pub struct CFSetCallBacks { } #[repr(C)] -struct __CFSet(c_void); +pub struct __CFSet(c_void); pub type CFSetRef = *const __CFSet; diff --git a/core-foundation-sys/src/string.rs b/core-foundation-sys/src/string.rs index 76655da..746368c 100644 --- a/core-foundation-sys/src/string.rs +++ b/core-foundation-sys/src/string.rs @@ -188,7 +188,7 @@ pub type CFStringEncodings = CFIndex; //static kCFStringEncodingShiftJIS_X0213_00: CFStringEncoding = 0x0628; /* Deprecated */ #[repr(C)] -struct __CFString(c_void); +pub struct __CFString(c_void); pub type CFStringRef = *const __CFString; diff --git a/core-foundation-sys/src/url.rs b/core-foundation-sys/src/url.rs index 53fc456..443f893 100644 --- a/core-foundation-sys/src/url.rs +++ b/core-foundation-sys/src/url.rs @@ -12,7 +12,7 @@ use base::{CFOptionFlags, CFIndex, CFAllocatorRef, Boolean, CFTypeID}; use string::CFStringRef; #[repr(C)] -struct __CFURL(c_void); +pub struct __CFURL(c_void); pub type CFURLRef = *const __CFURL; From 74be6d333b98632237254542051b274809dcff12 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sun, 17 Apr 2016 22:37:53 -0700 Subject: [PATCH 3/3] Add CFError --- core-foundation/src/error.rs | 77 ++++++++++++++++++++++++++++++++++++ core-foundation/src/lib.rs | 1 + 2 files changed, 78 insertions(+) create mode 100644 core-foundation/src/error.rs diff --git a/core-foundation/src/error.rs b/core-foundation/src/error.rs new file mode 100644 index 0000000..af9b26f --- /dev/null +++ b/core-foundation/src/error.rs @@ -0,0 +1,77 @@ +// Copyright 2016 The Servo Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Core Foundation errors. + +use core_foundation_sys::error::*; +use core_foundation_sys::base::CFRelease; +use std::error::Error; +use std::fmt; + +use base::{CFIndex, TCFType}; +use string::CFString; + +/// An error value. +pub struct CFError(CFErrorRef); + +impl Drop for CFError { + fn drop(&mut self) { + unsafe { + CFRelease(self.as_CFTypeRef()) + } + } +} + +impl_TCFType!(CFError, CFErrorRef, CFErrorGetTypeID); + +impl fmt::Debug for CFError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("CFError") + .field("domain", &self.domain()) + .field("code", &self.code()) + .field("description", &self.description()) + .finish() + } +} + +impl fmt::Display for CFError { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + write!(fmt, "{}", self.description()) + } +} + +impl Error for CFError { + fn description(&self) -> &str { + "a Core Foundation error" + } +} + +impl CFError { + /// Returns a string identifying the domain with which this error is + /// associated. + pub fn domain(&self) -> CFString { + unsafe { + let s = CFErrorGetDomain(self.0); + CFString::wrap_under_get_rule(s) + } + } + + /// Returns the code identifying this type of error. + pub fn code(&self) -> CFIndex { + unsafe { CFErrorGetCode(self.0) } + } + + /// Returns a human-presentable description of the error. + pub fn description(&self) -> CFString { + unsafe { + let s = CFErrorCopyDescription(self.0); + CFString::wrap_under_create_rule(s) + } + } +} diff --git a/core-foundation/src/lib.rs b/core-foundation/src/lib.rs index 4457f72..345b123 100644 --- a/core-foundation/src/lib.rs +++ b/core-foundation/src/lib.rs @@ -54,6 +54,7 @@ pub mod boolean; pub mod data; pub use core_foundation_sys::date; // back compat pub mod dictionary; +pub mod error; pub mod number; pub mod set; pub mod string;