lazycell

Struct LazyCell

Source
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§

Source§

impl<T> LazyCell<T>

Source

pub fn new() -> LazyCell<T>

Creates a new, empty, LazyCell.

Source

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.

Source

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.

Source

pub fn filled(&self) -> bool

Test whether this cell has been previously filled.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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

Consumes this LazyCell, returning the underlying value.

Source§

impl<T: Copy> LazyCell<T>

Source

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

Returns a copy of the contents of the lazy cell.

This function will return Some if the cell has been previously initialized, and None if it has not yet been initialized.

Trait Implementations§

Source§

impl<T: Clone> Clone for LazyCell<T>

Source§

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 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for LazyCell<T>

Source§

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

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

impl<T> Default for LazyCell<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> !Freeze for LazyCell<T>

§

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§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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>,

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.