maitake::sync::spin::once

Struct Lazy

Source
pub struct Lazy<T, F = fn() -> T> { /* private fields */ }
Expand description

A cell which will be lazily initialized by the provided function the first time it is accessed.

This can be used as a safer alternative to static mut.

Implementations§

Source§

impl<T, F> Lazy<T, F>

Source

pub const fn new(initializer: F) -> Lazy<T, F>

Returns a new Lazy cell, initialized with the provided initializer function.

Source

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

Returns the value of the lazy cell, if it has already been initialized. Otherwise, returns None.

Source§

impl<T, F> Lazy<T, F>
where F: Fn() -> T,

Source

pub fn get(&self) -> &T

Borrow the value, or initialize it if it has not yet been initialized.

Source

pub fn get_mut(&mut self) -> &mut T

Borrow the value mutably, or initialize it if it has not yet been initialized.

Source

pub fn init(&self)

Ensure that the cell has been initialized.

If the cell has yet to be initialized, this initializes it. If it is currently initializing, this spins until it has been fully initialized. Otherwise, this returns immediately.

Trait Implementations§

Source§

impl<T, F> Debug for Lazy<T, F>
where T: Debug,

Source§

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

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

impl<T, F> Deref for Lazy<T, F>
where F: Fn() -> T,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &<Lazy<T, F> as Deref>::Target

Dereferences the value.
Source§

impl<T, F> DerefMut for Lazy<T, F>
where F: Fn() -> T,

Source§

fn deref_mut(&mut self) -> &mut <Lazy<T, F> as Deref>::Target

Mutably dereferences the value.
Source§

impl<T, F> Send for Lazy<T, F>
where T: Send, F: Send,

Source§

impl<T, F> Sync for Lazy<T, F>
where T: Sync, F: Sync,

Auto Trait Implementations§

§

impl<T, F = fn() -> T> !Freeze for Lazy<T, F>

§

impl<T, F = fn() -> T> !RefUnwindSafe for Lazy<T, F>

§

impl<T, F> Unpin for Lazy<T, F>
where F: Unpin, T: Unpin,

§

impl<T, F> UnwindSafe for Lazy<T, F>
where F: UnwindSafe, 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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.