Struct async_once_cell::unpin::Lazy

source ·
pub struct Lazy<T, F = Pin<Box<dyn Future<Output = T> + Send>>> { /* private fields */ }
Expand description

A value which is initialized on the first access.

See ConstLazy if you need to initialize in a const context.

use std::sync::Arc;
use async_once_cell::unpin::Lazy;

let shared = Arc::new(Lazy::new(async {
    4
}));

let value : &i32 = shared.get().await;
assert_eq!(value, &4);

You can also call await on a reference:

use async_once_cell::unpin::Lazy;
struct Foo {
    value: Lazy<i32>,
}

let foo = Foo {
    value : Lazy::new(Box::pin(async { 4 })),
};

assert_eq!((&foo.value).await, &4);

Implementations§

source§

impl<T, F> Lazy<T, F>where F: Future<Output = T> + Send + 'static,

source

pub fn new(future: F) -> Self

Creates a new lazy value with the given initializing future.

source

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

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

This is equivalent to the Future impl on &Lazy, but is explicit and may be simpler to call. This will panic if the initializing closure panics or has panicked.

source§

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

source

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

Creates an already-initialized lazy value.

source

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

Gets the value without blocking or starting the initialization.

source

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

Gets the value without blocking or starting the initialization.

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_value(self) -> Option<T>

Gets the value if it was set.

Trait Implementations§

source§

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

source§

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

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

impl<'a, T, F> Future for &'a Lazy<T, F>where F: Future<Output = T> + Send + 'static,

§

type Output = &'a T

The type of value produced on completion.
source§

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<&'a T>

Attempt to resolve the future to a final value, registering the current task for wakeup if the value is not yet available. Read more

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

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.