raii-counter-futures

Crates.ioraii-counter-futures
lib.rsraii-counter-futures
version0.1.0
sourcesrc
created_at2020-04-15 23:24:27.6645
updated_at2020-04-15 23:24:27.6645
descriptionRAII Counter that allows you to wait for a count of zero asynchronously
homepagehttps://github.com/nate-onesignal/raii-counter-futures
repositoryhttps://github.com/DarrenTsung/raii-counter-futures
max_upload_size
id230643
size10,838
Lily Mara (lily-mara)

documentation

README

raii-counter-futures

Fork of DarrenTsung's raii-counter library that allows you to wait asynchronously for the count to reach zero.

Demo

use raii_counter_futures::WeakCounter;
use std::time::Duration;

let weak = Arc::new(WeakCounter::new());
assert_eq!(counter.count(), 0);

let weak2 = Arc::clone(&weak);
tokio::spawn(async move {
    let counter1 = weak2.spawn_upgrade();
    let counter2 = weak2.spawn_upgrade();

    tokio::time::delay_for(Duration::from_secs(2)).await;
});

// Give the weak counters a chance to spawn, in a real workload you would not
// want to start waiting immediately after your async tasks spawn.
tokio::time::delay_for(Duration::from_millis(500)).await;

// This will not complete until the 2 second delay in the async task has finished.
counter.wait_for_empty().await;
Commit count: 0

cargo fmt