Struct async_once_cell::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::OnceFuture;
let shared = Arc::new(OnceFuture::new());
let value : &i32 = shared.get_or_init_with(|| async {
4
}).await;
assert_eq!(value, &4);Implementations
sourceimpl<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.
sourceimpl<T, F> OnceFuture<T, F>
impl<T, F> OnceFuture<T, F>
sourceimpl<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.
sourceimpl<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.
Trait Implementations
impl<T: RefUnwindSafe + UnwindSafe, F, I: RefUnwindSafe> RefUnwindSafe for OnceFuture<T, F, I>
impl<T: Send, F: Send, I: Send> Send for OnceFuture<T, F, I>
impl<T: Sync + Send, F: Send, I: Send> Sync for OnceFuture<T, F, I>
impl<T, F, I> Unpin for OnceFuture<T, F, I>
impl<T: UnwindSafe, F, I: UnwindSafe> UnwindSafe for OnceFuture<T, F, I>
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