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

Modernization and cleanup #69

Merged
merged 8 commits into from Sep 5, 2015
Merged
Next

Modernize CFArray

* Implement IntoIterator for &CFArray
* Abstract from_CFTypes over TCFTypes
  • Loading branch information
sfackler committed Aug 29, 2015
commit 0d216f2e337dfecb63e730e3f654d9d833320687
@@ -10,7 +10,7 @@
//! Heterogeneous immutable arrays.

use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease, CFRetain};
use base::{CFType, CFTypeID, CFTypeRef, TCFType};
use base::{CFTypeID, CFTypeRef, TCFType};
use base::{kCFAllocatorDefault};
use libc::c_void;
use std::mem;
@@ -113,7 +113,7 @@ impl TCFType<CFArrayRef> for CFArray {

impl CFArray {
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects.
pub fn from_CFTypes(elems: &[CFType]) -> CFArray {
pub fn from_CFTypes<R, T: TCFType<R>>(elems: &[T]) -> CFArray {
unsafe {
let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect();
let array_ref = CFArrayCreate(kCFAllocatorDefault,
@@ -128,7 +128,7 @@ impl CFArray {
///
/// Careful; the loop body must wrap the reference properly. Generally, when array elements are
/// Core Foundation objects (not always true), they need to be wrapped with
/// `TCFType::wrap_under_get_rule()`. The safer `iter_CFTypes` method will do this for you.
/// `TCFType::wrap_under_get_rule()`.
#[inline]
pub fn iter<'a>(&'a self) -> CFArrayIterator<'a> {
CFArrayIterator {
@@ -153,6 +153,15 @@ impl CFArray {
}
}

impl<'a> IntoIterator for &'a CFArray {
type Item = *const c_void;
type IntoIter = CFArrayIterator<'a>;

fn into_iter(self) -> CFArrayIterator<'a> {
self.iter()
}
}

#[link(name = "CoreFoundation", kind = "framework")]
extern {
/*
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.