Crates.io | syncell |
lib.rs | syncell |
version | 0.1.0 |
source | src |
created_at | 2022-06-02 04:17:10.550786 |
updated_at | 2022-06-02 06:16:07.823733 |
description | Sync alternative to RefCell |
homepage | https://github.com/kvark/syncell |
repository | |
max_upload_size | |
id | 598653 |
size | 11,139 |
Just a Sync
alternative to std::cell::RefCell
. Useful when you have a value to share between tasks/closures,
and you already know or guarantee that the access to the value is safe (for example, via choir task dependencies).
The cost of borrowing is a single atomic operation, and it's riguriously checked by both Loom and Miri.
Rust already has tools for sharing values between threads, but it strikes me that they are all rather involved and complicated:
Shareable mutable containers exist to permit mutability in a controlled manner, even in the presence of aliasing. Both
Cell<T>
andRefCell<T>
allow doing this in a single-threaded way. However, neitherCell<T>
norRefCell<T>
are thread safe (they do not implementSync
). If you need to do aliasing and mutation between multiple threads it is possible to useMutex<T>
,RwLock<T>
or atomic types.
This paragraph from std::cell documentation proposes to use lock-based primitives as an alternative in Sync
world. But what if you just need sharing without locking, and still want safety? SynCell
comes to help.