Struct clip_qdrant::Lazy

source ·
pub struct Lazy<T, Fut = Pin<Box<dyn Future<Output = T> + Send, Global>>, 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 fn new(init: F) -> Lazy<T, Fut, F>

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

source

pub const fn const_new(init: F) -> Lazy<T, Fut, F>

Available on crate feature parking_lot only.

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::const_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<&Lazy<T, Fut, F>>) -> 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 Lazy<T, Fut, F>>) -> 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) -> impl Future<Output = &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) -> impl Future<Output = &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<&Lazy<T, Fut, F>> ) -> impl Future<Output = 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 Lazy<T, Fut, F>> ) -> impl Future<Output = 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, Fut, F> Debug for Lazy<T, Fut, F>where T: Debug,

source§

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

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

type Output = &'a T

The output that the future will produce on completion.
§

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) -> <&'a Lazy<T, Fut, F> as IntoFuture>::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.
§

type Output = &'a mut T

The output that the future will produce on completion.
§

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) -> <&'a mut Lazy<T, Fut, F> as IntoFuture>::IntoFuture

Creates a future from a value. Read more
source§

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

source§

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

source§

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

source§

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

source§

impl<T, Fut, F> UnwindSafe for Lazy<T, Fut, F>where T: UnwindSafe, Fut: UnwindSafe, F: 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,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere 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> IntoRequest<T> for T

source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
source§

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

§

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 Twhere U: TryFrom<T>,

§

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

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more