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>
impl<T, Fut, F> Lazy<T, Fut, F>
sourcepub fn new(init: F) -> Lazy<T, Fut, F>
pub fn new(init: F) -> Lazy<T, Fut, F>
Creates a new empty Lazy instance with the given initializing
async function.
sourcepub const fn const_new(init: F) -> Lazy<T, Fut, F>
Available on crate feature parking_lot only.
pub const fn const_new(init: F) -> Lazy<T, Fut, F>
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);
}sourcepub fn get(&self) -> Option<&T>
pub fn get(&self) -> Option<&T>
Gets a reference to the result of this lazy value if it was initialized,
otherwise returns None.
sourcepub fn get_mut(&mut self) -> Option<&mut T>
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§impl<T, Fut, F> Lazy<T, Fut, F>where
Fut: Future<Output = T> + Unpin,
F: FnOnce() -> Fut,
impl<T, Fut, F> Lazy<T, Fut, F>where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,
sourcepub async fn force(&self) -> impl Future<Output = &T>
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§impl<T, Fut, F> Lazy<T, Fut, F>where
Fut: Future<Output = T>,
F: FnOnce() -> Fut,
impl<T, Fut, F> Lazy<T, Fut, F>where Fut: Future<Output = T>, F: FnOnce() -> Fut,
sourcepub async fn force_pin(
self: Pin<&Lazy<T, Fut, F>>
) -> impl Future<Output = Pin<&T>>
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.
sourcepub async fn force_pin_mut(
self: Pin<&mut Lazy<T, Fut, F>>
) -> impl Future<Output = Pin<&mut T>>
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<'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.
impl<'a, T, Fut, F> IntoFuture for &'a Lazy<T, Fut, F>where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,
nightly only.§type IntoFuture = impl Future<Output = <&'a Lazy<T, Fut, F> as IntoFuture>::Output>
type IntoFuture = impl Future<Output = <&'a Lazy<T, Fut, F> as IntoFuture>::Output>
source§fn into_future(self) -> <&'a Lazy<T, Fut, F> as IntoFuture>::IntoFuture
fn into_future(self) -> <&'a Lazy<T, Fut, F> as IntoFuture>::IntoFuture
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.
impl<'a, T, Fut, F> IntoFuture for &'a mut Lazy<T, Fut, F>where Fut: Future<Output = T> + Unpin, F: FnOnce() -> Fut,
nightly only.§type IntoFuture = impl Future<Output = <&'a mut Lazy<T, Fut, F> as IntoFuture>::Output>
type IntoFuture = impl Future<Output = <&'a mut Lazy<T, Fut, F> as IntoFuture>::Output>
source§fn into_future(self) -> <&'a mut Lazy<T, Fut, F> as IntoFuture>::IntoFuture
fn into_future(self) -> <&'a mut Lazy<T, Fut, F> as IntoFuture>::IntoFuture
impl<T, Fut, F> RefUnwindSafe for Lazy<T, F, Fut>where T: UnwindSafe + RefUnwindSafe, Fut: UnwindSafe, F: UnwindSafe,
impl<T, Fut, F> Send for Lazy<T, Fut, F>where T: Send, Fut: Send, F: Send,
impl<T, Fut, F> Sync for Lazy<T, Fut, F>where T: Send + Sync, Fut: Send, F: Send,
impl<T, Fut, F> Unpin for Lazy<T, Fut, F>where T: Unpin, Fut: Unpin,
impl<T, Fut, F> UnwindSafe for Lazy<T, Fut, F>where T: UnwindSafe, Fut: UnwindSafe, F: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request