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:

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.

Structs

A value which is initialized on the first access.
A thread-safe cell which can be written to only once.