Skip to content

Commit f4710dc

Browse files
committed
feat: multithread support
1 parent 76287a4 commit f4710dc

18 files changed

Lines changed: 3249 additions & 1040 deletions

File tree

‎Cargo.lock‎

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ homepage = "https://github.com/compio-rs/thin-cell"
1111
keywords = ["rc", "refcell", "smart-pointer", "interior-mutability", "thin"]
1212
categories = ["data-structures", "memory-management"]
1313
readme = "README.md"
14+
15+
[dependencies]
16+
synchrony = "0.1.5"

‎README.md‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,23 @@
77
[![Test](https://github.com/compio-rs/thin-cell/actions/workflows/ci_test.yml/badge.svg)](https://github.com/compio-rs/thin-cell/actions/workflows/ci_test.yml)
88
[![Telegram](https://img.shields.io/badge/Telegram-compio--rs-blue?logo=telegram)](https://t.me/compio_rs)
99

10+
A compact smart pointer combining reference counting and interior mutability.
1011

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`.
1413

1514
## Features
1615

1716
- One-`usize` pointer, no matter what `T` is
18-
- Reference counted ownership (like `Rc`)
17+
- Reference counted ownership (like `Rc` or `Arc`)
1918
- Interior mutability with only mutable borrows (so it only needs 1-bit to
2019
track borrow state)
20+
- Both `sync` and `unsync` versions, with the same API and slightly different behavior on borrow rules (see below)
2121

2222
## How It Works
2323

2424
`ThinCell` achieves its compact representation by storing metadata inline at offset 0 of the allocation (for unsized types) like `ThinBox` does.
2525

26-
Overall layout:
26+
Simplified layout:
2727

2828
```rust,ignore
2929
struct Inner<T> {
@@ -35,7 +35,12 @@ struct Inner<T> {
3535

3636
## Borrow Rules
3737

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.
3944

4045
# Examples
4146

‎flake.nix‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,7 @@
2727
{
2828
devShells.default = mkShell {
2929
buildInputs = [
30-
go
31-
cmake
32-
glib
33-
lldb
34-
openssl
35-
pkg-config
30+
cargo-nextest
3631
(rust-bin.selectLatestNightlyWith (
3732
toolchain:
3833
toolchain.default.override {

‎src/fat_ptr.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ pub struct FatPtr {
1111
pub metadata: usize,
1212
}
1313

14-
pub const fn is_fat<T: ?Sized>() -> bool {
15-
size_of::<*const T>() != size_of::<*const ()>()
14+
pub const fn is_sized<T: ?Sized>() -> bool {
15+
size_of::<*const T>() == size_of::<*const ()>()
1616
}
1717

1818
impl FatPtr {
1919
const fn assert_fat<T: ?Sized>() {
2020
const {
2121
assert!(
22-
is_fat::<T>(),
23-
"`T` must be a `!Thin` type, i.e., `*const T` must be a fat pointer"
22+
!is_sized::<T>(),
23+
"`T` must be a `!Thin` type, i.e., `*mut T` must be a fat pointer"
2424
);
2525
}
2626
}

0 commit comments

Comments
 (0)