5 个不稳定版本

0.3.0 2023 年 12 月 21 日
0.2.0 2020 年 5 月 25 日
0.1.2 2020 年 5 月 6 日
0.1.1 2020 年 5 月 6 日
0.1.0 2020 年 5 月 4 日

#369异步

Download history 179/week @ 2024-03-13 250/week @ 2024-03-20 226/week @ 2024-03-27 312/week @ 2024-04-03 388/week @ 2024-04-10 401/week @ 2024-04-17 315/week @ 2024-04-24 325/week @ 2024-05-01 225/week @ 2024-05-08 446/week @ 2024-05-15 629/week @ 2024-05-22 511/week @ 2024-05-29 785/week @ 2024-06-05 570/week @ 2024-06-12 470/week @ 2024-06-19 596/week @ 2024-06-26

2,506 每月下载量
2 crates 中使用

Apache-2.0

19KB
352

wait-for-me

异步 CountDownLatch

安装

crates.io 安装

[dependencies]
wait-for-me = "0.1"

示例

使用 smol

use wait_for_me::CountDownLatch;
use smol::Task;


#[smol_potat::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let latch = CountDownLatch::new(10);
    for _ in 0..10 {
        let latch1 = latch.clone();
        Task::spawn(async move {
            latch1.count_down().await;
        }).detach();
    }
    latch.wait().await;


    Ok(())
}

使用超时

use smol::{Task, Timer};
use std::time::Duration;
use wait_for_me::CountDownLatch;

#[smol_potat::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let latch = CountDownLatch::new(10);
    for _ in 0..10 {
        let latch1 = latch.clone();
        Task::spawn(async move {
            Timer::after(Duration::from_secs(3)).await;
            latch1.count_down().await;
        })
        .detach();
    }
    let result = latch.wait_for(Duration::from_secs(1)).await;

    assert_eq!(false, result);

    Ok(())
}

依赖

~1.5MB
~24K SLoC