pub struct OnceLockCompat<T> { /* private fields */ }Expand description
A cache-invalidation wrapper with consistent semantics across std and no_std.
| Feature | Backing type |
|---|---|
std | std::sync::OnceLock<T> |
| no_std | once_cell::race::OnceBox<T> |
§Why not Clone?
Under std, OnceLock::clone copies the initialized value.
Under no_std, OnceBox does not support value extraction, so a “clone”
would silently produce an empty instance — a violation of the Clone
contract. Clone is therefore not implemented on either configuration.
If you need a fresh instance, call OnceLockCompat::new() explicitly.
Implementations§
Source§impl<T> OnceLockCompat<T>
impl<T> OnceLockCompat<T>
Sourcepub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
pub fn get_or_init<F>(&self, f: F) -> &Twhere
F: FnOnce() -> T,
Returns the value if initialized, or initializes it with f.
If multiple threads race, each may execute f, but only one value is
stored; the losers’ values are dropped immediately.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Invalidates the cache so the next get_or_init
recomputes the value.
§no_std limitation
Under no_std the backing OnceBox does not support extracting its value,
so the stored value is dropped on reset and cannot be recovered.
If you need the value before invalidating, call get first.