diff --git a/core-foundation-sys/src/dictionary.rs b/core-foundation-sys/src/dictionary.rs index d9aa8d9..bf51bb1 100644 --- a/core-foundation-sys/src/dictionary.rs +++ b/core-foundation-sys/src/dictionary.rs @@ -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); + } diff --git a/core-foundation/src/dictionary.rs b/core-foundation/src/dictionary.rs index e27ed4a..9953c4b 100644 --- a/core-foundation/src/dictionary.rs +++ b/core-foundation/src/dictionary.rs @@ -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) + } } diff --git a/core-foundation/src/lib.rs b/core-foundation/src/lib.rs index 345b123..0a0e328 100644 --- a/core-foundation/src/lib.rs +++ b/core-foundation/src/lib.rs @@ -83,10 +83,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()]); } }