async-lazy

Crates.ioasync-lazy
lib.rsasync-lazy
version0.1.0
sourcesrc
created_at2023-05-18 00:33:27.213607
updated_at2023-05-18 00:33:27.213607
descriptionA value which is initialized on the first access, with an async function.
homepagehttps://github.com/Jules-Bertholet/async-lazy
repositoryhttps://github.com/Jules-Bertholet/async-lazy
max_upload_size
id867320
size42,352
Jules Bertholet (Jules-Bertholet)

documentation

README

async-lazy

Build Status Crates.io API reference License

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 in tokio, and ssallows constructing Lazys in const contexts.
  • nightly: Uses nightly Rust features to implement IntoFuture for references to Lazys, obviating the need to call force().

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);
}
Commit count: 6

cargo fmt