Skip to content

Commit a564776

Browse files
authored
Merge pull request #115 from madsmtm/foundation-deref
Use `Deref` in `objc2-foundation` and remove "`INS`" helper traits
2 parents c0b7df0 + 2029dc2 commit a564776

File tree

15 files changed

+492
-476
lines changed

15 files changed

+492
-476
lines changed

objc2-foundation/CHANGELOG.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,33 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
66

77
## Unreleased - YYYY-MM-DD
88

9+
### Added
10+
* Objects now `Deref` to their superclasses. E.g. `NSMutableArray` derefs to
11+
`NSArray`, which derefs to `NSObject`, which derefs to `Object`.
12+
13+
This allows more ergonomic usage.
14+
15+
### Changed
16+
* **BREAKING**: Removed the following helper traits in favor of inherent
17+
methods on the objects themselves:
18+
- `INSMutableArray`
19+
- `INSArray`
20+
- `INSMutableData`
21+
- `INSData`
22+
- `INSDictionary`
23+
- `INSString`
24+
- `INSValue`
25+
- `INSObject`
26+
27+
This changed because objects now deref to their superclasses.
28+
* **BREAKING**: Relaxed a lot of bounds from `INSObject` to `Message`. At some
29+
point in the future a new trait will be introduced which remedies this
30+
change.
31+
* **BREAKING**: Removed the `I` prefix from:
32+
- `INSCopying` (now `NSCopying`)
33+
- `INSMutableCopying` (now `NSMutableCopying`)
34+
- `INSFastEnumeration` (now `NSFastEnumeration`)
35+
936

1037
## 0.2.0-alpha.4 - 2022-01-03
1138

objc2-foundation/examples/basic_usage.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use objc2::rc::autoreleasepool;
2-
use objc2_foundation::{
3-
INSArray, INSCopying, INSDictionary, INSString, NSArray, NSDictionary, NSObject, NSString,
4-
};
2+
use objc2_foundation::{NSArray, NSCopying, NSDictionary, NSObject, NSString};
53

64
fn main() {
75
// Create and compare NSObjects

objc2-foundation/examples/class_with_lifetime.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use objc2::rc::{Id, Owned, Shared};
77
use objc2::runtime::{Class, Object, Sel};
88
use objc2::{msg_send, sel};
99
use objc2::{Encoding, Message, RefEncode};
10-
use objc2_foundation::{INSObject, NSObject};
10+
use objc2_foundation::NSObject;
1111

1212
#[repr(C)]
1313
pub struct MyObject<'a> {
@@ -23,6 +23,8 @@ unsafe impl RefEncode for MyObject<'_> {
2323

2424
unsafe impl Message for MyObject<'_> {}
2525

26+
static MYOBJECT_REGISTER_CLASS: Once = Once::new();
27+
2628
impl<'a> MyObject<'a> {
2729
fn new(number_ptr: &'a mut u8) -> Id<Self, Owned> {
2830
unsafe {
@@ -48,11 +50,7 @@ impl<'a> MyObject<'a> {
4850
**ptr = number;
4951
}
5052
}
51-
}
52-
53-
static MYOBJECT_REGISTER_CLASS: Once = Once::new();
5453

55-
unsafe impl INSObject for MyObject<'_> {
5654
fn class() -> &'static Class {
5755
MYOBJECT_REGISTER_CLASS.call_once(|| {
5856
let superclass = NSObject::class();

objc2-foundation/examples/custom_class.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use objc2::rc::{Id, Owned};
66
use objc2::runtime::{Class, Object, Sel};
77
use objc2::{msg_send, sel};
88
use objc2::{Encoding, Message, RefEncode};
9-
use objc2_foundation::{INSObject, NSObject};
9+
use objc2_foundation::NSObject;
1010

1111
/// In the future this should be an `extern type`, if that gets stabilized,
1212
/// see [RFC-1861](https://rust-lang.github.io/rfcs/1861-extern-types.html).
@@ -22,6 +22,10 @@ unsafe impl RefEncode for MYObject {
2222
const ENCODING_REF: Encoding<'static> = Encoding::Object;
2323
}
2424

25+
unsafe impl Message for MYObject {}
26+
27+
static MYOBJECT_REGISTER_CLASS: Once = Once::new();
28+
2529
impl MYObject {
2630
fn new() -> Id<Self, Owned> {
2731
let cls = Self::class();
@@ -41,13 +45,7 @@ impl MYObject {
4145
obj.set_ivar("_number", number);
4246
}
4347
}
44-
}
45-
46-
unsafe impl Message for MYObject {}
47-
48-
static MYOBJECT_REGISTER_CLASS: Once = Once::new();
4948

50-
unsafe impl INSObject for MYObject {
5149
fn class() -> &'static Class {
5250
MYOBJECT_REGISTER_CLASS.call_once(|| {
5351
let superclass = NSObject::class();

0 commit comments

Comments
 (0)