Struct lazyonce::LazyOnce

source ·
pub struct LazyOnce<T> { /* private fields */ }
Expand description

Wrapper type for lazy initialization

Implementations§

source§

impl<T> LazyOnce<T>

source

pub fn new() -> Self

source

pub fn get(&self, f: impl FnOnce() -> T) -> &T

Get cached value, or if no value has been cached yet, compute it using the callback function.

If the callback panics, any use of the cell will panic, too.

source

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

Get cached value or None if the cell is still empty

Does not wait if the cell is being filled in

source

pub fn try_get_for( &self, duration: Duration, f: impl FnOnce() -> T ) -> Option<&T>

Get cached value or None if the cell is still empty

It waits for the cell to be filled in only for a limited time, and falls back to None on timeout

source

pub fn get_mut(&mut self, f: impl FnOnce() -> T) -> &mut T

Get mutable reference to the cached value, or if no value has been cached yet, compute it using the callback function.

source

pub fn expect(&self, s: &str) -> &T

Assume the value has already been computed. Crash if the cell is empty.

§Panics

If get() hasn’t been called yet.

source

pub fn expect_mut(&mut self, s: &str) -> &mut T

Assume the value has already been computed. Crash if the cell is empty.

§Panics

If get() hasn’t been called yet.

source

pub fn into_inner(self) -> Option<T>

Return computed value or None if the cell is empty.

Unlike try_get this returns an owned value, permanently “unwrapping” the cell.

Auto Trait Implementations§

§

impl<T> !Freeze for LazyOnce<T>

§

impl<T> !RefUnwindSafe for LazyOnce<T>

§

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

§

impl<T> Sync for LazyOnce<T>
where T: Send,

§

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

§

impl<T> UnwindSafe for LazyOnce<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>,

§

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>,

§

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.