Skip to content

Commit

Permalink
Make NSObject !Send and !Sync
Browse files Browse the repository at this point in the history
Similar to objc2::runtime::Object
  • Loading branch information
madsmtm committed Dec 21, 2021
1 parent 47241b3 commit c58fdac
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 2 additions & 0 deletions objc2-foundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Fixed
* Soundness issue with `NSValue`, `NSDictionary`, `NSArray` and
`NSMutableArray` not being `#[repr(C)]`.
* **BREAKING**: `NSObject` is no longer `Send` and `Sync` (because its
subclasses may not be).

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

Expand Down
10 changes: 10 additions & 0 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,16 @@ unsafe impl<T: Sync + Send> Send for NSArray<T, Shared> {}
unsafe impl<T: Sync> Sync for NSArray<T, Owned> {}
unsafe impl<T: Send> Send for NSArray<T, Owned> {}

/// ```compile_fail
/// use objc2::rc::Shared;
/// use objc2::runtime::Object;
/// use objc2_foundation::NSArray;
/// fn needs_send_sync<T: Send + Sync>() {}
/// needs_send_sync::<NSArray<Object, Shared>>();
/// ```
#[cfg(doctest)]
pub struct NSArrayWithObjectNotSendSync;

unsafe impl<T: INSObject, O: Ownership> INSArray for NSArray<T, O> {
/// The `NSArray` itself (length and number of items) is always immutable,
/// but we would like to know when we're the only owner of the array, to
Expand Down
20 changes: 18 additions & 2 deletions objc2-foundation/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use core::marker::PhantomData;
use core::ptr::NonNull;

use objc2::msg_send;
use objc2::rc::{Id, Owned, Shared};
use objc2::runtime::{Bool, Class};
use objc2::runtime::{Bool, Class, Object};
use objc2::Message;

use super::NSString;
Expand Down Expand Up @@ -37,7 +38,22 @@ pub unsafe trait INSObject: Sized + Message {
}
}

object!(unsafe pub struct NSObject);
object!(unsafe pub struct NSObject<> {
p: PhantomData<Object>, // Temporary
});

/// ```compile_fail
/// use objc2_foundation::NSObject;
/// fn needs_sync<T: Sync>() {}
/// needs_sync::<NSObject>();
/// ```
/// ```compile_fail
/// use objc2_foundation::NSObject;
/// fn needs_send<T: Send>() {}
/// needs_send::<NSObject>();
/// ```
#[cfg(doctest)]
pub struct NSObjectNotSendNorSync;

impl NSObject {
unsafe_def_fn!(pub fn new -> Owned);
Expand Down

0 comments on commit c58fdac

Please sign in to comment.