Struct 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§

Source§

impl<T> DoubleCheckedCell<T>

Source

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);
Source

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"));
Source

pub async fn get_or_init<Fut>(&self, init: Fut) -> &T
where 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);
Source

pub async fn get_or_try_init<Fut, E>(&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));
Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Default for DoubleCheckedCell<T>

Source§

fn default() -> DoubleCheckedCell<T>

Returns the “default value” for a type. Read more
Source§

impl<T> From<T> for DoubleCheckedCell<T>

Source§

fn from(t: T) -> DoubleCheckedCell<T>

Converts to this type from the input type.
Source§

impl<T> RefUnwindSafe for DoubleCheckedCell<T>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.