Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to iterate untyped CFArray #154

Merged
merged 1 commit into from Jan 27, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Add possibility to iterate untyped CFArray

  • Loading branch information
faern committed Jan 27, 2018
commit dc9392c19ed9fae9773db6396d9bc2916073ea74
@@ -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<T: TCFType> 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::<Vec<_>>();
let strings = cf_strings.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert_eq!(cf_string.retain_count(), 3);
assert_eq!(&strings[..], &["bar"]);
}

#[test]
fn should_box_and_unbox() {
use number::CFNumber;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.