File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -16,6 +16,34 @@ let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
16
16
let _ : () = msg_send! [obj , release ];
17
17
```
18
18
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
+
19
47
## Declaring classes
20
48
21
49
Classes can be declared using the ` ClassDecl ` struct. Instance variables and
You can’t perform that action at this time.
0 commit comments