Skip to content

Commit 41dd355

Browse files
committed
Fix NSArray::new
Found while experimenting with verification using associated objects in #127.
1 parent b8f9b88 commit 41dd355

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

objc2-foundation/src/array.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,13 @@ unsafe fn from_refs<T: Message + ?Sized>(cls: &Class, refs: &[&T]) -> *mut Objec
5757
}
5858
}
5959

60-
impl<T: Message, O: Ownership> NSArray<T, O> {
60+
impl<T: Message> NSArray<T, Shared> {
6161
unsafe_def_fn! {
62-
/// The `NSArray` itself (length and number of items) is always immutable,
63-
/// but we would like to know when we're the only owner of the array, to
64-
/// allow mutation of the array's items.
65-
pub fn new -> O;
62+
pub fn new -> Shared;
6663
}
64+
}
6765

66+
impl<T: Message, O: Ownership> NSArray<T, O> {
6867
#[doc(alias = "count")]
6968
pub fn len(&self) -> usize {
7069
unsafe { msg_send![self, count] }
@@ -103,6 +102,9 @@ impl<T: Message, O: Ownership> NSArray<T, O> {
103102
}
104103
}
105104

105+
// The `NSArray` itself (length and number of items) is always immutable,
106+
// but we would like to know when we're the only owner of the array, to
107+
// allow mutation of the array's items.
106108
pub fn from_vec(vec: Vec<Id<T, O>>) -> Id<Self, O> {
107109
unsafe { Id::new(from_refs(Self::class(), vec.as_slice_ref()).cast()).unwrap() }
108110
}
@@ -204,8 +206,8 @@ impl<T: Message, O: Ownership> Index<usize> for NSArray<T, O> {
204206
}
205207
}
206208

207-
impl<T: Message, O: Ownership> DefaultId for NSArray<T, O> {
208-
type Ownership = O;
209+
impl<T: Message> DefaultId for NSArray<T, Shared> {
210+
type Ownership = Shared;
209211

210212
#[inline]
211213
fn default_id() -> Id<Self, Self::Ownership> {
@@ -407,9 +409,15 @@ mod tests {
407409
unsafe { msg_send![obj, retainCount] }
408410
}
409411

412+
#[test]
413+
fn test_two_empty() {
414+
let _empty_array1 = NSArray::<NSObject, _>::new();
415+
let _empty_array2 = NSArray::<NSObject, _>::new();
416+
}
417+
410418
#[test]
411419
fn test_len() {
412-
let empty_array = NSArray::<NSObject, Owned>::new();
420+
let empty_array = NSArray::<NSObject, _>::new();
413421
assert_eq!(empty_array.len(), 0);
414422

415423
let array = sample_array(4);
@@ -450,7 +458,7 @@ mod tests {
450458
assert_eq!(array.first(), array.get(0));
451459
assert_eq!(array.last(), array.get(3));
452460

453-
let empty_array = <NSArray<NSObject, Owned>>::new();
461+
let empty_array = <NSArray<NSObject, Shared>>::new();
454462
assert!(empty_array.first().is_none());
455463
assert!(empty_array.last().is_none());
456464
}

0 commit comments

Comments
 (0)