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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
use std::cmp::Ordering;
use std::marker::PhantomData;
use std::ops::{Index, Range};

use objc::runtime::{Class, Object};
use objc_id::{Id, Owned, Ownership, Shared, ShareId};

use {INSCopying, INSFastEnumeration, INSMutableCopying, INSObject, NSEnumerator};

#[repr(isize)]
#[derive(Clone, Copy)]
pub enum NSComparisonResult {
    Ascending  = -1,
    Same       = 0,
    Descending = 1,
}

impl NSComparisonResult {
    pub fn from_ordering(order: Ordering) -> NSComparisonResult {
        match order {
            Ordering::Less => NSComparisonResult::Ascending,
            Ordering::Equal => NSComparisonResult::Same,
            Ordering::Greater => NSComparisonResult::Descending,
        }
    }

    pub fn as_ordering(&self) -> Ordering {
        match *self {
            NSComparisonResult::Ascending => Ordering::Less,
            NSComparisonResult::Same => Ordering::Equal,
            NSComparisonResult::Descending => Ordering::Greater,
        }
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct NSRange {
    pub location: usize,
    pub length: usize,
}

impl NSRange {
    pub fn from_range(range: Range<usize>) -> NSRange {
        assert!(range.end >= range.start);
        NSRange { location: range.start, length: range.end - range.start }
    }

    pub fn as_range(&self) -> Range<usize> {
        Range { start: self.location, end: self.location + self.length }
    }
}

unsafe fn from_refs<A>(refs: &[&A::Item]) -> Id<A> where A: INSArray {
    let cls = A::class();
    let obj: *mut A = msg_send![cls, alloc];
    let obj: *mut A = msg_send![obj, initWithObjects:refs.as_ptr()
                                               count:refs.len()];
    Id::from_retained_ptr(obj)
}

pub trait INSArray : INSObject {
    type Item: INSObject;
    type Own: Ownership;

    fn count(&self) -> usize {
        unsafe {
            msg_send![self, count]
        }
    }

    fn object_at(&self, index: usize) -> &Self::Item {
        unsafe {
            let obj: *const Self::Item = msg_send![self, objectAtIndex:index];
            &*obj
        }
    }

    fn first_object(&self) -> Option<&Self::Item> {
        unsafe {
            let obj: *const Self::Item = msg_send![self, firstObject];
            if obj.is_null() { None } else { Some(&*obj) }
        }
    }

    fn last_object(&self) -> Option<&Self::Item> {
        unsafe {
            let obj: *const Self::Item = msg_send![self, lastObject];
            if obj.is_null() { None } else { Some(&*obj) }
        }
    }

    fn object_enumerator(&self) -> NSEnumerator<Self::Item> {
        unsafe {
            let result: *mut Object = msg_send![self, objectEnumerator];
            NSEnumerator::from_ptr(result)
        }
    }

    fn from_vec(vec: Vec<Id<Self::Item, Self::Own>>) -> Id<Self> {
        let refs: Vec<&Self::Item> = vec.iter().map(|obj| &**obj).collect();
        unsafe {
            from_refs(&refs)
        }
    }

    fn objects_in_range(&self, range: Range<usize>) -> Vec<&Self::Item> {
        let range = NSRange::from_range(range);
        let mut vec = Vec::with_capacity(range.length);
        unsafe {
            let _: () = msg_send![self, getObjects:vec.as_ptr() range:range];
            vec.set_len(range.length);
        }
        vec
    }

    fn to_vec(&self) -> Vec<&Self::Item> {
        self.objects_in_range(0..self.count())
    }

    fn into_vec(array: Id<Self>) -> Vec<Id<Self::Item, Self::Own>> {
        array.to_vec().into_iter().map(|obj| unsafe {
            let obj_ptr: *const Self::Item = obj;
            Id::from_ptr(obj_ptr as *mut Self::Item)
        }).collect()
    }

    fn mut_object_at(&mut self, index: usize) -> &mut Self::Item
            where Self: INSArray<Own=Owned> {
        unsafe {
            let result: *mut Self::Item = msg_send![self, objectAtIndex:index];
            &mut *result
        }
    }

    fn shared_object_at(&self, index: usize) -> ShareId<Self::Item>
            where Self: INSArray<Own=Shared> {
        let obj = self.object_at(index);
        unsafe {
            Id::from_ptr(obj as *const _ as *mut Self::Item)
        }
    }

    fn from_slice(slice: &[ShareId<Self::Item>]) -> Id<Self>
            where Self: INSArray<Own=Shared> {
        let refs: Vec<&Self::Item> = slice.iter().map(|obj| &**obj).collect();
        unsafe {
            from_refs(&refs)
        }
    }

    fn to_shared_vec(&self) -> Vec<ShareId<Self::Item>>
            where Self: INSArray<Own=Shared> {
        self.to_vec().into_iter().map(|obj| unsafe {
            let obj_ptr: *const Self::Item = obj;
            Id::from_ptr(obj_ptr as *mut Self::Item)
        }).collect()
    }
}

pub struct NSArray<T, O = Owned> {
    item: PhantomData<Id<T, O>>,
}

object_impl!(NSArray<T, O>);

impl<T, O> INSObject for NSArray<T, O> where T: INSObject, O: Ownership {
    fn class() -> &'static Class {
        Class::get("NSArray").unwrap()
    }
}

impl<T, O> INSArray for NSArray<T, O> where T: INSObject, O: Ownership {
    type Item = T;
    type Own = O;
}

impl<T> INSCopying for NSArray<T, Shared> where T: INSObject {
    type Output = NSSharedArray<T>;
}

impl<T> INSMutableCopying for NSArray<T, Shared> where T: INSObject {
    type Output = NSMutableSharedArray<T>;
}

impl<T, O> INSFastEnumeration for NSArray<T, O>
        where T: INSObject, O: Ownership {
    type Item = T;
}

impl<T, O> Index<usize> for NSArray<T, O> where T: INSObject, O: Ownership {
    type Output = T;

