#async #lazy-static #lazily #declaring #macro #evaluated

过程宏 async_static

Rust中声明异步懒加载静态变量的宏

2个版本

0.1.3 2021年2月20日
0.1.2 2021年2月19日
0.1.1 2021年2月19日
0.1.0 2021年2月19日

#1559过程宏

MIT/Apache

8KB
76

Async Static

在Rust中声明异步懒加载静态变量的宏。


基本用法

依赖项

async_static = "0.1"
once_cell = "1"
# Only used in the current example
tokio = { version = "1", features = ["full"] }

src

use async_static::async_static;
use tokio::time::sleep;

async fn get_num() -> i32 {
    println!("hello world");
    sleep(Duration::from_millis(100)).await;
    123
}

async_static! {
    static ref FOO:i32 = get_num().await;
}

/// run print
/// ```
/// hello world
/// The result of the first call: 123
/// The result of the second call: 123
/// ```
#[tokio::test]
async fn test() {
    // The first call, print hello world
    let n = FOO.await;
    println!("The result of the first call: {}", n);

    // The second call, nothing print
    let n = FOO.await;
    println!("The result of the second call: {}", n);
}

许可证

根据您的选择,受以下任一许可证的约束:

贡献

除非您明确说明,否则您提交的任何有意纳入工作的贡献,根据Apache-2.0许可证定义,应按上述方式双许可,不附加任何额外条款或条件。

依赖项

~1.5MB
~35K SLoC