[−][src]Struct double_checked_cell_async::DoubleCheckedCell
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]
pub 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().await, None);
pub async fn get<'_, '_>(&'_ self) -> Option<&'_ T>
[src]
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"));
pub async fn get_or_init<'_, '_, Fut>(&'_ self, init: Fut) -> &'_ T where
Fut: Future<Output = T>,
[src]
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);
pub async fn get_or_try_init<'_, '_, Fut, E>(
&'_ self,
init: Fut
) -> Result<&'_ T, E> where
Fut: Future<Output = Result<T, E>>,
[src]
&'_ self,
init: Fut
) -> Result<&'_ T, E> where
Fut: Future<Output = 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));
pub 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: Debug> Debug for DoubleCheckedCell<T>
[src]
impl<T> Default for DoubleCheckedCell<T>
[src]
fn default() -> DoubleCheckedCell<T>
[src]
impl<T> From<T> for DoubleCheckedCell<T>
[src]
fn from(t: T) -> DoubleCheckedCell<T>
[src]
impl<T> RefUnwindSafe for DoubleCheckedCell<T>
[src]
impl<T: Send + Sync> Sync for DoubleCheckedCell<T>
[src]
Auto Trait Implementations
impl<T> Send for DoubleCheckedCell<T> where
T: Send,
T: Send,
impl<T> Unpin for DoubleCheckedCell<T> where
T: Unpin,
T: Unpin,
impl<T> UnwindSafe for DoubleCheckedCell<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<!> for T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,