Struct double_checked_cell::DoubleCheckedCell
source · 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
sourceimpl<T> DoubleCheckedCell<T>
impl<T> DoubleCheckedCell<T>
sourcepub fn new() -> DoubleCheckedCell<T>
pub fn new() -> DoubleCheckedCell<T>
Creates a new uninitialized DoubleCheckedCell
.
Examples
use double_checked_cell::DoubleCheckedCell;
let cell = DoubleCheckedCell::<u32>::new();
assert_eq!(cell.get(), None);
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
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"));
sourcepub fn get_or_init<F>(&self, init: F) -> &Twhere
F: FnOnce() -> T,
pub fn get_or_init<F>(&self, init: F) -> &Twhere
F: FnOnce() -> T,
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);
sourcepub fn get_or_try_init<F, E>(&self, init: F) -> Result<&T, E>where
F: FnOnce() -> Result<T, E>,
pub fn get_or_try_init<F, E>(&self, init: F) -> Result<&T, E>where
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 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));
sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
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
sourceimpl<T: Debug> Debug for DoubleCheckedCell<T>
impl<T: Debug> Debug for DoubleCheckedCell<T>
sourceimpl<T> Default for DoubleCheckedCell<T>
impl<T> Default for DoubleCheckedCell<T>
sourcefn default() -> DoubleCheckedCell<T>
fn default() -> DoubleCheckedCell<T>
Returns the “default value” for a type. Read more
sourceimpl<T> From<T> for DoubleCheckedCell<T>
impl<T> From<T> for DoubleCheckedCell<T>
sourcefn from(t: T) -> DoubleCheckedCell<T>
fn from(t: T) -> DoubleCheckedCell<T>
Converts to this type from the input type.
impl<T> RefUnwindSafe for DoubleCheckedCell<T>
impl<T: Send + Sync> Sync for DoubleCheckedCell<T>
Auto Trait Implementations
impl<T> Send for DoubleCheckedCell<T>where
T: Send,
impl<T> Unpin for DoubleCheckedCell<T>where
T: Unpin,
impl<T> UnwindSafe for DoubleCheckedCell<T>where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more