Skip to main content

OnceLockCompat

Struct OnceLockCompat 

Source
pub struct OnceLockCompat<T> { /* private fields */ }
Expand description

A cache-invalidation wrapper with consistent semantics across std and no_std.

FeatureBacking type
stdstd::sync::OnceLock<T>
no_stdonce_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>

Source

pub const fn new() -> Self

Creates a new, empty OnceLockCompat.

Source

pub fn get(&self) -> Option<&T>

Returns the value if already initialized, otherwise None.

Source

pub fn get_or_init<F>(&self, f: F) -> &T
where 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.

Source

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.

Trait Implementations§

Source§

impl<T: Debug> Debug for OnceLockCompat<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for OnceLockCompat<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for OnceLockCompat<T>

§

impl<T> RefUnwindSafe for OnceLockCompat<T>

§

impl<T> Send for OnceLockCompat<T>
where T: Send,

§

impl<T> Sync for OnceLockCompat<T>
where T: Sync + Send,

§

impl<T> Unpin for OnceLockCompat<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for OnceLockCompat<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for OnceLockCompat<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.