From dc9392c19ed9fae9773db6396d9bc2916073ea74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Linus=20F=C3=A4rnstrand?= Date: Sat, 27 Jan 2018 10:50:06 +0100 Subject: [PATCH] Add possibility to iterate untyped CFArray --- core-foundation/src/array.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/core-foundation/src/array.rs b/core-foundation/src/array.rs index 5f0a50d..962b2dd 100644 --- a/core-foundation/src/array.rs +++ b/core-foundation/src/array.rs @@ -55,6 +55,12 @@ unsafe impl FromVoid for u32 { } } +unsafe impl FromVoid for *const c_void { + unsafe fn from_void<'a>(x: *const c_void) -> ItemRef<'a, Self> { + ItemRef(ManuallyDrop::new(x), PhantomData) + } +} + unsafe impl FromVoid for T { unsafe fn from_void<'a>(x: *const c_void) -> ItemRef<'a, Self> { ItemRef(ManuallyDrop::new(TCFType::wrap_under_create_rule(T::Ref::from_void_ptr(x))), PhantomData) @@ -238,6 +244,21 @@ mod tests { assert_eq!(x.retain_count(), 1); } + #[test] + fn iter_untyped_array() { + use string::{CFString, CFStringRef}; + + let cf_string = CFString::from_static_string("bar"); + let array: CFArray = CFArray::from_CFTypes(&[cf_string.clone()]).into_untyped(); + + let cf_strings = array.iter().map(|ptr| { + unsafe { CFString::wrap_under_get_rule(CFStringRef::from_void_ptr(*ptr)) } + }).collect::>(); + let strings = cf_strings.iter().map(|s| s.to_string()).collect::>(); + assert_eq!(cf_string.retain_count(), 3); + assert_eq!(&strings[..], &["bar"]); + } + #[test] fn should_box_and_unbox() { use number::CFNumber;