Skip to content

Commit a270d44

Browse files
committed
Add default Ownership to NSArray and NSMutableArray
1 parent b9c31ff commit a270d44

File tree

5 files changed

+16
-14
lines changed

5 files changed

+16
-14
lines changed

objc2/CHANGELOG_FOUNDATION.md

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2020
of the `NSValue` matches the encoding of the given type.
2121
* Added functions `get_range`, `get_point`, `get_size` and `get_rect` to
2222
`NSValue` to help safely returning various types it will commonly contain.
23+
* `NSArray` and `NSMutableArray` now have sensible defaults for the ownership
24+
of the objects they contain.
2325

2426
### Changed
2527
* **BREAKING**: Moved from external crate `objc2_foundation` into

objc2/src/foundation/array.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ __inner_extern_class! {
5353
// `T: PartialEq` bound correct because `NSArray` does deep (instead of
5454
// shallow) equality comparisons.
5555
#[derive(PartialEq, Eq, Hash)]
56-
unsafe pub struct NSArray<T: Message, O: Ownership>: NSObject {
56+
unsafe pub struct NSArray<T: Message, O: Ownership = Shared>: NSObject {
5757
item: PhantomData<Id<T, O>>,
5858
notunwindsafe: PhantomData<&'static mut ()>,
5959
}
@@ -324,13 +324,13 @@ mod tests {
324324

325325
#[test]
326326
fn test_two_empty() {
327-
let _empty_array1 = NSArray::<NSObject, Shared>::new();
328-
let _empty_array2 = NSArray::<NSObject, Shared>::new();
327+
let _empty_array1 = NSArray::<NSObject>::new();
328+
let _empty_array2 = NSArray::<NSObject>::new();
329329
}
330330

331331
#[test]
332332
fn test_len() {
333-
let empty_array = NSArray::<NSObject, Shared>::new();
333+
let empty_array = NSArray::<NSObject>::new();
334334
assert_eq!(empty_array.len(), 0);
335335

336336
let array = sample_array(4);
@@ -367,7 +367,7 @@ mod tests {
367367
assert_eq!(array.first(), array.get(0));
368368
assert_eq!(array.last(), array.get(3));
369369

370-
let empty_array = <NSArray<NSObject, Shared>>::new();
370+
let empty_array = <NSArray<NSObject>>::new();
371371
assert!(empty_array.first().is_none());
372372
assert!(empty_array.last().is_none());
373373
}

objc2/src/foundation/mutable_array.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ __inner_extern_class! {
2222
///
2323
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nsmutablearray?language=objc
2424
#[derive(PartialEq, Eq, Hash)]
25-
unsafe pub struct NSMutableArray<T: Message, O: Ownership>: NSArray<T, O>, NSObject {
25+
unsafe pub struct NSMutableArray<T: Message, O: Ownership = Owned>: NSArray<T, O>, NSObject {
2626
p: PhantomData<*mut ()>,
2727
}
2828
}

objc2/src/macros/extern_class.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ macro_rules! __inner_extern_class {
189189
// TODO: Expose this variant in the `object` macro.
190190
(
191191
$(#[$m:meta])*
192-
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $($inheritance_chain:ty),+ {
192+
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident $(= $default:ty)?)?),*>: $($inheritance_chain:ty),+ {
193193
$($field_vis:vis $field:ident: $field_ty:ty,)*
194194
}
195195
) => {
196196
$crate::__inner_extern_class! {
197197
@__inner
198198
$(#[$m])*
199-
unsafe $v struct $name<$($t $(: $b)?),*>: $($inheritance_chain,)+ $crate::runtime::Object {
199+
unsafe $v struct $name<$($t $(: $b $(= $default)?)?),*>: $($inheritance_chain,)+ $crate::runtime::Object {
200200
$($field_vis $field: $field_ty,)*
201201
}
202202
}
@@ -217,14 +217,14 @@ macro_rules! __inner_extern_class {
217217
(
218218
@__inner
219219
$(#[$m:meta])*
220-
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $superclass:ty $(, $inheritance_rest:ty)* {
220+
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident $(= $default:ty)?)?),*>: $superclass:ty $(, $inheritance_rest:ty)* {
221221
$($field_vis:vis $field:ident: $field_ty:ty,)*
222222
}
223223
) => {
224224
$(#[$m])*
225225
// TODO: repr(transparent) when the inner pointer is no longer a ZST.
226226
#[repr(C)]
227-
$v struct $name<$($t $(: $b)?),*> {
227+
$v struct $name<$($t $(: $b $(= $default)?)?),*> {
228228
__inner: $superclass,
229229
// Additional fields (should only be zero-sized PhantomData or ivars).
230230
$($field_vis $field: $field_ty,)*

test-ui/ui/nsarray_bound_not_send_sync.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ error[E0277]: `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
77
= help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
88
= note: required because it appears within the type `objc_object`
99
= note: required because it appears within the type `objc2::runtime::Object`
10-
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object, Shared>`
10+
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object>`
1111
note: required by a bound in `needs_sync`
1212
--> ui/nsarray_bound_not_send_sync.rs
1313
|
@@ -26,7 +26,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely
2626
= note: required because it appears within the type `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
2727
= note: required because it appears within the type `objc_object`
2828
= note: required because it appears within the type `objc2::runtime::Object`
29-
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object, Shared>`
29+
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object>`
3030
note: required by a bound in `needs_sync`
3131
--> ui/nsarray_bound_not_send_sync.rs
3232
|
@@ -42,7 +42,7 @@ error[E0277]: `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
4242
= help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
4343
= note: required because it appears within the type `objc_object`
4444
= note: required because it appears within the type `objc2::runtime::Object`
45-
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object, Shared>`
45+
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object>`
4646
note: required by a bound in `needs_send`
4747
--> ui/nsarray_bound_not_send_sync.rs
4848
|
@@ -61,7 +61,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely
6161
= note: required because it appears within the type `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
6262
= note: required because it appears within the type `objc_object`
6363
= note: required because it appears within the type `objc2::runtime::Object`
64-
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object, Shared>`
64+
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object>`
6565
note: required by a bound in `needs_send`
6666
--> ui/nsarray_bound_not_send_sync.rs
6767
|

0 commit comments

Comments
 (0)