Auto merge of #89 - cjhowe:feature-dictionary-keys-values, r=jdm
Add CFDictionary function to get keys and values Tests included. No formatting issues this time! <!-- 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/89) <!-- Reviewable:end -->
This commit is contained in:
@@ -72,4 +72,8 @@ extern {
|
||||
pub fn CFDictionarySetValue(theDict: CFMutableDictionaryRef,
|
||||
key: *const c_void,
|
||||
value: *const c_void);
|
||||
pub fn CFDictionaryGetKeysAndValues(theDict: CFDictionaryRef,
|
||||
keys: *mut *const c_void,
|
||||
values: *mut *const c_void);
|
||||
|
||||
}
|
||||
|
||||
@@ -101,4 +101,18 @@ impl CFDictionary {
|
||||
pub unsafe fn set_value(&self, key: *const c_void, value: *const c_void) {
|
||||
CFDictionarySetValue(self.0, key, value)
|
||||
}
|
||||
|
||||
pub fn get_keys_and_values(&self) -> (Vec<*const c_void>, Vec<*const c_void>) {
|
||||
let length = self.len();
|
||||
let mut keys = Vec::with_capacity(length);
|
||||
let mut values = Vec::with_capacity(length);
|
||||
|
||||
unsafe {
|
||||
CFDictionaryGetKeysAndValues(self.0, keys.as_mut_ptr(), values.as_mut_ptr());
|
||||
keys.set_len(length);
|
||||
values.set_len(length);
|
||||
}
|
||||
|
||||
(keys, values)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,10 +84,15 @@ pub mod test {
|
||||
let tru = CFBoolean::true_value();
|
||||
let n42 = number(42);
|
||||
|
||||
let _d = CFDictionary::from_CFType_pairs(&[
|
||||
let d = CFDictionary::from_CFType_pairs(&[
|
||||
(bar.as_CFType(), boo.as_CFType()),
|
||||
(baz.as_CFType(), tru.as_CFType()),
|
||||
(foo.as_CFType(), n42.as_CFType()),
|
||||
]);
|
||||
|
||||
let (v1, v2) = d.get_keys_and_values();
|
||||
|
||||
assert!(v1 == &[bar.as_CFTypeRef(), baz.as_CFTypeRef(), foo.as_CFTypeRef()]);
|
||||
assert!(v2 == &[boo.as_CFTypeRef(), tru.as_CFTypeRef(), n42.as_CFTypeRef()]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user