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>

source

pub const fn with_init(init: I) -> Self

Creates a new OnceFuture with an initializing value

source

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.

source

pub const fn with_value(value: T) -> Self

Creates a new OnceFuture that is immediately ready

source

pub fn get(&self) -> Option<&T>

Gets the value without blocking or starting the initialization.

source

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.

source

pub fn into_inner(self) -> (Option<I>, Option<T>)

Gets the initializer or final value

source§

impl<T, F> OnceFuture<T, F>

source

pub const fn new() -> Self

Creates a new OnceFuture without an initializing value

The resulting Future must be produced by the closure passed to get_or_init_with

source§

impl<F> OnceFuture<F::Output, F>where F: Future + Send + 'static,

source

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,

source

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.

source

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.

source

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§

source§

impl<T: Debug, F: Debug, I: Debug> Debug for OnceFuture<T, F, I>

source§

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

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

impl<T: RefUnwindSafe + UnwindSafe, F, I: RefUnwindSafe> RefUnwindSafe for OnceFuture<T, F, I>

source§

impl<T: Send, F: Send, I: Send> Send for OnceFuture<T, F, I>

source§

impl<T: Sync + Send, F: Send, I: Send> Sync for OnceFuture<T, F, I>

source§

impl<T, F, I> Unpin for OnceFuture<T, F, I>

source§

impl<T: UnwindSafe, F, I: UnwindSafe> UnwindSafe for OnceFuture<T, F, I>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.