-
Notifications
You must be signed in to change notification settings - Fork 235
Add a type parameter to CFArray #127
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,31 +12,56 @@ | |
pub use core_foundation_sys::array::*; | ||
pub use core_foundation_sys::base::{CFIndex, CFRelease}; | ||
use core_foundation_sys::base::{CFTypeRef, kCFAllocatorDefault}; | ||
use base::CFType; | ||
use libc::c_void; | ||
use std::mem; | ||
use std::marker::PhantomData; | ||
|
||
use base::{CFIndexConvertible, TCFType, CFRange}; | ||
|
||
/// A heterogeneous immutable array. | ||
pub struct CFArray(CFArrayRef); | ||
pub struct CFArray<T = *const c_void>(CFArrayRef, PhantomData<T>); | ||
|
||
impl Drop for CFArray { | ||
/// A trait describing how to convert from the stored *const c_void to the desired T | ||
pub trait FromVoid { | ||
fn from_void(x: *const c_void) -> Self; | ||
} | ||
|
||
impl FromVoid for u32 { | ||
fn from_void(x: *const c_void) -> u32 { | ||
x as usize as u32 | ||
} | ||
} | ||
|
||
impl FromVoid for *const c_void { | ||
fn from_void(x: *const c_void) -> *const c_void { | ||
x | ||
} | ||
} | ||
|
||
impl FromVoid for CFType { | ||
fn from_void(x: *const c_void) -> CFType { | ||
unsafe { TCFType::wrap_under_get_rule(mem::transmute(x)) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unsound, one can pass |
||
} | ||
} | ||
|
||
impl<T> Drop for CFArray<T> { | ||
fn drop(&mut self) { | ||
unsafe { | ||
CFRelease(self.as_CFTypeRef()) | ||
} | ||
} | ||
} | ||
|
||
pub struct CFArrayIterator<'a> { | ||
array: &'a CFArray, | ||
pub struct CFArrayIterator<'a, T: 'a> { | ||
array: &'a CFArray<T>, | ||
index: CFIndex, | ||
} | ||
|
||
impl<'a> Iterator for CFArrayIterator<'a> { | ||
type Item = *const c_void; | ||
impl<'a, T: FromVoid> Iterator for CFArrayIterator<'a, T> { | ||
type Item = T; | ||
|
||
fn next(&mut self) -> Option<*const c_void> { | ||
fn next(&mut self) -> Option<T> { | ||
if self.index >= self.array.len() { | ||
None | ||
} else { | ||
|
@@ -47,18 +72,18 @@ impl<'a> Iterator for CFArrayIterator<'a> { | |
} | ||
} | ||
|
||
impl<'a> ExactSizeIterator for CFArrayIterator<'a> { | ||
impl<'a, T: FromVoid> ExactSizeIterator for CFArrayIterator<'a, T> { | ||
fn len(&self) -> usize { | ||
(self.array.len() - self.index) as usize | ||
} | ||
} | ||
|
||
impl_TCFType!(CFArray, CFArrayRef, CFArrayGetTypeID); | ||
impl_CFTypeDescription!(CFArray); | ||
impl_TCFTypeGeneric!(CFArray, CFArrayRef, CFArrayGetTypeID); | ||
impl_CFTypeDescriptionGeneric!(CFArray); | ||
|
||
impl CFArray { | ||
impl<T> CFArray<T> { | ||
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects. | ||
pub fn from_CFTypes<R, T>(elems: &[T]) -> CFArray where T: TCFType<R> { | ||
pub fn from_CFTypes<R>(elems: &[T]) -> CFArray<T> where T: TCFType<R> { | ||
unsafe { | ||
let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect(); | ||
let array_ref = CFArrayCreate(kCFAllocatorDefault, | ||
|
@@ -69,13 +94,17 @@ impl CFArray { | |
} | ||
} | ||
|
||
pub fn to_untyped(self) -> CFArray { | ||
CFArray(self.0, PhantomData) | ||
} | ||
|
||
/// Iterates over the elements of this `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()`. | ||
#[inline] | ||
pub fn iter<'a>(&'a self) -> CFArrayIterator<'a> { | ||
pub fn iter<'a>(&'a self) -> CFArrayIterator<'a, T> { | ||
CFArrayIterator { | ||
array: self, | ||
index: 0 | ||
|
@@ -90,11 +119,9 @@ impl CFArray { | |
} | ||
|
||
#[inline] | ||
pub fn get(&self, index: CFIndex) -> *const c_void { | ||
pub fn get(&self, index: CFIndex) -> T where T: FromVoid { | ||
assert!(index < self.len()); | ||
unsafe { | ||
CFArrayGetValueAtIndex(self.0, index) | ||
} | ||
T::from_void(unsafe { CFArrayGetValueAtIndex(self.0, index) }) | ||
} | ||
|
||
pub fn get_values(&self, range: CFRange) -> Vec<*const c_void> { | ||
|
@@ -114,11 +141,11 @@ impl CFArray { | |
} | ||
} | ||
|
||
impl<'a> IntoIterator for &'a CFArray { | ||
type Item = *const c_void; | ||
type IntoIter = CFArrayIterator<'a>; | ||
impl<'a, T: FromVoid> IntoIterator for &'a CFArray<T> { | ||
type Item = T; | ||
type IntoIter = CFArrayIterator<'a, T>; | ||
|
||
fn into_iter(self) -> CFArrayIterator<'a> { | ||
fn into_iter(self) -> CFArrayIterator<'a, T> { | ||
self.iter() | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be unsafe.