From a270d44b7a6ff0d69a7edbc1fb812b505333725d Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Fri, 22 Jul 2022 03:35:32 +0200 Subject: [PATCH] Add default Ownership to NSArray and NSMutableArray --- objc2/CHANGELOG_FOUNDATION.md | 2 ++ objc2/src/foundation/array.rs | 10 +++++----- objc2/src/foundation/mutable_array.rs | 2 +- objc2/src/macros/extern_class.rs | 8 ++++---- test-ui/ui/nsarray_bound_not_send_sync.stderr | 8 ++++---- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/objc2/CHANGELOG_FOUNDATION.md b/objc2/CHANGELOG_FOUNDATION.md index 3055cc0f0..fcfdc80f9 100644 --- a/objc2/CHANGELOG_FOUNDATION.md +++ b/objc2/CHANGELOG_FOUNDATION.md @@ -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 diff --git a/objc2/src/foundation/array.rs b/objc2/src/foundation/array.rs index 73ee24e2d..25592b7fa 100644 --- a/objc2/src/foundation/array.rs +++ b/objc2/src/foundation/array.rs @@ -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: NSObject { + unsafe pub struct NSArray: NSObject { item: PhantomData>, notunwindsafe: PhantomData<&'static mut ()>, } @@ -324,13 +324,13 @@ mod tests { #[test] fn test_two_empty() { - let _empty_array1 = NSArray::::new(); - let _empty_array2 = NSArray::::new(); + let _empty_array1 = NSArray::::new(); + let _empty_array2 = NSArray::::new(); } #[test] fn test_len() { - let empty_array = NSArray::::new(); + let empty_array = NSArray::::new(); assert_eq!(empty_array.len(), 0); let array = sample_array(4); @@ -367,7 +367,7 @@ mod tests { assert_eq!(array.first(), array.get(0)); assert_eq!(array.last(), array.get(3)); - let empty_array = >::new(); + let empty_array = >::new(); assert!(empty_array.first().is_none()); assert!(empty_array.last().is_none()); } diff --git a/objc2/src/foundation/mutable_array.rs b/objc2/src/foundation/mutable_array.rs index eb4d95abf..10f8d7379 100644 --- a/objc2/src/foundation/mutable_array.rs +++ b/objc2/src/foundation/mutable_array.rs @@ -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: NSArray, NSObject { + unsafe pub struct NSMutableArray: NSArray, NSObject { p: PhantomData<*mut ()>, } } diff --git a/objc2/src/macros/extern_class.rs b/objc2/src/macros/extern_class.rs index 46a74b966..7a37e3949 100644 --- a/objc2/src/macros/extern_class.rs +++ b/objc2/src/macros/extern_class.rs @@ -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,)* } } @@ -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,)* diff --git a/test-ui/ui/nsarray_bound_not_send_sync.stderr b/test-ui/ui/nsarray_bound_not_send_sync.stderr index a6c04f106..76903c6ec 100644 --- a/test-ui/ui/nsarray_bound_not_send_sync.stderr +++ b/test-ui/ui/nsarray_bound_not_send_sync.stderr @@ -7,7 +7,7 @@ error[E0277]: `UnsafeCell, PhantomPinned)>>` = help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `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` + = note: required because of the requirements on the impl of `Sync` for `NSArray` note: required by a bound in `needs_sync` --> ui/nsarray_bound_not_send_sync.rs | @@ -26,7 +26,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely = note: required because it appears within the type `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` + = note: required because of the requirements on the impl of `Sync` for `NSArray` note: required by a bound in `needs_sync` --> ui/nsarray_bound_not_send_sync.rs | @@ -42,7 +42,7 @@ error[E0277]: `UnsafeCell, PhantomPinned)>>` = help: within `objc2::runtime::Object`, the trait `Sync` is not implemented for `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` + = note: required because of the requirements on the impl of `Send` for `NSArray` note: required by a bound in `needs_send` --> ui/nsarray_bound_not_send_sync.rs | @@ -61,7 +61,7 @@ error[E0277]: `*const UnsafeCell<()>` cannot be sent between threads safely = note: required because it appears within the type `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` + = note: required because of the requirements on the impl of `Send` for `NSArray` note: required by a bound in `needs_send` --> ui/nsarray_bound_not_send_sync.rs |