1 unstable release
0.1.0 | May 18, 2023 |
---|
#743 in Asynchronous
1,048 downloads per month
Used in 5 crates
(4 directly)
25KB
354 lines
async-lazy
An async
version of once_cell::sync::Lazy
, std::sync::OnceLock
or lazy_static
. Uses tokio
's sychronization primitives.
Crate features
parking_lot
: Enables the corresponding feature intokio
, and ssallows constructingLazy
s inconst
contexts.nightly
: Uses nightly Rust features to implementIntoFuture
for references toLazy
s, 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::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);
}
Dependencies
~2–7.5MB
~49K SLoC