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§
§impl<T, F> Lazy<T, F>
impl<T, F> Lazy<T, F>
pub const fn new(initializer: F) -> Lazy<T, F>
pub const fn new(initializer: F) -> Lazy<T, F>
Returns a new Lazy
cell, initialized with the provided initializer
function.
pub fn get_if_present(&self) -> Option<&T>
pub fn get_if_present(&self) -> Option<&T>
Returns the value of the lazy cell, if it has already been initialized.
Otherwise, returns None
.
§impl<T, F> Lazy<T, F>where
F: Fn() -> T,
impl<T, F> Lazy<T, F>where
F: Fn() -> T,
pub fn get_mut(&mut self) -> &mut T
pub fn get_mut(&mut self) -> &mut T
Borrow the value mutably, or initialize it if it has not yet been initialized.
pub fn init(&self)
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§
impl<T, F> Send for Lazy<T, F>
impl<T, F> Sync for Lazy<T, F>
Auto Trait Implementations§
impl<T, F = fn() -> T> !RefUnwindSafe for Lazy<T, F>
impl<T, F> Unpin for Lazy<T, F>
impl<T, F> UnwindSafe for Lazy<T, F>where
F: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more