1use core_foundation_sys::base::{kCFAllocatorDefault, CFRelease, CFTypeRef};
13pub use core_foundation_sys::set::*;
14
15use crate::base::{CFIndexConvertible, TCFType};
16
17use std::marker::PhantomData;
18use std::os::raw::c_void;
19
20pub struct CFSet<T = *const c_void>(CFSetRef, PhantomData<T>);
22
23impl<T> Drop for CFSet<T> {
24 fn drop(&mut self) {
25 unsafe { CFRelease(self.as_CFTypeRef()) }
26 }
27}
28
29impl_TCFType!(CFSet<T>, CFSetRef, CFSetGetTypeID);
30impl_CFTypeDescription!(CFSet);
31
32impl CFSet {
33 pub fn from_slice<T>(elems: &[T]) -> CFSet<T>
35 where
36 T: TCFType,
37 {
38 unsafe {
39 let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect();
40 let set_ref = CFSetCreate(
41 kCFAllocatorDefault,
42 elems.as_ptr(),
43 elems.len().to_CFIndex(),
44 &kCFTypeSetCallBacks,
45 );
46 TCFType::wrap_under_create_rule(set_ref)
47 }
48 }
49}
50
51impl<T> CFSet<T> {
52 pub fn len(&self) -> usize {
54 unsafe { CFSetGetCount(self.0) as usize }
55 }
56
57 pub fn is_empty(&self) -> bool {
59 self.len() == 0
60 }
61}