    fn index(&self, index: usize) -> &T {
        self.object_at(index)
    }
}

pub type NSSharedArray<T> = NSArray<T, Shared>;

pub trait INSMutableArray : INSArray {
    fn add_object(&mut self, obj: Id<Self::Item, Self::Own>) {
        unsafe {
            let _: () = msg_send![self, addObject:&*obj];
        }
    }

    fn insert_object_at(&mut self, index: usize, obj: Id<Self::Item, Self::Own>) {
        unsafe {
            let _: () = msg_send![self, insertObject:&*obj atIndex:index];
        }
    }

    fn replace_object_at(&mut self, index: usize, obj: Id<Self::Item, Self::Own>) ->
            Id<Self::Item, Self::Own> {
        let old_obj = unsafe {
            let obj = self.object_at(index);
            Id::from_ptr(obj as *const _ as *mut Self::Item)
        };
        unsafe {
            let _: () = msg_send![self, replaceObjectAtIndex:index
                                                  withObject:&*obj];
        }
        old_obj
    }

    fn remove_object_at(&mut self, index: usize) -> Id<Self::Item, Self::Own> {
        let obj = unsafe {
            let obj = self.object_at(index);
            Id::from_ptr(obj as *const _ as *mut Self::Item)
        };
        unsafe {
            let _: () = msg_send![self, removeObjectAtIndex:index];
        }
        obj
    }

    fn remove_last_object(&mut self) -> Id<Self::Item, Self::Own> {
        let obj = self.last_object().map(|obj| unsafe {
            Id::from_ptr(obj as *const _ as *mut Self::Item)
        });
        unsafe {
            let _: () = msg_send![self, removeLastObject];
        }
        // removeLastObject would have failed if the array is empty,
        // so we know this won't be None
        obj.unwrap()
    }

    fn remove_all_objects(&mut self) {
        unsafe {
            let _: () = msg_send![self, removeAllObjects];
        }
    }

    fn sort_by<F>(&mut self, compare: F)
            where F: FnMut(&Self::Item, &Self::Item) -> Ordering {
        extern fn compare_with_closure<T, F>(obj1: &T, obj2: &T,
                compare: &mut F) -> NSComparisonResult
                where F: FnMut(&T, &T) -> Ordering {
            NSComparisonResult::from_ordering((*compare)(obj1, obj2))
        }

        let f: extern fn(&Self::Item, &Self::Item, &mut F) -> NSComparisonResult =
            compare_with_closure;
        let mut closure = compare;
        unsafe {
            let _: () = msg_send![self, sortUsingFunction:f
                                                  context:&mut closure];
        }
    }
}

pub struct NSMutableArray<T, O = Owned> {
    item: PhantomData<Id<T, O>>,
}

object_impl!(NSMutableArray<T, O>);

impl<T, O> INSObject for NSMutableArray<T, O> where T: INSObject, O: Ownership {
    fn class() -> &'static Class {
        Class::get("NSMutableArray").unwrap()
    }
}

