// Copyright 2013 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! Heterogeneous immutable arrays.
usecrate::ConcreteCFType; pubuse core_foundation_sys::array::*; pubuse core_foundation_sys::base::CFIndex; use core_foundation_sys::base::{kCFAllocatorDefault, CFRelease, CFTypeRef}; use std::marker::PhantomData; use std::mem; use std::os::raw::c_void; use std::ptr;
unsafeimpl ConcreteCFType for CFArray<*const c_void> {}
impl<T> CFArray<T> { /// Creates a new `CFArray` with the given elements, which must implement `Copy`. pubfn from_copyable(elems: &[T]) -> CFArray<T> where
T: Copy,
{ unsafe { let array_ref = CFArrayCreate(
kCFAllocatorDefault,
elems.as_ptr() as *const *const c_void,
elems.len().to_CFIndex(),
ptr::null(),
);
TCFType::wrap_under_create_rule(array_ref)
}
}
/// Creates a new `CFArray` with the given elements, which must be `CFType` objects. pubfn from_CFTypes(elems: &[T]) -> CFArray<T> where
T: TCFType,
{ unsafe { let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect(); let array_ref = CFArrayCreate(
kCFAllocatorDefault,
elems.as_ptr(),
elems.len().to_CFIndex(),
&kCFTypeArrayCallBacks,
);
TCFType::wrap_under_create_rule(array_ref)
}
}
/// Returns the same array, but with the type reset to void pointers. /// Equal to `to_untyped`, but is faster since it does not increment the retain count. #[inline] pubfn into_untyped(self) -> CFArray { let reference = self.0;
mem::forget(self); unsafe { CFArray::wrap_under_create_rule(reference) }
}
/// 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] pubfn iter(&self) -> CFArrayIterator<'_, T> {
CFArrayIterator {
array: self,
index: 0,
len: self.len(),
}
}
let n0 = CFNumber::from(0); let n1 = CFNumber::from(1); let n2 = CFNumber::from(2); let n3 = CFNumber::from(3); let n4 = CFNumber::from(4); let n5 = CFNumber::from(5);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.