Struct mitochondria::OnceCell [] [src]

pub struct OnceCell<T>(_);

A mutable memory location that can be set only once.

Methods

impl<T> OnceCell<T>
[src]

Creates a new OnceCell.

Examples

use mitochondria::OnceCell;

let c = OnceCell::<String>::new();Run

Creates a new OnceCell initialised with value.

Examples

use mitochondria::OnceCell;

let c = OnceCell::new_with_value(Some("Hello vesicle!".to_owned()));Run

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

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

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]

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]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<T: Debug> Debug for OnceCell<T>
[src]

Formats the value using the given formatter.

impl<T> Default for OnceCell<T>
[src]

Returns the "default value" for a type. Read more

impl<T> From<T> for OnceCell<T>
[src]

Performs the conversion.