Struct gstuff::Constructible
source · [−]pub struct Constructible<T> { /* private fields */ }
use once_cell
Expand description
A cell that can be initialized, but only once.
Once initialized the cell remains immutable, allowing us to alias the value.
NB: There is a similar abstraction at https://github.com/matklad/once_cell.
(I’ve only just discovered it, some months after implementing the Constructible
myself).
Implementations
sourceimpl<T> Constructible<T>
impl<T> Constructible<T>
sourcepub fn initialize<'a>(&'a self, v: Box<T>) -> Result<&'a T, String>
pub fn initialize<'a>(&'a self, v: Box<T>) -> Result<&'a T, String>
Provides the cell with the value.
The value is effectively pinned in the cell, it won’t be moved.
Returns an error if the cell is already initialized.
sourcepub fn pin<'a>(&'a self, v: T) -> Result<&'a T, String>
pub fn pin<'a>(&'a self, v: T) -> Result<&'a T, String>
Provides the cell with the value.
The value is moved into a Box
and pinned there.
Returns an error if the cell is already initialized.
sourcepub fn or<'a, F>(&'a self, default: &'a F) -> &'a T where
F: Fn() -> &'a T,
T: 'a,
pub fn or<'a, F>(&'a self, default: &'a F) -> &'a T where
F: Fn() -> &'a T,
T: 'a,
Get a reference to the value.
If the value is not (yet) available then returns the reference provided by default
.
sourcepub fn copy_or(&self, default: T) -> T where
T: Copy,
pub fn copy_or(&self, default: T) -> T where
T: Copy,
Returns a copy of the value or the default
if the value is not yet available.
sourcepub fn clone_or(&self, default: T) -> T where
T: Clone,
pub fn clone_or(&self, default: T) -> T where
T: Clone,
Returns a clone of the value or the default
if the value is not yet available.
sourcepub fn ok_or<'a, E>(&'a self, error: E) -> Result<&'a T, E>
pub fn ok_or<'a, E>(&'a self, error: E) -> Result<&'a T, E>
Returns a reference to the value or the given error
if the value is not yet available.
Trait Implementations
sourceimpl<T> Debug for Constructible<T> where
T: Debug,
impl<T> Debug for Constructible<T> where
T: Debug,
Debug formatting similar to Option<&T>
.
sourceimpl<T> Default for Constructible<T>
impl<T> Default for Constructible<T>
Creates a cell without a value.
Use pin
or initialize
to provide the value later.
sourcefn default() -> Constructible<T>
fn default() -> Constructible<T>
Returns the “default value” for a type. Read more
sourceimpl<T> Display for Constructible<T> where
T: Display,
impl<T> Display for Constructible<T> where
T: Display,
Prints the value or “-” if it is not yet available.
sourceimpl<T> Drop for Constructible<T>
impl<T> Drop for Constructible<T>
sourceimpl<T> From<Option<T>> for Constructible<T>
impl<T> From<Option<T>> for Constructible<T>
Translate an Option
into a Constructible
cell.
If the Option
has a value then the cell with be initialized with it.
If the Option
is empty then the cell will be empty as well, and waiting for delayed initialization.
sourcefn from(v: Option<T>) -> Constructible<T>
fn from(v: Option<T>) -> Constructible<T>
Performs the conversion.
sourceimpl<T> From<T> for Constructible<T>
impl<T> From<T> for Constructible<T>
Pre-initialize the cell with the given value.
sourcefn from(v: T) -> Constructible<T>
fn from(v: T) -> Constructible<T>
Performs the conversion.
sourceimpl<T, E> FromStr for Constructible<T> where
T: FromStr<Err = E>,
impl<T, E> FromStr for Constructible<T> where
T: FromStr<Err = E>,
Allows to parse
directly into the cell.
sourceimpl<'a, T> IntoIterator for &'a Constructible<T>
impl<'a, T> IntoIterator for &'a Constructible<T>
Makes it possible to access the value with a for
loop.
for value in &constructible {println! ("{}", value)}
Auto Trait Implementations
impl<T> RefUnwindSafe for Constructible<T> where
T: RefUnwindSafe,
impl<T> Send for Constructible<T> where
T: Send,
impl<T> Sync for Constructible<T> where
T: Sync,
impl<T> Unpin for Constructible<T> where
T: Unpin,
impl<T> UnwindSafe for Constructible<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