From 5616935b563e403cdc7bf36aabf84fcdd90585b9 Mon Sep 17 00:00:00 2001 From: Jeff Muizelaar Date: Thu, 1 Feb 2018 22:21:56 -0500 Subject: [PATCH] Give CFDictionary type parameters for keys and values There's more to be done to clean up the interface now that we have type parameters but this is a good place to start. --- core-foundation/src/dictionary.rs | 24 +++++++++++++++--------- core-graphics/src/font.rs | 4 ++-- core-text/src/font_descriptor.rs | 7 +++---- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index 3a9a571..cf51788 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -11,23 +11,29 @@ pub use core_foundation_sys::dictionary::*; -use core_foundation_sys::base::{CFTypeRef, kCFAllocatorDefault}; +use core_foundation_sys::base::{CFTypeRef, CFRelease, kCFAllocatorDefault}; use std::mem; use std::os::raw::c_void; use std::ptr; +use std::marker::PhantomData; + use base::{CFType, CFIndexConvertible, TCFType, TCFTypeRef}; +// consume the type parameters with PhantomDatas +pub struct CFDictionary(CFDictionaryRef, PhantomData, PhantomData); -declare_TCFType!{ - /// An immutable dictionary of key-value pairs. - CFDictionary, CFDictionaryRef +impl Drop for CFDictionary { + fn drop(&mut self) { + unsafe { CFRelease(self.as_CFTypeRef()) } + } } -impl_TCFType!(CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID); + +impl_TCFType!(CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID); impl_CFTypeDescription!(CFDictionary); -impl CFDictionary { - pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFDictionary { +impl CFDictionary { + pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFDictionary where K: TCFType, V: TCFType { let (keys, values): (Vec, Vec) = pairs .iter() .map(|&(ref key, ref value)| (key.as_CFTypeRef(), value.as_CFTypeRef())) @@ -64,7 +70,7 @@ impl CFDictionary { /// Similar to `contains_key` but acts on a higher level, automatically converting from any /// `TCFType` to the raw pointer of its concrete TypeRef. #[inline] - pub fn contains_key2(&self, key: &K) -> bool { + pub fn contains_key2(&self, key: &K) -> bool where K: TCFType { self.contains_key(key.as_concrete_TypeRef().as_void_ptr()) } @@ -83,7 +89,7 @@ impl CFDictionary { /// Similar to `find` but acts on a higher level, automatically converting from any `TCFType` /// to the raw pointer of its concrete TypeRef. #[inline] - pub fn find2(&self, key: &K) -> Option<*const c_void> { + pub fn find2(&self, key: &K) -> Option<*const c_void> where K: TCFType { self.find(key.as_concrete_TypeRef().as_void_ptr()) } diff --git a/core-graphics/src/font.rs b/core-graphics/src/font.rs index 96e9f33..c2b8a10 100644 --- a/core-graphics/src/font.rs +++ b/core-graphics/src/font.rs @@ -8,7 +8,7 @@ // except according to those terms. use std::ptr; -use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType}; +use core_foundation::base::{CFRelease, CFRetain, CFTypeID, TCFType, CFType}; use core_foundation::array::{CFArray, CFArrayRef}; use core_foundation::data::{CFData, CFDataRef}; use core_foundation::string::{CFString, CFStringRef}; @@ -63,7 +63,7 @@ impl CGFont { } } - pub fn create_copy_from_variations(&self, vars: &CFDictionary) -> Result { + pub fn create_copy_from_variations(&self, vars: &CFDictionary) -> Result { unsafe { let font_ref = CGFontCreateCopyWithVariations(self.as_ptr(), vars.as_concrete_TypeRef()); diff --git a/core-text/src/font_descriptor.rs b/core-text/src/font_descriptor.rs index 9ae9680..9d643d4 100644 --- a/core-text/src/font_descriptor.rs +++ b/core-text/src/font_descriptor.rs @@ -114,7 +114,7 @@ impl StylisticClassAccessors for CTFontStylisticClass { pub type CTFontAttributes = CFDictionary; -pub type CTFontTraits = CFDictionary; +pub type CTFontTraits = CFDictionary; pub trait TraitAccessors { fn symbolic_traits(&self) -> CTFontSymbolicTraits; @@ -130,8 +130,7 @@ trait TraitAccessorPrivate { impl TraitAccessorPrivate for CTFontTraits { unsafe fn extract_number_for_key(&self, key: CFStringRef) -> CFNumber { let cftype = self.get_CFType(mem::transmute(key)); - assert!(cftype.instance_of::()); - CFNumber::wrap_under_get_rule(mem::transmute(cftype.as_CFTypeRef())) + cftype.downcast::().unwrap() } } @@ -269,7 +268,7 @@ impl CTFontDescriptor { } } -pub fn new_from_attributes(attributes: &CFDictionary) -> CTFontDescriptor { +pub fn new_from_attributes(attributes: &CFDictionary) -> CTFontDescriptor { unsafe { let result: CTFontDescriptorRef = CTFontDescriptorCreateWithAttributes(attributes.as_concrete_TypeRef());