| Crates.io | atomic_once_cell |
| lib.rs | atomic_once_cell |
| version | 0.2.0 |
| created_at | 2021-12-21 21:30:39.604004+00 |
| updated_at | 2025-02-13 08:03:10.952089+00 |
| description | Thread-safe and lock-free OnceCell and Lazy |
| homepage | |
| repository | https://github.com/rustne-kretser/atomic_once_cell |
| max_upload_size | |
| id | 501338 |
| size | 26,848 |
atomic_once_cell provides two new types, [AtomicOnceCell] and
[AtomicLazy], which are thread-safe and mostly lock-free drop-in
replacements of [core::lazy::OnceCell] and [core::lazy::Lazy]
suitable for use in #[no_std] environments.
Because dereferencing [AtomicLazy] can't fail, it can't be
lock-free (if you know a way, please tell me).
Both types can be used in a non-blocking way, but there are some
blocking calls that should not be used from interrupt handlers or
other contexts where blocking will lead to a deadlock. Blocking is
based on
crossbeam::utils::Backoff,
and will be reduced to a spinlock in #[no_std] environments.
AtomicOnceCelluse atomic_once_cell::AtomicOnceCell;
static CELL: AtomicOnceCell<String> = AtomicOnceCell::new();
fn main() {
CELL.set("Hello, World!".to_owned()).unwrap();
assert_eq!(*CELL.get().unwrap(), "Hello, World!");
}
AtomicLazyuse atomic_once_cell::AtomicLazy;
static LAZY: AtomicLazy<String> = AtomicLazy::new(|| "Hello, World!".to_owned());
fn main() {
assert_eq!(*LAZY, "Hello, World!");
}
For more details, see docs.
Add this to your Cargo.toml:
[dependencies]
atomic_once_cell = "0.2.0"
MPL-2.0