Auto merge of #187 - jrmuizel:dict, r=jdm

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.

<!-- 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/187)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo
2018-02-02 16:57:30 -05:00
committed by GitHub
3 changed files with 20 additions and 15 deletions
+15 -9
View File
@@ -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<K = *const c_void, V = *const c_void>(CFDictionaryRef, PhantomData<K>, PhantomData<V>);
declare_TCFType!{
/// An immutable dictionary of key-value pairs.
CFDictionary, CFDictionaryRef
impl<K, V> Drop for CFDictionary<K, V> {
fn drop(&mut self) {
unsafe { CFRelease(self.as_CFTypeRef()) }
}
}
impl_TCFType!(CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID);
impl_TCFType!(CFDictionary<K, V>, CFDictionaryRef, CFDictionaryGetTypeID);
impl_CFTypeDescription!(CFDictionary);
impl CFDictionary {
pub fn from_CFType_pairs<K: TCFType, V: TCFType>(pairs: &[(K, V)]) -> CFDictionary {
impl<K, V> CFDictionary<K, V> {
pub fn from_CFType_pairs(pairs: &[(K, V)]) -> CFDictionary<K, V> where K: TCFType, V: TCFType {
let (keys, values): (Vec<CFTypeRef>, Vec<CFTypeRef>) = 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<K: TCFType>(&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<K: TCFType>(&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())
}
+2 -2
View File
@@ -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<CGFont, ()> {
pub fn create_copy_from_variations(&self, vars: &CFDictionary<CFType, CFType>) -> Result<CGFont, ()> {
unsafe {
let font_ref = CGFontCreateCopyWithVariations(self.as_ptr(),
vars.as_concrete_TypeRef());
+3 -4
View File
@@ -114,7 +114,7 @@ impl StylisticClassAccessors for CTFontStylisticClass {
pub type CTFontAttributes = CFDictionary;
pub type CTFontTraits = CFDictionary;
pub type CTFontTraits = CFDictionary<CFType, CFType>;
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>());
CFNumber::wrap_under_get_rule(mem::transmute(cftype.as_CFTypeRef()))
cftype.downcast::<CFNumber>().unwrap()
}
}
@@ -269,7 +268,7 @@ impl CTFontDescriptor {
}
}
pub fn new_from_attributes(attributes: &CFDictionary) -> CTFontDescriptor {
pub fn new_from_attributes(attributes: &CFDictionary<CFType, CFType>) -> CTFontDescriptor {
unsafe {
let result: CTFontDescriptorRef =
CTFontDescriptorCreateWithAttributes(attributes.as_concrete_TypeRef());