Struct mitochondria::OnceCell
[−]
[src]
pub struct OnceCell<T>(_);
A mutable memory location that can be set only once.
Methods
impl<T> OnceCell<T>
[src]
fn new() -> Self
fn new_with_value(value: T) -> Self
Creates a new OnceCell
initialised with value
.
Examples
use mitochondria::OnceCell; let c = OnceCell::new_with_value(Some("Hello vesicle!".to_owned()));Run
fn try_init_once<E, F>(&self, f: F) -> Result<&T, E> where F: FnOnce() -> Result<T, E>
Calls a function to try to initialize this cell.
If the cell was already-initialized, the function is not called.
Otherwise, if the function returns Ok(value)
, the cell is
initialized with value
.
This method returns None
if the cell could not be initialized,
or Some(&value)
otherwise.
Examples
use mitochondria::OnceCell; let c = OnceCell::new(); assert_eq!(c.try_init_once(|| Err(())), Err(())); let greeting = c.try_init_once::<(), _>(|| { Ok("Hello ribosome!".to_owned()) }).unwrap();Run
use mitochondria::OnceCell; let c = OnceCell::new_with_value("Hello reticulum!".to_owned()); // Calls to `try_init_once` on initialized cells are ignored. assert_eq!( c.try_init_once::<(), _>(|| Ok("Goodbye!".to_owned())).unwrap(), "Hello reticulum!");Run
fn as_ref(&self) -> Option<&T>
Returns None
if the cell is not initialised, or else returns a
reference to the value wrapped in Some
.
Examples
use mitochondria::OnceCell; let c = OnceCell::new(); assert!(c.as_ref().is_none()); let greeting = c.init_once(|| "Hello nucleus!".to_owned()); assert_eq!(c.as_ref(), Some(greeting));Run
fn as_mut(&mut self) -> Option<&mut T>
Returns None
if the cell is not initialised, or else returns a
mutable reference to the value wrapped in Some
.
This call borrows OnceCell
mutably (at compile-time) which
guarantees that we possess the only reference.
Examples
use mitochondria::OnceCell; let mut c = OnceCell::new(); assert!(c.as_mut().is_none()); c.init_once(|| "Nucleo".to_owned()); *c.as_mut().unwrap() += "lus!"; assert_eq!(c.as_ref().unwrap(), "Nucleolus!");Run
impl<T> OnceCell<T>
[src]
fn init_once<F>(&self, f: F) -> &T where F: FnOnce() -> T
Calls a function to initialize this cell and borrows its value.
If the cell was already-initialized, the function is not called and the returned value is the one that was already there.
Examples
use mitochondria::OnceCell; let c = OnceCell::new(); let greeting: &str = c.init_once(|| "Hello ribosome!".to_owned());Run
use mitochondria::OnceCell; let c = OnceCell::new_with_value("Hello reticulum!".to_owned()); // Calls to `init_once` on initialized cells are ignored. assert_eq!( c.init_once(|| "Goodbye!".to_owned()), "Hello reticulum!");Run
Trait Implementations
impl<T> Send for OnceCell<T> where T: Send
[src]
impl<T: Clone> Clone for OnceCell<T>
[src]
fn clone(&self) -> Self
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: Debug> Debug for OnceCell<T>
[src]
impl<T> Default for OnceCell<T>
[src]
impl<T> From<T> for OnceCell<T>
[src]
fn from(value: T) -> Self
Performs the conversion.