Skip to content

Commit 923b893

Browse files
authored
Merge pull request #21 from madsmtm/id-improvements
A bunch of `Id` improvements
2 parents 39c7ac1 + baff6f9 commit 923b893

25 files changed

+930
-757
lines changed

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ members = [
66
"objc2_exception",
77
"objc2_foundation",
88
"objc2_foundation_derive",
9-
"objc2_id",
109
"objc2_sys",
1110
"objc2_test_utils",
1211
]

objc2/README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,33 @@ unsafe {
2929

3030
The utilities of the `rc` module provide ARC-like semantics for working with
3131
Objective-C's reference counted objects in Rust.
32-
A `StrongPtr` retains an object and releases the object when dropped.
33-
A `WeakPtr` will not retain the object, but can be upgraded to a `StrongPtr`
34-
and safely fails if the object has been deallocated.
32+
33+
An `Id` retains an object and releases the object when dropped.
34+
A `WeakId` will not retain the object, but can be upgraded to an `Id` and
35+
safely fails if the object has been deallocated.
3536

3637
```rust , no_run
3738
use objc2::{class, msg_send};
38-
use objc2::rc::{autoreleasepool, StrongPtr};
39+
use objc2::rc::{autoreleasepool, Id, Shared, WeakId};
40+
use objc2::runtime::Object;
3941

40-
// StrongPtr will release the object when dropped
41-
let obj = unsafe {
42-
StrongPtr::new(msg_send![class!(NSObject), new])
42+
// Id will release the object when dropped
43+
let obj: Id<Object, Shared> = unsafe {
44+
Id::new(msg_send![class!(NSObject), new])
4345
};
4446

4547
// Cloning retains the object an additional time
4648
let cloned = obj.clone();
47-
autoreleasepool(|_| {
48-
// Autorelease consumes the StrongPtr, but won't
49+
autoreleasepool(|pool| {
50+
// Autorelease consumes the Id, but won't
4951
// actually release until the end of an autoreleasepool
50-
cloned.autorelease();
52+
let obj_ref: &Object = cloned.autorelease(pool);
5153
});
5254

5355
// Weak references won't retain the object
54-
let weak = obj.weak();
56+
let weak = WeakId::new(&obj);
5557
drop(obj);
56-
assert!(weak.load().is_null());
58+
assert!(weak.load().is_none());
5759
```
5860

5961
## Declaring classes

objc2/examples/introspection.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use objc2::rc::StrongPtr;
1+
use core::ptr::NonNull;
2+
3+
use objc2::rc::{Id, Owned};
24
use objc2::runtime::{Class, Object};
35
use objc2::{class, msg_send, sel, Encode};
46

@@ -14,15 +16,15 @@ fn main() {
1416
}
1517

1618
// Allocate an instance
17-
let obj = unsafe {
19+
let obj: Id<Object, Owned> = unsafe {
1820
let obj: *mut Object = msg_send![cls, alloc];
19-
let obj: *mut Object = msg_send![obj, init];
20-
StrongPtr::new(obj)
21+
let obj: NonNull<Object> = msg_send![obj, init];
22+
Id::new(obj)
2123
};
2224
println!("NSObject address: {:p}", obj);
2325

2426
// Access an ivar of the object
25-
let isa: *const Class = unsafe { *(**obj).get_ivar("isa") };
27+
let isa: *const Class = unsafe { *obj.get_ivar("isa") };
2628
println!("NSObject isa: {:?}", isa);
2729

2830
// Inspect a method of the class
@@ -33,6 +35,6 @@ fn main() {
3335
assert!(*hash_return == usize::ENCODING);
3436

3537
// Invoke a method on the object
36-
let hash: usize = unsafe { msg_send![*obj, hash] };
38+
let hash: usize = unsafe { msg_send![obj, hash] };
3739
println!("NSObject hash: {}", hash);
3840
}

objc2/src/exception.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
use crate::rc::StrongPtr;
1+
use core::ptr::NonNull;
2+
3+
use crate::rc::Id;
24
use crate::runtime::Object;
5+
use objc2_exception::{r#try, Exception};
36

47
// Comment copied from `objc2_exception`
58

@@ -18,6 +21,6 @@ use crate::runtime::Object;
1821
/// undefined behaviour until `C-unwind` is stabilized, see [RFC-2945].
1922
///
2023
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
21-
pub unsafe fn catch_exception<R>(closure: impl FnOnce() -> R) -> Result<R, StrongPtr> {
22-
objc2_exception::r#try(closure).map_err(|exception| StrongPtr::new(exception as *mut Object))
24+
pub unsafe fn catch_exception<R>(closure: impl FnOnce() -> R) -> Result<R, Id<Exception>> {
25+
r#try(closure).map_err(|e| Id::new(NonNull::new(e).unwrap()))
2326
}

objc2/src/macros.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,15 @@ macro_rules! sel {
6666
Sends a message to an object.
6767
6868
The first argument can be any type that dereferences to a type that implements
69-
[`Message`], like a reference, a pointer, or an `objc2_id::Id` to an object.
69+
[`Message`], like a reference, a pointer, or an [`rc::Id`] to an
70+
object.
7071
7172
The syntax is similar to the message syntax in Objective-C.
7273
7374
Variadic arguments are not currently supported.
7475
7576
[`Message`]: crate::Message
77+
[`rc::Id`]: crate::rc::Id
7678
7779
# Panics
7880

objc2/src/rc/autorelease.rs

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ use crate::runtime::{objc_autoreleasePoolPop, objc_autoreleasePoolPush};
1313
///
1414
/// And this is not [`Sync`], since you can only autorelease a reference to a
1515
/// pool on the current thread.
16-
///
17-
/// See [the clang documentation][clang-arc] and [the apple article on memory
18-
/// management][memory-mgmt] for more information on automatic reference
19-
/// counting.
20-
///
21-
/// [clang-arc]: https://clang.llvm.org/docs/AutomaticReferenceCounting.html
22-
/// [memory-mgmt]: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
2316
pub struct AutoreleasePool {
2417
context: *mut c_void,
2518
}

0 commit comments

Comments
 (0)