Auto merge of #154 - faern:iterate-untyped-array, r=jdm

Add possibility to iterate untyped CFArray

Maybe it was intentional, or maybe it was forgotten. But it is not possible to iterate an untyped array (`CFArray<*const c_void>`) in the `0.5.1` release, it was in `0.4`.

This extra `FromVoid` impl on `*const c_void` fixes that.

<!-- 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/154)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo
2018-01-27 09:37:41 -06:00
committed by GitHub
+21
View File
@@ -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;