Struct async_once_cell::unpin::OnceFuture
source · pub struct OnceFuture<T, F = Pin<Box<dyn Future<Output = T> + Send>>, I = Infallible> { /* private fields */ }
Expand description
A Future which is executed exactly once, producing an output accessible without locking.
This is primarily used as a building block for Lazy and ConstLazy, but can also be used on its own similar to OnceCell.
use std::sync::Arc;
use async_once_cell::unpin::OnceFuture;
let shared = Arc::new(OnceFuture::new());
let value : &i32 = shared.get_or_init_with(|| async {
4
}).await;
assert_eq!(value, &4);
Implementations§
source§impl<T, F, I> OnceFuture<T, F, I>
impl<T, F, I> OnceFuture<T, F, I>
sourcepub const fn with_no_init() -> Self
pub const fn with_no_init() -> Self
Creates a new OnceFuture without an initializing value
The resulting Future must be produced by the closure passed to Self::get_or_init_with. This function is identical to Self::new but is more likely to need type hints.
sourcepub const fn with_value(value: T) -> Self
pub const fn with_value(value: T) -> Self
Creates a new OnceFuture that is immediately ready
sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets the value without blocking or starting the initialization.
sourcepub fn get_mut(&mut self) -> (Option<&mut I>, Option<&mut T>)
pub fn get_mut(&mut self) -> (Option<&mut I>, Option<&mut T>)
Get mutable access to the initializer or final value.
This requires mutable access to self, so rust’s aliasing rules prevent any concurrent access and allow violating the usual rules for accessing this cell.
sourcepub fn into_inner(self) -> (Option<I>, Option<T>)
pub fn into_inner(self) -> (Option<I>, Option<T>)
Gets the initializer or final value
source§impl<T, F> OnceFuture<T, F>
impl<T, F> OnceFuture<T, F>
source§impl<F> OnceFuture<F::Output, F>where
F: Future + Send + 'static,
impl<F> OnceFuture<F::Output, F>where F: Future + Send + 'static,
sourcepub fn from_future(future: F) -> Self
pub fn from_future(future: F) -> Self
Creates a new OnceFuture directly from a Future.
The gen_future
or into_future
closures will never be called.
source§impl<T, F, I> OnceFuture<T, F, I>where
F: Future<Output = T> + Send + 'static,
impl<T, F, I> OnceFuture<T, F, I>where F: Future<Output = T> + Send + 'static,
sourcepub async fn get_or_init_with(&self, gen_future: impl FnOnce() -> F) -> &T
pub async fn get_or_init_with(&self, gen_future: impl FnOnce() -> F) -> &T
Create and run the future until it produces a result, then return a reference to that result.
This is a convenience wrapper around OnceFuture::get_or_populate_with for use when the initializer value is not used or not present.
sourcepub async fn get_or_populate_with(
&self,
into_future: impl FnOnce(Option<I>) -> F
) -> &T
pub async fn get_or_populate_with( &self, into_future: impl FnOnce(Option<I>) -> F ) -> &T
Create and run the future until it produces a result, then return a reference to that result.
Only one into_future
closure will be called per OnceFuture
instance, and only if the
future was not already set by from_future
.
sourcepub fn poll_populate(
&self,
cx: &mut Context<'_>,
into_future: impl FnOnce(Option<I>) -> F
) -> Poll<&T>
pub fn poll_populate( &self, cx: &mut Context<'_>, into_future: impl FnOnce(Option<I>) -> F ) -> Poll<&T>
Create and run the future until it produces a result, then return a reference to that result.
Only one into_future
closure will be called per OnceFuture
instance, and only if the
future was not already set by from_future
.