pub struct LazyOnce<T> { /* private fields */ }
Expand description
Wrapper type for lazy initialization
Implementations§
source§impl<T> LazyOnce<T>
impl<T> LazyOnce<T>
pub fn new() -> Self
sourcepub fn get(&self, f: impl FnOnce() -> T) -> &T
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.
sourcepub fn try_get(&self) -> Option<&T>
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
sourcepub fn try_get_for(
&self,
duration: Duration,
f: impl FnOnce() -> T
) -> Option<&T>
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
sourcepub fn get_mut(&mut self, f: impl FnOnce() -> T) -> &mut T
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.
sourcepub fn expect(&self, s: &str) -> &T
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.
sourcepub fn expect_mut(&mut self, s: &str) -> &mut T
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.
sourcepub fn into_inner(self) -> Option<T>
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.