Struct mitochondria::OnceCell [] [src]

pub struct OnceCell<T>(_);

A mutable memory location that can be set only once.

This is useful to lazily build some value or to cache an expensive computation without the overhead of RefCell.

Usage

use mitochondria::OnceCell;

let c = OnceCell::new();

assert_eq!(c.as_ref(), None);

let value = c.init_once(|| "ribosome");

assert_eq!(value, &"ribosome");
assert_eq!(c.as_ref(), Some(&"ribosome"));

let value_again = c.init_once(|| "nucleolus");

assert_eq!(value_again, &"ribosome");Run

Reentrancy

To ensure that the cell is initialized only once in all its lifetime, if try_init_once or init_once are called reentranly from the f argument they take, the result of that reentrant call will be used and the return value of their caller f will be ignored.

use mitochondria::OnceCell;

let x = OnceCell::new();
let value = x.init_once(|| {
    x.init_once(|| "ribosome");
    "nucleolus"
});

assert_eq!(value, &"ribosome");
assert_eq!(x.as_ref(), Some(&"ribosome"));

let y = OnceCell::new();
let value = y.try_init_once::<(), _>(|| {
    let _ = y.try_init_once::<(), _>(|| Ok("ribosome"));
    Ok("nucleolus")
});

assert_eq!(value, Ok(&"ribosome"));
assert_eq!(y.as_ref(), Some(&"ribosome"));

let z = OnceCell::new();
let value = z.try_init_once::<(), _>(|| {
    z.init_once(|| "ribosome");
    Err(())
});
assert_eq!(value, Ok(&"ribosome"));
assert_eq!(z.as_ref(), Some(&"ribosome"));Run

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 Err(error) if f was called and returned an error, or Ok(&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.