async-lazy

Crates.ioasync-lazy
lib.rsasync-lazy
version
sourcesrc
created_at2023-05-18 00:33:27.213607+00
updated_at2025-02-23 22:49:53.458056+00
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
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
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.

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 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::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