pub struct DoubleCheckedCell<T> { /* private fields */ }
Expand description

A thread-safe lazily initialized cell.

The cell is immutable once it is initialized. See the module-level documentation for more.

Implementations

Creates a new uninitialized DoubleCheckedCell.

Examples
use double_checked_cell::DoubleCheckedCell;

let cell = DoubleCheckedCell::<u32>::new();
assert_eq!(cell.get(), None);

Borrows the value if the cell is initialized.

Examples
use double_checked_cell::DoubleCheckedCell;

let cell = DoubleCheckedCell::from("hello");
assert_eq!(cell.get(), Some(&"hello"));

Borrows the value if the cell is initialized or initializes it from a closure.

Panics

Panics or deadlocks when trying to access the cell from the initilization closure.

Examples
use double_checked_cell::DoubleCheckedCell;

let cell = DoubleCheckedCell::new();

// Initialize the cell.
let value = cell.get_or_init(|| 1 + 2);
assert_eq!(*value, 3);

// The cell is now immutable.
let value = cell.get_or_init(|| 42);
assert_eq!(*value, 3);

Borrows the value if the cell is initialized or attempts to initialize it from a closure.

Errors

Forwards any error from the closure if the cell is not yet initialized. The cell then remains uninitialized.

Panics

Panics or deadlocks when trying to access the cell from the initilization closure.

Examples
use double_checked_cell::DoubleCheckedCell;

let cell = DoubleCheckedCell::new();

let result = cell.get_or_try_init(|| "not an integer".parse());
assert!(result.is_err());

let result = cell.get_or_try_init(|| "42".parse());
assert_eq!(result, Ok(&42));

let result = cell.get_or_try_init(|| "irrelevant".parse());
assert_eq!(result, Ok(&42));

Unwraps the value.

Examples
use double_checked_cell::DoubleCheckedCell;

let cell = DoubleCheckedCell::from(42);
let contents = cell.into_inner();
assert_eq!(contents, Some(42));

Trait Implementations

Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
Converts to this type from the input type.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.