Expand description
§Overview
generic_once_cell is a generic no_std version of once_cell.
Internal synchronization for initialization is provided as type parameter via custom mutexes based on lock_api.
This makes it suitable for use in complex no_std scenarios where once_cell’s critical-section support and once_cell::race are not sufficient.
The core API looks roughly like this:
impl<R: lock_api::RawMutex, T> OnceCell<R, T> {
const fn new() -> Self { ... }
fn set(&self, value: T) -> Result<(), T> { ... }
fn get(&self) -> Option<&T> { ... }
}§API
Apart from the generic type parameter, this crate has exactly once_cell’s API.
§Mutex implementations
You can plug in any implementation of lock_api::RawMutex.
Different usable mutex implementations are available in crates:
- spinning-top from Rust OSDev.
- spin, though you might prefer
spin::once::Onceandspin::lazy::Lazy. - parking_lot, though you might prefer the original once_cell.
This crate shows its real strength when using custom, specialized mutex implementations though.
§Performance
The mutex is only used for initialization.
Once initialized, the overhead for each access is to check an AtomicBool.