@@ -5,9 +5,9 @@ use core::marker::PhantomData;
5
5
use core:: ops:: { Index , Range } ;
6
6
use core:: ptr:: NonNull ;
7
7
8
+ use objc2:: msg_send;
8
9
use objc2:: rc:: { Id , Owned , Ownership , Shared , SliceId } ;
9
- use objc2:: runtime:: { Class , Object } ;
10
- use objc2:: { class, msg_send} ;
10
+ use objc2:: runtime:: Object ;
11
11
12
12
use super :: {
13
13
INSCopying , INSFastEnumeration , INSMutableCopying , INSObject , NSComparisonResult , NSEnumerator ,
@@ -158,27 +158,21 @@ pub unsafe trait INSArray: INSObject {
158
158
}
159
159
}
160
160
161
- /// TODO
162
- ///
163
- /// You can have a `Id<NSArray<T, Owned>, Owned>`, which allows mutable access
164
- /// to the elements (without modifying the array itself), and
165
- /// `Id<NSArray<T, Shared>, Shared>` which allows sharing the array.
166
- ///
167
- /// `Id<NSArray<T, Owned>, Shared>` is possible, but pretty useless.
168
- /// TODO: Can we make it impossible? Should we?
169
- ///
170
- /// What about `Id<NSArray<T, Shared>, Owned>`?
171
- pub struct NSArray < T , O : Ownership > {
172
- item : PhantomData < Id < T , O > > ,
173
- }
174
-
175
- object_impl ! ( unsafe NSArray <T , O : Ownership >) ;
176
-
177
- unsafe impl < T : INSObject , O : Ownership > INSObject for NSArray < T , O > {
178
- fn class ( ) -> & ' static Class {
179
- class ! ( NSArray )
161
+ object ! (
162
+ /// TODO
163
+ ///
164
+ /// You can have a `Id<NSArray<T, Owned>, Owned>`, which allows mutable access
165
+ /// to the elements (without modifying the array itself), and
166
+ /// `Id<NSArray<T, Shared>, Shared>` which allows sharing the array.
167
+ ///
168
+ /// `Id<NSArray<T, Owned>, Shared>` is possible, but pretty useless.
169
+ /// TODO: Can we make it impossible? Should we?
170
+ ///
171
+ /// What about `Id<NSArray<T, Shared>, Owned>`?
172
+ unsafe pub struct NSArray <T , O : Ownership > {
173
+ item: PhantomData <Id <T , O >>,
180
174
}
181
- }
175
+ ) ;
182
176
183
177
unsafe impl < T : INSObject , O : Ownership > INSArray for NSArray < T , O > {
184
178
/// The `NSArray` itself (length and number of items) is always immutable,
@@ -319,17 +313,11 @@ pub unsafe trait INSMutableArray: INSArray {
319
313
}
320
314
}
321
315
322
- pub struct NSMutableArray < T , O : Ownership > {
323
- item : PhantomData < Id < T , O > > ,
324
- }
325
-
326
- object_impl ! ( unsafe NSMutableArray <T , O : Ownership >) ;
327
-
328
- unsafe impl < T : INSObject , O : Ownership > INSObject for NSMutableArray < T , O > {
329
- fn class ( ) -> & ' static Class {
330
- class ! ( NSMutableArray )
316
+ object ! (
317
+ unsafe pub struct NSMutableArray <T , O : Ownership > {
318
+ item: PhantomData <Id <T , O >>,
331
319
}
332
- }
320
+ ) ;
333
321
334
322
unsafe impl < T : INSObject , O : Ownership > INSArray for NSMutableArray < T , O > {
335
323
type Ownership = Owned ;
@@ -364,14 +352,15 @@ impl<T: INSObject, O: Ownership> Index<usize> for NSMutableArray<T, O> {
364
352
365
353
#[ cfg( test) ]
366
354
mod tests {
355
+ use alloc:: format;
367
356
use alloc:: vec;
368
357
use alloc:: vec:: Vec ;
369
358
370
359
use objc2:: msg_send;
371
360
use objc2:: rc:: { autoreleasepool, Id , Owned , Shared } ;
372
361
373
362
use super :: { INSArray , INSMutableArray , NSArray , NSMutableArray } ;
374
- use crate :: { INSObject , INSString , NSObject , NSString } ;
363
+ use crate :: { INSObject , INSString , INSValue , NSObject , NSString , NSValue } ;
375
364
376
365
fn sample_array ( len : usize ) -> Id < NSArray < NSObject , Owned > , Owned > {
377
366
let mut vec = Vec :: with_capacity ( len) ;
@@ -381,6 +370,14 @@ mod tests {
381
370
NSArray :: from_vec ( vec)
382
371
}
383
372
373
+ fn sample_number_array ( len : u8 ) -> Id < NSArray < NSValue < u8 > , Shared > , Shared > {
374
+ let mut vec = Vec :: with_capacity ( len as usize ) ;
375
+ for i in 0 ..len {
376
+ vec. push ( NSValue :: new ( i) ) ;
377
+ }
378
+ NSArray :: from_vec ( vec)
379
+ }
380
+
384
381
fn retain_count < T : INSObject > ( obj : & T ) -> usize {
385
382
unsafe { msg_send ! [ obj, retainCount] }
386
383
}
@@ -394,6 +391,33 @@ mod tests {
394
391
assert_eq ! ( array. len( ) , 4 ) ;
395
392
}
396
393
394
+ #[ test]
395
+ fn test_equality ( ) {
396
+ let array1 = sample_array ( 3 ) ;
397
+ let array2 = sample_array ( 3 ) ;
398
+ assert_ne ! ( array1, array2) ;
399
+
400
+ let array1 = sample_number_array ( 3 ) ;
401
+ let array2 = sample_number_array ( 3 ) ;
402
+ assert_eq ! ( array1, array2) ;
403
+
404
+ let array1 = sample_number_array ( 3 ) ;
405
+ let array2 = sample_number_array ( 4 ) ;
406
+ assert_ne ! ( array1, array2) ;
407
+ }
408
+
409
+ #[ test]
410
+ #[ ignore = "Output is different depending on OS version and runtime" ]
411
+ fn test_debug ( ) {
412
+ let obj = sample_number_array ( 3 ) ;
413
+ let expected = r#"(
414
+ "<00>",
415
+ "<01>",
416
+ "<02>"
417
+ )"# ;
418
+ assert_eq ! ( format!( "{:?}" , obj) , format!( "{:?}" , expected) ) ;
419
+ }
420
+
397
421
#[ test]
398
422
fn test_get ( ) {
399
423
let array = sample_array ( 4 ) ;
0 commit comments