#lazy-evaluation #lazy-static #once-cell #async #static

async-lazy

A value which is initialized on the first access, with an async function

1 unstable release

0.1.0 May 18, 2023

#764 in Asynchronous

Download history 63/week @ 2024-09-01 20/week @ 2024-09-08 10/week @ 2024-09-15 36/week @ 2024-09-22 27/week @ 2024-09-29 296/week @ 2024-10-06 362/week @ 2024-10-13 362/week @ 2024-10-20 182/week @ 2024-10-27 38/week @ 2024-11-03 9/week @ 2024-11-10 204/week @ 2024-11-17 85/week @ 2024-11-24 50/week @ 2024-12-01 93/week @ 2024-12-08 50/week @ 2024-12-15

285 downloads per month
Used in 5 crates (4 directly)

MIT/Apache

25KB
354 lines

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);
}

Dependencies

~2–7.5MB
~49K SLoC