Expand description
§async-lazy
An async version of once_cell::sync::Lazy, std::sync::OnceLock or lazy_static. Uses tokio’s sychronization primitives.
This crate offers an API similar to the Lazy type from async-once-cell crate. The difference is that this crate’s Lazy accepts an FnOnce() -> impl Future instead of a Future, which makes use in statics easier.
§Crate features
nightly: Uses nightly Rust features to implementIntoFuturefor references toLazys, obviating the need to callforce().
§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);
}Structs§
- Lazy
- 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.