|
1 | 1 | /*!
|
2 |
| -Utilities for reference-counting Objective-C objects. |
| 2 | +Utilities for reference counting Objective-C objects. |
3 | 3 |
|
4 |
| -These utilities provide ARC-like semantics in Rust. They are not intended to |
5 |
| -provide a fully safe interface, but can be useful when writing higher-level |
6 |
| -Rust wrappers for Objective-C code. |
| 4 | +The utilities of the `rc` module provide ARC-like semantics for working with |
| 5 | +Objective-C's reference counted objects in Rust. |
| 6 | +A `StrongPtr` retains an object and releases the object when dropped. |
| 7 | +A `WeakPtr` will not retain the object, but can be upgraded to a `StrongPtr` |
| 8 | +and safely fails if the object has been deallocated. |
| 9 | +
|
| 10 | +These utilities are not intended to provide a fully safe interface, but can be |
| 11 | +useful when writing higher-level Rust wrappers for Objective-C code. |
7 | 12 |
|
8 | 13 | For more information on Objective-C's reference counting, see Apple's documentation:
|
9 | 14 | <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html>
|
| 15 | +
|
| 16 | +# Example |
| 17 | +
|
| 18 | +``` no_run |
| 19 | +# #[macro_use] extern crate objc; |
| 20 | +# use objc::rc::{autoreleasepool, StrongPtr}; |
| 21 | +# fn main() { |
| 22 | +// StrongPtr will release the object when dropped |
| 23 | +let obj = unsafe { |
| 24 | + StrongPtr::new(msg_send![class!(NSObject), new]) |
| 25 | +}; |
| 26 | +
|
| 27 | +// Cloning retains the object an additional time |
| 28 | +let cloned = obj.clone(); |
| 29 | +autoreleasepool(|| { |
| 30 | + // Autorelease consumes the StrongPtr, but won't |
| 31 | + // actually release until the end of an autoreleasepool |
| 32 | + cloned.autorelease(); |
| 33 | +}); |
| 34 | +
|
| 35 | +// Weak references won't retain the object |
| 36 | +let weak = obj.weak(); |
| 37 | +drop(obj); |
| 38 | +assert!(weak.load().is_null()); |
| 39 | +# } |
| 40 | +``` |
10 | 41 | */
|
11 | 42 |
|
12 | 43 | mod strong;
|
|
0 commit comments