Struct Lazy

Source
pub struct Lazy<T, Fut = Pin<Box<dyn Future<Output = T> + Send>>, F = fn() -> Fut> { /* private fields */ }
Expand description

A value that is initialized on the first access. The initialization procedure is allowed to be asycnhronous, so access to the value requires an await.

§Example

use std::time::Duration;
use async_lazy::Lazy;

async fn some_computation() -> u32 {
    tokio::time::sleep(Duration::from_secs(1)).await;
    1 + 1
}

#[tokio::main]
async fn main() {
    let lazy : Lazy<u32> = Lazy::new(|| Box::pin(async { some_computation().await }));

    let result = tokio::spawn(async move {
        *lazy.force().await
    }).await.unwrap();

    assert_eq!(result, 2);
}

Implementations§

Source§

impl<T, Fut, F> Lazy<T, Fut, F>

Source

pub const fn new(init: F) -> Self

Creates a new Lazy instance with the given initializing async function.

Equivalent to Lazy::new, except that it can be used in static variables.

§Example
use std::time::Duration;
use async_lazy::Lazy;

async fn some_computation() -> u32 {
    tokio::time::sleep(Duration::from_secs(1)).await;
    1 + 1
}

static LAZY: Lazy<u32> = Lazy::new(|| Box::pin(async { some_computation().await }));

#[tokio::main]
async fn main() {
    let result = tokio::spawn(async {
        *LAZY.force().await
    }).await.unwrap();

    assert_eq!(result, 2);
}
Source

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

Gets a reference to the result of this lazy value if it was initialized, otherwise returns None.

Source

pub fn get_mut(&mut self) -> Option<&mut T>

Gets a mutable reference to the result of this lazy value if it was initialized, otherwise returns None.

Source

pub fn get_pin(self: Pin<&Self>) -> Option<Pin<&T>>

Gets a pinned reference to the result of this lazy value if it was initialized, otherwise returns None.

Source

pub fn get_pin_mut(self: Pin<&mut Self>) -> Option<Pin<&mut T>>

Gets a pinned mutable reference to the result of this lazy value if it was initialized, otherwise returns None.

Source§

impl<T, Fut, F> Lazy<T, Fut, F>
where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,

Source

pub async fn force(&self) -> &T

Forces the evaluation of this lazy value and returns a reference to the result.

If the caller of force is cancelled, the state of initialization is preserved; the next call to force starts polling the initializing future again where the last left off.

§Panics

If the initialization function panics, the Lazy is poisoned and all subsequent calls to this function will panic.

Source

pub async fn force_mut(&mut self) -> &mut T

Forces the evaluation of this lazy value and returns a mutable reference to the result.

§Panics

If the initialization function panics, the Lazy is poisoned and all subsequent calls to this function will panic.

Source§

impl<T, Fut, F> Lazy<T, Fut, F>
where Fut: Future<Output = T>, F: FnOnce() -> Fut,

Source

pub async fn force_pin(self: Pin<&Self>) -> Pin<&T>

Forces the evaluation of this lazy value and returns a reference to the result.

§Panics

If the initialization function panics, the Lazy is poisoned and all subsequent calls to this function will panic.

Source

pub async fn force_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T>

Forces the evaluation of this lazy value and returns a mutable reference to the result.

§Panics

If the initialization function panics, the Lazy is poisoned and all subsequent calls to this function will panic.

Trait Implementations§

Source§

impl<T: Debug, Fut, F> Debug for Lazy<T, Fut, F>

Source§

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

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

impl<T, Fut, F> Drop for Lazy<T, Fut, F>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a, T, Fut, F> IntoFuture for &'a Lazy<T, Fut, F>
where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,

Available on crate feature nightly only.
Source§

type Output = &'a T

The output that the future will produce on completion.
Source§

type IntoFuture = impl Future<Output = <&'a Lazy<T, Fut, F> as IntoFuture>::Output>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
Source§

impl<'a, T, Fut, F> IntoFuture for &'a mut Lazy<T, Fut, F>
where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,

Available on crate feature nightly only.
Source§

type Output = &'a mut T

The output that the future will produce on completion.
Source§

type IntoFuture = impl Future<Output = <&'a mut Lazy<T, Fut, F> as IntoFuture>::Output>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
Source§

impl<'a, T, Fut, F> IntoFuture for Pin<&'a Lazy<T, Fut, F>>
where Fut: Future<Output = T>, F: FnOnce() -> Fut,

Available on crate feature nightly only.
Source§

type Output = Pin<&'a T>

The output that the future will produce on completion.
Source§

type IntoFuture = impl Future<Output = <Pin<&'a Lazy<T, Fut, F>> as IntoFuture>::Output>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
Source§

impl<'a, T, Fut, F> IntoFuture for Pin<&'a mut Lazy<T, Fut, F>>
where Fut: Future<Output = T>, F: FnOnce() -> Fut,

Available on crate feature nightly only.
Source§

type Output = Pin<&'a mut T>

The output that the future will produce on completion.
Source§

type IntoFuture = impl Future<Output = <Pin<&'a mut Lazy<T, Fut, F>> as IntoFuture>::Output>

Which kind of future are we turning this into?
Source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more
Source§

impl<T: UnwindSafe + RefUnwindSafe, Fut: UnwindSafe, F: UnwindSafe> RefUnwindSafe for Lazy<T, F, Fut>

Source§

impl<T: Send, Fut: Send, F: Send> Send for Lazy<T, Fut, F>

Source§

impl<T: Send + Sync, Fut: Send, F: Send> Sync for Lazy<T, Fut, F>

Source§

impl<T: Unpin, Fut: Unpin, F> Unpin for Lazy<T, Fut, F>

Source§

impl<T: UnwindSafe, Fut: UnwindSafe, F: UnwindSafe> UnwindSafe for Lazy<T, Fut, F>

Auto Trait Implementations§

§

impl<T, Fut = Pin<Box<dyn Future<Output = T> + Send>>, F = fn() -> Fut> !Freeze for Lazy<T, Fut, F>

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<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.