Skip to content

Commit 7eec09b

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

File tree

4 files changed

+12
-10
lines changed

4 files changed

+12
-10
lines changed

objc2/CHANGELOG_FOUNDATION.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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,)*

0 commit comments

Comments
 (0)