Skip to content

Commit e478974

Browse files
committed
Make NSObject !Send and !Sync
Similar to objc2::runtime::Object
1 parent e245f34 commit e478974

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

objc2-foundation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2525
### Fixed
2626
* Soundness issue with `NSValue`, `NSDictionary`, `NSArray` and
2727
`NSMutableArray` not being `#[repr(C)]`.
28+
* **BREAKING**: `NSObject` is no longer `Send` and `Sync` (because its
29+
subclasses may not be).
2830

2931
## 0.2.0-alpha.2 - 2021-11-22
3032

objc2-foundation/src/array.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,16 @@ unsafe impl<T: Sync + Send> Send for NSArray<T, Shared> {}
184184
unsafe impl<T: Sync> Sync for NSArray<T, Owned> {}
185185
unsafe impl<T: Send> Send for NSArray<T, Owned> {}
186186

187+
/// ```compile_fail
188+
/// use objc2::rc::Shared;
189+
/// use objc2::runtime::Object;
190+
/// use objc2_foundation::NSArray;
191+
/// fn needs_send_sync<T: Send + Sync>() {}
192+
/// needs_send_sync::<NSArray<Object, Shared>>();
193+
/// ```
194+
#[cfg(doctest)]
195+
pub struct NSArrayWithObjectNotSendSync;
196+
187197
unsafe impl<T: INSObject, O: Ownership> INSArray for NSArray<T, O> {
188198
/// The `NSArray` itself (length and number of items) is always immutable,
189199
/// but we would like to know when we're the only owner of the array, to

objc2-foundation/src/object.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use core::marker::PhantomData;
12
use core::ptr::NonNull;
23

34
use objc2::msg_send;
45
use objc2::rc::{Id, Owned, Shared};
5-
use objc2::runtime::{Bool, Class};
6+
use objc2::runtime::{Bool, Class, Object};
67
use objc2::Message;
78

89
use super::NSString;
@@ -37,7 +38,22 @@ pub unsafe trait INSObject: Sized + Message {
3738
}
3839
}
3940

40-
object!(unsafe pub struct NSObject);
41+
object!(unsafe pub struct NSObject<> {
42+
p: PhantomData<Object>, // Temporary
43+
});
44+
45+
/// ```compile_fail
46+
/// use objc2_foundation::NSObject;
47+
/// fn needs_sync<T: Sync>() {}
48+
/// needs_sync::<NSObject>();
49+
/// ```
50+
/// ```compile_fail
51+
/// use objc2_foundation::NSObject;
52+
/// fn needs_send<T: Send>() {}
53+
/// needs_send::<NSObject>();
54+
/// ```
55+
#[cfg(doctest)]
56+
pub struct NSObjectNotSendNorSync;
4157

4258
impl NSObject {
4359
unsafe_def_fn!(pub fn new -> Owned);

0 commit comments

Comments
 (0)