1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use base::{CFAllocatorRef, CFIndex, CFIndexConvertible, CFRelease, CFRetain};
use base::{CFType, CFTypeID, CFTypeRef, TCFType};
use base::{kCFAllocatorDefault};
use libc::c_void;
use std::mem;
pub type CFArrayRetainCallBack = *const u8;
pub type CFArrayReleaseCallBack = *const u8;
pub type CFArrayCopyDescriptionCallBack = *const u8;
pub type CFArrayEqualCallBack = *const u8;
#[allow(dead_code)]
#[repr(C)]
#[derive(Clone, Copy)]
pub struct CFArrayCallBacks {
version: CFIndex,
retain: CFArrayRetainCallBack,
release: CFArrayReleaseCallBack,
copyDescription: CFArrayCopyDescriptionCallBack,
equal: CFArrayEqualCallBack,
}
#[repr(C)]
struct __CFArray;
pub type CFArrayRef = *const __CFArray;
pub struct CFArray {
obj: CFArrayRef,
}
impl Drop for CFArray {
fn drop(&mut self) {
unsafe {
CFRelease(self.as_CFTypeRef())
}
}
}
pub struct CFArrayIterator<'a> {
array: &'a CFArray,
index: CFIndex,
}
impl<'a> Iterator for CFArrayIterator<'a> {
type Item = *const c_void;
fn next(&mut self) -> Option<*const c_void> {
if self.index >= self.array.len() {
None
} else {
let value = self.array.get(self.index);
self.index += 1;
Some(value)
}
}
}
impl TCFType<CFArrayRef> for CFArray {
#[inline]
fn as_concrete_TypeRef(&self) -> CFArrayRef {
self.obj
}
#[inline]
unsafe fn wrap_under_get_rule(reference: CFArrayRef) -> CFArray {
let reference: CFArrayRef = mem::transmute(CFRetain(mem::transmute(reference)));
TCFType::wrap_under_create_rule(reference)
}
#[inline]
fn as_CFTypeRef(&self) -> CFTypeRef {
unsafe {
mem::transmute(self.as_concrete_TypeRef())
}
}
#[inline]
unsafe fn wrap_under_create_rule(obj: CFArrayRef) -> CFArray {
CFArray {
obj: obj,
}
}
#[inline]
fn type_id() -> CFTypeID {
unsafe {
CFArrayGetTypeID()
}
}
}
impl CFArray {
pub fn from_CFTypes(elems: &[CFType]) -> CFArray {
unsafe {
let elems: Vec<CFTypeRef> = elems.iter().map(|elem| elem.as_CFTypeRef()).collect();
let array_ref = CFArrayCreate(kCFAllocatorDefault,
mem::transmute(elems.as_ptr()),
elems.len().to_CFIndex(),
&kCFTypeArrayCallBacks);
TCFType::wrap_under_create_rule(array_ref)
}
}
#[inline]
pub fn iter<'a>(&'a self) -> CFArrayIterator<'a> {
CFArrayIterator {
array: self,
index: 0
}
}
#[inline]
pub fn len(&self) -> CFIndex {
unsafe {
CFArrayGetCount(self.obj)
}
}
#[inline]
pub fn get(&self, index: CFIndex) -> *const c_void {
assert!(index < self.len());
unsafe {
CFArrayGetValueAtIndex(self.obj, index)
}
}
}
#[link(name = "CoreFoundation", kind = "framework")]
extern {
static kCFTypeArrayCallBacks: CFArrayCallBacks;
fn CFArrayCreate(allocator: CFAllocatorRef, values: *const *const c_void,
numValues: CFIndex, callBacks: *const CFArrayCallBacks) -> CFArrayRef;
fn CFArrayGetCount(theArray: CFArrayRef) -> CFIndex;
fn CFArrayGetValueAtIndex(theArray: CFArrayRef, idx: CFIndex) -> *const c_void;
fn CFArrayGetTypeID() -> CFTypeID;
}
#[test]
fn should_box_and_unbox() {
use number::{CFNumber, number};
let arr = CFArray::from_CFTypes(&[
number(1).as_CFType(),
number(2).as_CFType(),
number(3).as_CFType(),
number(4).as_CFType(),
number(5).as_CFType(),
]);
unsafe {
let mut sum = 0;
for elem in arr.iter() {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}
assert!(sum == 15);
for elem in arr.iter() {
let number: CFNumber = TCFType::wrap_under_get_rule(mem::transmute(elem));
sum += number.to_i64().unwrap()
}
assert!(sum == 30);
}
}