Skip to content

Commit decaa94

Browse files
committed
Add a README section about the rc module
1 parent fd3e7de commit decaa94

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
1616
let _: () = msg_send![obj, release];
1717
```
1818

19+
## Reference counting
20+
21+
The utilities of the `rc` module provide ARC-like semantics for working with
22+
Objective-C's reference counted objects in Rust.
23+
A `StrongPtr` retains an object and releases the object when dropped.
24+
A `WeakPtr` will not retain the object, but can be upgraded to a `StrongPtr`
25+
and safely fails if the object has been deallocated.
26+
27+
``` rust
28+
// StrongPtr will release the object when dropped
29+
let obj = unsafe {
30+
StrongPtr::new(msg_send![class!(NSObject), new])
31+
};
32+
33+
// Cloning retains the object an additional time
34+
let cloned = obj.clone();
35+
autoreleasepool(|| {
36+
// Autorelease consumes the StrongPtr, but won't
37+
// actually release until the end of an autoreleasepool
38+
cloned.autorelease();
39+
});
40+
41+
// Weak references won't retain the object
42+
let weak = obj.weak();
43+
drop(obj);
44+
assert!(weak.load().is_null());
45+
```
46+
1947
## Declaring classes
2048

2149
Classes can be declared using the `ClassDecl` struct. Instance variables and

0 commit comments

Comments
 (0)