4 releases

0.2.0 May 25, 2020
0.1.2 May 6, 2020
0.1.1 May 6, 2020
0.1.0 May 4, 2020

#496 in Asynchronous

Download history 83/week @ 2021-08-11 86/week @ 2021-08-18 17/week @ 2021-08-25 41/week @ 2021-09-01 25/week @ 2021-09-08 54/week @ 2021-09-15 64/week @ 2021-09-22 55/week @ 2021-09-29 48/week @ 2021-10-06 54/week @ 2021-10-13 113/week @ 2021-10-20 74/week @ 2021-10-27 40/week @ 2021-11-03 83/week @ 2021-11-10 59/week @ 2021-11-17 60/week @ 2021-11-24

246 downloads per month
Used in see-you-later

Apache-2.0

16KB
217 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

~1MB
~18K SLoC