5 unstable releases

0.3.0 Dec 21, 2023
0.2.0 May 25, 2020
0.1.2 May 6, 2020
0.1.1 May 6, 2020
0.1.0 May 4, 2020

#1344 in Asynchronous

Download history 284/week @ 2024-07-19 307/week @ 2024-07-26 528/week @ 2024-08-02 286/week @ 2024-08-09 327/week @ 2024-08-16 394/week @ 2024-08-23 694/week @ 2024-08-30 480/week @ 2024-09-06 368/week @ 2024-09-13 330/week @ 2024-09-20 205/week @ 2024-09-27 173/week @ 2024-10-04 104/week @ 2024-10-11 135/week @ 2024-10-18 39/week @ 2024-10-25 85/week @ 2024-11-01

401 downloads per month
Used in 2 crates

Apache-2.0

19KB
352 lines

wait-for-me

Async CountDownLatch

Install

Install from crates.io

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

Example

with 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(())
}

with timeout

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

Dependencies

~1.5MB
~24K SLoC