forked from servo/core-foundation-rs
/
array.rs
154 lines (129 loc) · 4.97 KB
/
array.rs
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
// 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.
use base::{
AbstractCFTypeRef,
CFAllocatorRef,
CFIndex,
CFTypeID,
CFTypeRef,
CFWrapper,
kCFAllocatorDefault,
};
use core::libc::c_void;
pub type CFArrayRetainCallBack = *u8;
pub type CFArrayReleaseCallBack = *u8;
pub type CFArrayCopyDescriptionCallBack = *u8;
pub type CFArrayEqualCallBack = *u8;
pub struct CFArrayCallBacks {
version: CFIndex,
retain: CFArrayRetainCallBack,
release: CFArrayReleaseCallBack,
copyDescription: CFArrayCopyDescriptionCallBack,
equal: CFArrayEqualCallBack,
}
struct __CFArray { private: () }
pub type CFArrayRef = *__CFArray;
impl AbstractCFTypeRef for CFArrayRef {
fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef }
fn type_id() -> CFTypeID { unsafe { CFArrayGetTypeID() } }
}
pub type CFArray<ElemRefType> = CFWrapper<CFArrayRef, ElemRefType, ()>;
pub impl<ElemRefType:AbstractCFTypeRef> CFArray<ElemRefType> {
fn new(elems: &[ElemRefType]) -> CFArray<ElemRefType> {
let array_ref: CFArrayRef;
let elems_refs = do vec::map(elems) |e: &ElemRefType| { e.as_type_ref() };
unsafe {
array_ref = CFArrayCreate(kCFAllocatorDefault,
cast::transmute::<*CFTypeRef, **c_void>(vec::raw::to_ptr(elems_refs)),
elems.len() as CFIndex,
ptr::to_unsafe_ptr(&kCFTypeArrayCallBacks));
}
CFWrapper::wrap_owned(array_ref)
}
pub fn each_ref<A>(&self, cb: &fn(ElemRefType) -> A) {
for uint::range(0, self.len()) |i| { cb(self[i]); }
}
pub fn eachi_ref<A>(&self, cb: &fn(uint, ElemRefType) -> A) {
for uint::range(0, self.len()) |i| { cb(i, self[i]); }
}
// Careful; the callback must wrap the reference properly.
// Generally, when array elements are Core Foundation objects (not
// always true), they need to be wrapped with CFWrapper::wrap_shared.
pub fn each<A>(&self, cb: &fn(&ElemRefType) -> A) {
for uint::range(0, self.len()) |i| { cb(&self[i]); }
}
// Careful; the callback must wrap the reference properly.
// Generally, when array elements are Core Foundation objects (not
// always true), they need to be wrapped with CFWrapper::wrap_shared.
pub fn eachi<A>(&self, cb: &fn(uint, &ElemRefType) -> A) {
for uint::range(0, self.len()) |i| { cb(i, &self[i]); }
}
pub fn len(&self) -> uint {
unsafe { return CFArrayGetCount(*self.borrow_ref()) as uint; }
}
}
impl<ElemRefType:AbstractCFTypeRef> Index<uint,ElemRefType> for CFArray<ElemRefType> {
// Careful; the caller must wrap any returned reference properly.
// Generally, when array elements are Core Foundation objects (not
// always true), they need to be wrapped with CFWrapper::wrap_shared.
fn index(&self, idx: &uint) -> ElemRefType {
assert!(*idx < self.len());
unsafe {
let elem = CFArrayGetValueAtIndex(*self.borrow_ref(), *idx as CFIndex);
// Don't return a wrapped thing, since we don't know whether
// it needs base::wrap_shared() or base::wrap_owned()
cast::transmute::<*c_void,ElemRefType>(elem)
}
}
}
#[link_args="-framework CoreFoundation"]
#[nolink]
extern {
/*
* CFArray.h
*/
static kCFTypeArrayCallBacks: CFArrayCallBacks;
fn CFArrayCreate(allocator: CFAllocatorRef, values: **c_void,
numValues: CFIndex, callBacks: *CFArrayCallBacks) -> CFArrayRef;
// CFArrayCreateCopy
// CFArrayBSearchValues
// CFArrayContainsValue
fn CFArrayGetCount(theArray: CFArrayRef) -> CFIndex;
// CFArrayGetCountOfValue
// CFArrayGetFirstIndexOfValue
// CFArrayGetLastIndexOfValue
// CFArrayGetValues
fn CFArrayGetValueAtIndex(theArray: CFArrayRef, idx: CFIndex) -> *c_void;
// CFArrayApplyFunction
fn CFArrayGetTypeID() -> CFTypeID;
}
#[test]
fn should_box_and_unbox() {
use number::{CFNumber, CFNumberRef};
let one = CFNumber::new(1 as i32);
let two = CFNumber::new(2 as i32);
let thr = CFNumber::new(3 as i32);
let fou = CFNumber::new(4 as i32);
let fiv = CFNumber::new(5 as i32);
let arr = CFArray::new([
*one.borrow_ref(),
*two.borrow_ref(),
*thr.borrow_ref(),
*fou.borrow_ref(),
*fiv.borrow_ref()]);
let mut sum = 0i32;
for arr.each |elem: &CFNumberRef| {
sum += CFWrapper::wrap_shared(*elem).to_i32();
}
assert!(sum == 15);
for arr.each |elem: &CFNumberRef| {
sum += CFWrapper::wrap_shared(*elem).to_i32();
}
assert!(sum == 30);
}