once_cell_no_std 0.1.1

Sync single assignment cells for `no_std`
Documentation
  • Coverage
  • 93.33%
    14 out of 15 items documented9 out of 14 items with examples
  • Size
  • Source code size: 54.47 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.81 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 13s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • phil-opp/once_cell_no_std
    4 2 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • phil-opp

once_cell_no_std

The once_cell_no_std crate provides a no_std OnceCell type that implements Sync and can be used in statics. It does not use spinlocks or any other form of blocking. Instead, concurrent initialization is reported as an explicit ConcurrentInitialization error that the caller can handle as it likes.

OnceCell might store arbitrary non-Copy types, can be assigned to at most once and provide direct access to the stored contents. In a nutshell, API looks roughly like this:

impl OnceCell<T> {
    fn new() -> OnceCell<T> { ... }
    fn set(&self, value: T) -> Result<Result<(), T>, ConcurrentInitialization> { ... }
    fn get(&self) -> Option<&T> { ... }
}

Note that, like with RefCell and Mutex, the set method requires only a shared reference. Because of the single assignment restriction get can return an &T instead of Ref<T> or MutexGuard<T>.

More patterns and use-cases are in the docs!

Related crates

This crate was forked from the great once_cell crate. The original once_cell crate provides two flavors of OnceCell types: unsync::OnceCell and sync::OnceCell. The following table compares the types against once_cell_no_std::OnceCell:

OnceCell (this crate) once_cell::sync::OnceCell once_cell::unsync::OnceCell
implements Sync yes yes no
concurrent initialization leads to ConcurrentInitialization error returned thread blocked cannot happen
no_std supported yes partially (requires critical-section implementation) yes

Parts of once_cell API are included into std/core as of Rust 1.70.0. The following table compares once_cell_no_std::OnceCell against the core::cell::OnceCell and std::sync::OnceLock types:

OnceCell (this crate) std::sync::OnceLock core::cell::OnceCell
implements Sync yes yes no
concurrent initialization leads to ConcurrentInitialization error returned thread blocked cannot happen
no_std supported yes no yes

For more related crates, check out the README of once_cell.