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(value: Option<T>) -> Self

Creates a new OnceCell that may already be initialized.

Example

use mitochondria::OnceCell;

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

fn try_init_once<F>(&self, f: F) -> Option<&T> where F: FnOnce() -> Result<T, ()>

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(None);

assert_eq!(c.try_init_once(|| Err(())), None);

let greeting: &str = c.try_init_once(|| {
    Ok("Hello ribosome!".to_owned())
}).unwrap();
use mitochondria::OnceCell;

let c = OnceCell::new(Some("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!");

fn borrow(&self) -> Option<&T>

Borrows the contained value, if initialized.

Examples

use mitochondria::OnceCell;

let c = OnceCell::new(None);

assert!(c.borrow().is_none());

let greeting = c.init_once(|| "Hello nucleus!".to_owned());
assert_eq!(c.borrow(), Some(greeting));

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(None);

let greeting: &str = c.init_once(|| "Hello ribosome!".to_owned());
use mitochondria::OnceCell;

let c = OnceCell::new(Some("Hello reticulum!".to_owned()));

// Calls to `init_once` on initialized cells are ignored.
assert_eq!(c.init_once(|| "Goodbye!".to_owned()),
           "Hello reticulum!");