Struct double_checked_cell::DoubleCheckedCell
[−]
[src]
pub struct DoubleCheckedCell<T> { /* fields omitted */ }
A thread-safe lazily initialized cell.
The cell is immutable once it is initialized. See the module-level documentation for more.
Methods
impl<T> DoubleCheckedCell<T>
[src]
fn new() -> DoubleCheckedCell<T>
[src]
Creates a new uninitialized DoubleCheckedCell
.
Examples
use double_checked_cell::DoubleCheckedCell; let cell = DoubleCheckedCell::<u32>::new(); assert_eq!(cell.get(), None);
fn get(&self) -> Option<&T>
[src]
Borrows the value if the cell is initialized.
Panics
Panics if the cell is poisoned.
Examples
use double_checked_cell::DoubleCheckedCell; let cell = DoubleCheckedCell::from("hello"); assert_eq!(cell.get(), Some(&"hello"));
fn get_or_init<F>(&self, init: F) -> &T where
F: FnOnce() -> T,
[src]
F: FnOnce() -> T,
Borrows the value if the cell is initialized or initializes it from a closure.
Panics
Panics if the cell is poisoned.
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);
fn get_or_try_init<F, E>(&self, init: F) -> Result<&T, E> where
F: FnOnce() -> Result<T, E>,
[src]
F: FnOnce() -> Result<T, E>,
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 if the cell is poisoned.
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));
fn into_inner(self) -> Option<T>
[src]
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
impl<T> Default for DoubleCheckedCell<T>
[src]
fn default() -> DoubleCheckedCell<T>
[src]
Returns the "default value" for a type. Read more
impl<T: Debug> Debug for DoubleCheckedCell<T>
[src]
impl<T> From<T> for DoubleCheckedCell<T>
[src]
fn from(t: T) -> DoubleCheckedCell<T>
[src]
Performs the conversion.