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§
Source§impl<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().await, None);
Sourcepub async fn get(&self) -> Option<&T>
pub async 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().await, Some(&"hello"));
Sourcepub async fn get_or_init<Fut>(&self, init: Fut) -> &Twhere
Fut: Future<Output = T>,
pub async fn get_or_init<Fut>(&self, init: Fut) -> &Twhere
Fut: Future<Output = 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;
use futures::future::ready;
let cell = DoubleCheckedCell::new();
// Initialize the cell.
let value = cell.get_or_init(async { 1 + 2 }).await;
assert_eq!(*value, 3);
// The cell is now immutable.
let value = cell.get_or_init(async { 42 }).await;
assert_eq!(*value, 3);
Sourcepub async fn get_or_try_init<Fut, E>(&self, init: Fut) -> Result<&T, E>
pub async fn get_or_try_init<Fut, E>(&self, init: Fut) -> 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;
use futures::future::ready;
let cell = DoubleCheckedCell::new();
let result = cell.get_or_try_init(async { "not an integer".parse() }).await;
assert!(result.is_err());
let result = cell.get_or_try_init(async { "42".parse() }).await;
assert_eq!(result, Ok(&42));
let result = cell.get_or_try_init(async { "irrelevant".parse() }).await;
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§
Source§impl<T: Debug> Debug for DoubleCheckedCell<T>
impl<T: Debug> Debug for DoubleCheckedCell<T>
Source§impl<T> Default for DoubleCheckedCell<T>
impl<T> Default for DoubleCheckedCell<T>
Source§fn default() -> DoubleCheckedCell<T>
fn default() -> DoubleCheckedCell<T>
Returns the “default value” for a type. Read more
Source§impl<T> From<T> for DoubleCheckedCell<T>
impl<T> From<T> for DoubleCheckedCell<T>
Source§fn 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> !Freeze for DoubleCheckedCell<T>
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§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more