You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit f4710dc
Browse filesBrowse the repository at this point in the historyBrowse files
A compact smart pointer combining reference counting and interior mutability.
10
11
11
-
A compact, single-threaded smart pointer combining reference counting and interior mutability.
12
-
13
-
`ThinCell` is a space-efficient alternative to `Rc` and `borrow_mut`-only `RefCell` that itself is always **1 pointer-sized** no matter if `T` is `Sized` or not (like `ThinBox`), compare to `Rc<RefCell<T>>` which is 2 pointer-sized for `T: !Sized`.
12
+
`ThinCell` is a space-efficient alternative to `Rc<RefCell<T>>` or `Arc<Mutex<T>>` that itself is always **1 pointer-sized** no matter if `T` is `Sized` or not (like `ThinBox`), compare to `Rc<RefCell<T>>` which is 2 pointer-sized for `T: !Sized`.
14
13
15
14
## Features
16
15
17
16
- One-`usize` pointer, no matter what `T` is
18
-
- Reference counted ownership (like `Rc`)
17
+
- Reference counted ownership (like `Rc` or `Arc`)
19
18
- Interior mutability with only mutable borrows (so it only needs 1-bit to
20
19
track borrow state)
20
+
- Both `sync` and `unsync` versions, with the same API and slightly different behavior on borrow rules (see below)
21
21
22
22
## How It Works
23
23
24
24
`ThinCell` achieves its compact representation by storing metadata inline at offset 0 of the allocation (for unsized types) like `ThinBox` does.
25
25
26
-
Overall layout:
26
+
Simplified layout:
27
27
28
28
```rust,ignore
29
29
struct Inner<T> {
@@ -35,7 +35,12 @@ struct Inner<T> {
35
35
36
36
## Borrow Rules
37
37
38
-
Unlike `RefCell` which supports multiple immutable borrows OR one mutable borrow, `ThinCell` only supports **one mutable borrow at a time**. Attempting to borrow while already borrowed will panic with `borrow` or return `None` with `try_borrow`.
38
+
Unlike `RefCell` which supports multiple immutable borrows OR one mutable borrow, `ThinCell` only supports **one mutable borrow at a time**. Attempting to borrow while already borrowed will:
39
+
40
+
-`sync::ThinCell<T>`: Thread-safe, uses `AtomicUsize` and blocking borrow semantics: borrowing while already borrowed will busy loop and `yield_now`.
41
+
-`unsync::ThinCell<T>`: Single threaded, uses `Cell<usize>` and panicking borrow semantics: borrowing while already borrowed will panic.
42
+
43
+
`try_borrow` is available for both versions, which returns `None` instead of panicking or blocking when already borrowed.
0 commit comments