Skip to content

Commit fd3e7de

Browse files
committed
Expand rc module docs
1 parent 2021619 commit fd3e7de

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ let _: () = msg_send![obj, release];
2020
# }
2121
```
2222
23+
# Reference counting
24+
25+
Utilities for reference counting Objective-C objects are provided in the
26+
[`rc`](rc/index.html) module.
27+
2328
# Declaring classes
2429
2530
Objective-C classes can even be declared from Rust using the functionality of

src/rc/autorelease.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ impl Drop for AutoReleaseHelper {
2020

2121
/**
2222
Execute `f` in the context of a new autorelease pool. The pool is drained
23-
after the execution of `f` completes. This corresponds to @autoreleasepool blocks
24-
in Objective-c and Swift.
23+
after the execution of `f` completes.
24+
25+
This corresponds to `@autoreleasepool` blocks in Objective-C and Swift.
2526
*/
2627
pub fn autoreleasepool<F: FnOnce()>(f: F) {
2728
let _context = unsafe { AutoReleaseHelper::new() };

src/rc/mod.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
/*!
2-
Utilities for reference-counting Objective-C objects.
2+
Utilities for reference counting Objective-C objects.
33
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.
712
813
For more information on Objective-C's reference counting, see Apple's documentation:
914
<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+
```
1041
*/
1142

1243
mod strong;

0 commit comments

Comments
 (0)