pub struct LazyCell<T> { /* private fields */ }
Expand description
A lazily filled Cell
, with mutable contents.
A LazyCell
is completely frozen once filled, unless you have &mut
access to it, in which case LazyCell::borrow_mut
may be used to mutate the
contents.
Implementations
sourceimpl<T> LazyCell<T>
impl<T> LazyCell<T>
sourcepub fn fill(&self, value: T) -> Result<(), T>
pub fn fill(&self, value: T) -> Result<(), T>
Put a value into this cell.
This function will return Err(value)
if the cell is already full.
sourcepub fn replace(&mut self, value: T) -> Option<T>
pub fn replace(&mut self, value: T) -> Option<T>
Put a value into this cell.
Note that this function is infallible but requires &mut self
. By
requiring &mut self
we’re guaranteed that no active borrows to this
cell can exist so we can always fill in the value. This may not always
be usable, however, as &mut self
may not be possible to borrow.
Return value
This function returns the previous value, if any.
sourcepub fn borrow(&self) -> Option<&T>
pub fn borrow(&self) -> Option<&T>
Borrows the contents of this lazy cell for the duration of the cell itself.
This function will return Some
if the cell has been previously
initialized, and None
if it has not yet been initialized.
sourcepub fn borrow_mut(&mut self) -> Option<&mut T>
pub fn borrow_mut(&mut self) -> Option<&mut T>
Borrows the contents of this lazy cell mutably for the duration of the cell itself.
This function will return Some
if the cell has been previously
initialized, and None
if it has not yet been initialized.
sourcepub fn borrow_with<F: FnOnce() -> T>(&self, f: F) -> &T
pub fn borrow_with<F: FnOnce() -> T>(&self, f: F) -> &T
Borrows the contents of this lazy cell for the duration of the cell itself.
If the cell has not yet been filled, the cell is first filled using the function provided.
Panics
Panics if the cell becomes filled as a side effect of f
.
sourcepub fn borrow_mut_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T
pub fn borrow_mut_with<F: FnOnce() -> T>(&mut self, f: F) -> &mut T
Borrows the contents of this LazyCell
mutably for the duration of the
cell itself.
If the cell has not yet been filled, the cell is first filled using the function provided.
Panics
Panics if the cell becomes filled as a side effect of f
.
sourcepub fn try_borrow_with<E, F>(&self, f: F) -> Result<&T, E> where
F: FnOnce() -> Result<T, E>,
pub fn try_borrow_with<E, F>(&self, f: F) -> Result<&T, E> where
F: FnOnce() -> Result<T, E>,
Same as borrow_with
, but allows the initializing function to fail.
Panics
Panics if the cell becomes filled as a side effect of f
.
sourcepub fn try_borrow_mut_with<E, F>(&mut self, f: F) -> Result<&mut T, E> where
F: FnOnce() -> Result<T, E>,
pub fn try_borrow_mut_with<E, F>(&mut self, f: F) -> Result<&mut T, E> where
F: FnOnce() -> Result<T, E>,
Same as borrow_mut_with
, but allows the initializing function to fail.
Panics
Panics if the cell becomes filled as a side effect of f
.
sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes this LazyCell
, returning the underlying value.
Trait Implementations
sourceimpl<T: Clone> Clone for LazyCell<T>
impl<T: Clone> Clone for LazyCell<T>
sourcefn clone(&self) -> LazyCell<T>
fn clone(&self) -> LazyCell<T>
Create a clone of this LazyCell
If self has not been initialized, returns an uninitialized LazyCell
otherwise returns a LazyCell
already initialized with a clone of the
contents of self.
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
Auto Trait Implementations
impl<T> !RefUnwindSafe for LazyCell<T>
impl<T> Send for LazyCell<T> where
T: Send,
impl<T> !Sync for LazyCell<T>
impl<T> Unpin for LazyCell<T> where
T: Unpin,
impl<T> UnwindSafe for LazyCell<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more