Skip to content

Commit

Permalink
Add default Ownership to NSArray and NSMutableArray
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 1, 2022
1 parent b9c31ff commit a270d44
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
2 changes: 2 additions & 0 deletions objc2/CHANGELOG_FOUNDATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
of the `NSValue` matches the encoding of the given type.
* Added functions `get_range`, `get_point`, `get_size` and `get_rect` to
`NSValue` to help safely returning various types it will commonly contain.
* `NSArray` and `NSMutableArray` now have sensible defaults for the ownership
of the objects they contain.

### Changed
* **BREAKING**: Moved from external crate `objc2_foundation` into
Expand Down
10 changes: 5 additions & 5 deletions objc2/src/foundation/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ __inner_extern_class! {
// `T: PartialEq` bound correct because `NSArray` does deep (instead of
// shallow) equality comparisons.
#[derive(PartialEq, Eq, Hash)]
unsafe pub struct NSArray<T: Message, O: Ownership>: NSObject {
unsafe pub struct NSArray<T: Message, O: Ownership = Shared>: NSObject {
item: PhantomData<Id<T, O>>,
notunwindsafe: PhantomData<&'static mut ()>,
}
Expand Down Expand Up @@ -324,13 +324,13 @@ mod tests {

#[test]
fn test_two_empty() {
let _empty_array1 = NSArray::<NSObject, Shared>::new();
let _empty_array2 = NSArray::<NSObject, Shared>::new();
let _empty_array1 = NSArray::<NSObject>::new();
let _empty_array2 = NSArray::<NSObject>::new();
}

#[test]
fn test_len() {
let empty_array = NSArray::<NSObject, Shared>::new();
let empty_array = NSArray::<NSObject>::new();
assert_eq!(empty_array.len(), 0);

let array = sample_array(4);
Expand Down Expand Up @@ -367,7 +367,7 @@ mod tests {
assert_eq!(array.first(), array.get(0));
assert_eq!(array.last(), array.get(3));

let empty_array = <NSArray<NSObject, Shared>>::new();
let empty_array = <NSArray<NSObject>>::new();
assert!(empty_array.first().is_none());
assert!(empty_array.last().is_none());
}
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/foundation/mutable_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ __inner_extern_class! {
///
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nsmutablearray?language=objc
#[derive(PartialEq, Eq, Hash)]
unsafe pub struct NSMutableArray<T: Message, O: Ownership>: NSArray<T, O>, NSObject {
unsafe pub struct NSMutableArray<T: Message, O: Ownership = Owned>: NSArray<T, O>, NSObject {
p: PhantomData<*mut ()>,
}
}
Expand Down
8 changes: 4 additions & 4 deletions objc2/src/macros/extern_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ macro_rules! __inner_extern_class {
// TODO: Expose this variant in the `object` macro.
(
$(#[$m:meta])*
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $($inheritance_chain:ty),+ {
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident $(= $default:ty)?)?),*>: $($inheritance_chain:ty),+ {
$($field_vis:vis $field:ident: $field_ty:ty,)*
}
) => {
$crate::__inner_extern_class! {
@__inner
$(#[$m])*
unsafe $v struct $name<$($t $(: $b)?),*>: $($inheritance_chain,)+ $crate::runtime::Object {
unsafe $v struct $name<$($t $(: $b $(= $default)?)?),*>: $($inheritance_chain,)+ $crate::runtime::Object {
$($field_vis $field: $field_ty,)*
}
}
Expand All @@ -217,14 +217,14 @@ macro_rules! __inner_extern_class {
(
@__inner
$(#[$m:meta])*
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident)?),*>: $superclass:ty $(, $inheritance_rest:ty)* {
unsafe $v:vis struct $name:ident<$($t:ident $(: $b:ident $(= $default:ty)?)?),*>: $superclass:ty $(, $inheritance_rest:ty)* {
$($field_vis:vis $field:ident: $field_ty:ty,)*
}
) => {
$(#[$m])*
// TODO: repr(transparent) when the inner pointer is no longer a ZST.
#[repr(C)]
$v struct $name<$($t $(: $b)?),*> {
$v struct $name<$($t $(: $b $(= $default)?)?),*> {
__inner: $superclass,
// Additional fields (should only be zero-sized PhantomData or ivars).
$($field_vis $field: $field_ty,)*
Expand Down
8 changes: 4 additions & 4 deletions test-ui/ui/nsarray_bound_not_send_sync.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ error[E0277]: `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= note: required because it appears within the type `objc_object`
= note: required because it appears within the type `objc2::runtime::Object`
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object, Shared>`
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object>`
note: required by a bound in `needs_sync`
--> ui/nsarray_bound_not_send_sync.rs
|
Expand All @@ -26,7 +26,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely
= note: required because it appears within the type `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= note: required because it appears within the type `objc_object`
= note: required because it appears within the type `objc2::runtime::Object`
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object, Shared>`
= note: required because of the requirements on the impl of `Sync` for `NSArray<objc2::runtime::Object>`
note: required by a bound in `needs_sync`
--> ui/nsarray_bound_not_send_sync.rs
|
Expand All @@ -42,7 +42,7 @@ error[E0277]: `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= note: required because it appears within the type `objc_object`
= note: required because it appears within the type `objc2::runtime::Object`
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object, Shared>`
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object>`
note: required by a bound in `needs_send`
--> ui/nsarray_bound_not_send_sync.rs
|
Expand All @@ -61,7 +61,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely
= note: required because it appears within the type `UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>`
= note: required because it appears within the type `objc_object`
= note: required because it appears within the type `objc2::runtime::Object`
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object, Shared>`
= note: required because of the requirements on the impl of `Send` for `NSArray<objc2::runtime::Object>`
note: required by a bound in `needs_send`
--> ui/nsarray_bound_not_send_sync.rs
|
Expand Down

0 comments on commit a270d44

Please sign in to comment.