Skip to content

Commit 2021619

Browse files
committed
Update example to use the provided StrongPtr
1 parent 2f906ee commit 2021619

File tree

1 file changed

+5
-21
lines changed

1 file changed

+5
-21
lines changed

examples/example.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,9 @@
22
extern crate objc;
33

44
use objc::Encode;
5+
use objc::rc::StrongPtr;
56
use objc::runtime::{Class, Object};
67

7-
/// Wrapper around an `Object` pointer that will release it when dropped.
8-
struct StrongPtr(*mut Object);
9-
10-
impl std::ops::Deref for StrongPtr {
11-
type Target = Object;
12-
13-
fn deref(&self) -> &Object {
14-
unsafe { &*self.0 }
15-
}
16-
}
17-
18-
impl Drop for StrongPtr {
19-
fn drop(&mut self) {
20-
let _: () = unsafe { msg_send![self.0, release] };
21-
}
22-
}
23-
248
fn main() {
259
// Get a class
2610
let cls = class!(NSObject);
@@ -36,13 +20,13 @@ fn main() {
3620
let obj = unsafe {
3721
let obj: *mut Object = msg_send![cls, alloc];
3822
let obj: *mut Object = msg_send![obj, init];
39-
StrongPtr(obj)
23+
StrongPtr::new(obj)
4024
};
41-
println!("NSObject address: {:p}", &*obj);
25+
println!("NSObject address: {:p}", obj);
4226

4327
// Access an ivar of the object
4428
let isa: *const Class = unsafe {
45-
*obj.get_ivar("isa")
29+
*(**obj).get_ivar("isa")
4630
};
4731
println!("NSObject isa: {:?}", isa);
4832

@@ -55,7 +39,7 @@ fn main() {
5539

5640
// Invoke a method on the object
5741
let hash: usize = unsafe {
58-
msg_send![obj, hash]
42+
msg_send![*obj, hash]
5943
};
6044
println!("NSObject hash: {}", hash);
6145
}

0 commit comments

Comments
 (0)