Struct async_once_cell::OnceCell
source · [−]pub struct OnceCell<T> { /* private fields */ }Expand description
A cell which can be written to only once.
This allows initialization using an async closure that borrows from its environment.
Unlike OnceFuture, the initialing closures do not require Send + 'static bounds.
use std::rc::Rc;
use std::sync::Arc;
use async_once_cell::OnceCell;
let non_send_value = Rc::new(4);
let shared = Arc::new(OnceCell::new());
let value : &i32 = shared.get_or_init(async {
*non_send_value
}).await;
assert_eq!(value, &4);
// A second init is not called
let second = shared.get_or_init(async {
unreachable!()
}).await;
assert_eq!(second, &4);
Implementations
sourceimpl<T> OnceCell<T>
impl<T> OnceCell<T>
sourcepub async fn get_or_init(&self, init: impl Future<Output = T>) -> &T
pub async fn get_or_init(&self, init: impl Future<Output = T>) -> &T
Gets the contents of the cell, initializing it with init if the cell was empty.
Many tasks may call get_or_init concurrently with different initializing futures, but
it is guaranteed that only one future will be executed as long as the resuting future is
polled to completion.
If init panics, the panic is propagated to the caller, and the cell remains uninitialized.
If the Future returned by this function is dropped prior to completion, the cell remains uninitialized (and another init futures may be selected for polling).
It is an error to reentrantly initialize the cell from init. The current implementation
deadlocks, but will recover if the offending task is dropped.
sourcepub async fn get_or_try_init<E>(
&self,
init: impl Future<Output = Result<T, E>>
) -> Result<&T, E>
pub async fn get_or_try_init<E>(
&self,
init: impl Future<Output = Result<T, E>>
) -> Result<&T, E>
Gets the contents of the cell, initializing it with init if the cell was empty. If the
cell was empty and init failed, an error is returned.
If init panics, the panic is propagated to the caller, and the cell remains
uninitialized.
If the Future returned by this function is dropped prior to completion, the cell remains uninitialized.
It is an error to reentrantly initialize the cell from init. The current implementation
deadlocks, but will recover if the offending task is dropped.
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets the reference to the underlying value.
Returns None if the cell is empty or being initialized. This method never blocks.
sourcepub fn take(&mut self) -> Option<T>
pub fn take(&mut self) -> Option<T>
Takes the value out of this OnceCell, moving it back to an uninitialized state.
sourcepub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
Consumes the OnceCell, returning the wrapped value. Returns None if the cell was empty.
Trait Implementations
impl<T: RefUnwindSafe + UnwindSafe> RefUnwindSafe for OnceCell<T>
impl<T: Send> Send for OnceCell<T>
impl<T: Sync + Send> Sync for OnceCell<T>
impl<T> Unpin for OnceCell<T>
impl<T: UnwindSafe> UnwindSafe for OnceCell<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
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