impl<T, O> INSArray for NSMutableArray<T, O> where T: INSObject, O: Ownership {
    type Item = T;
    type Own = O;
}

impl<T, O> INSMutableArray for NSMutableArray<T, O>
        where T: INSObject, O: Ownership { }

impl<T> INSCopying for NSMutableArray<T, Shared> where T: INSObject {
    type Output = NSSharedArray<T>;
}

impl<T> INSMutableCopying for NSMutableArray<T, Shared> where T: INSObject {
    type Output = NSMutableSharedArray<T>;
}

impl<T, O> INSFastEnumeration for NSMutableArray<T, O>
        where T: INSObject, O: Ownership {
    type Item = T;
}

impl<T, O> Index<usize> for NSMutableArray<T, O>
        where T: INSObject, O: Ownership {
    type Output = T;

    fn index(&self, index: usize) -> &T {
        self.object_at(index)
    }
}

pub type NSMutableSharedArray<T> = NSMutableArray<T, Shared>;

#[cfg(test)]
mod tests {
    use objc_id::Id;
    use {INSObject, INSString, NSObject, NSString};
    use super::{INSArray, INSMutableArray, NSArray, NSMutableArray};

    fn sample_array(len: usize) -> Id<NSArray<NSObject>> {
        let mut vec = Vec::with_capacity(len);
        for _ in 0..len {
            vec.push(NSObject::new());
        }
        NSArray::from_vec(vec)
    }

    #[test]
    fn test_count() {
        let empty_array = NSArray::<NSObject>::new();
        assert!(empty_array.count() == 0);

        let array = sample_array(4);
        assert!(array.count() == 4);
    }

    #[test]
    fn test_object_at() {
        let array = sample_array(4);
        assert!(array.object_at(0) != array.object_at(3));
        assert!(array.first_object().unwrap() == array.object_at(0));
        assert!(array.last_object().unwrap() == array.object_at(3));

        let empty_array: Id<NSArray<NSObject>> = INSObject::new();
        assert!(empty_array.first_object().is_none());
        assert!(empty_array.last_object().is_none());
    }

    #[test]
    fn test_object_enumerator() {
        let array = sample_array(4);

        assert!(array.object_enumerator().count() == 4);
        assert!(array.object_enumerator()
                     .enumerate()
                     .all(|(i, obj)| obj == array.object_at(i)));
    }

    #[test]
    fn test_objects_in_range() {
        let array = sample_array(4);

        let middle_objs = array.objects_in_range(1..3);
        assert!(middle_objs.len() == 2);
        assert!(middle_objs[0] == array.object_at(1));
        assert!(middle_objs[1] == array.object_at(2));

        let empty_objs = array.objects_in_range(1..1);
        assert!(empty_objs.len() == 0);

        let all_objs = array.objects_in_range(0..4);
        assert!(all_objs.len() == 4);
    }

    #[test]
    fn test_into_vec() {
        let array = sample_array(4);

        let vec = INSArray::into_vec(array);
        assert!(vec.len() == 4);
    }

    #[test]
    fn test_add_object() {
        let mut array = NSMutableArray::new();
        let obj = NSObject::new();
        array.add_object(obj);

        assert!(array.count() == 1);
        assert!(array.object_at(0) == array.object_at(0));

        let obj = NSObject::new();
        array.insert_object_at(0, obj);
        assert!(array.count() == 2);
    }

    #[test]
    fn test_replace_object() {
        let mut array = NSMutableArray::new();
        let obj = NSObject::new();
        array.add_object(obj);

        let obj = NSObject::new();
        let old_obj = array.replace_object_at(0, obj);
        assert!(&*old_obj != array.object_at(0));
    }

    #[test]
    fn test_remove_object() {
        let mut array = NSMutableArray::new();
        for _ in 0..4 {
            array.add_object(NSObject::new());
        }

        array.remove_object_at(1);
        assert!(array.count() == 3);

        array.remove_last_object();
        assert!(array.count() == 2);

        array.remove_all_objects();
        assert!(array.count() == 0);
    }

    #[test]
    fn test_sort() {
        let strings = vec![
            NSString::from_str("hello"),
            NSString::from_str("hi"),
        ];
        let mut strings = NSMutableArray::from_vec(strings);

        strings.sort_by(|s1, s2| s1.as_str().len().cmp(&s2.as_str().len()));
        assert!(strings[0].as_str() == "hi");
        assert!(strings[1].as_str() == "hello");
    }
}