1个不稳定版本
0.1.0 | 2020年4月15日 |
---|
#1178 在 异步
每月67次下载
10KB
182 行
raii-counter-futures
是DarrenTsung的raii-counter库的分支,允许您异步等待计数达到零。
示例
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;
lib.rs
:
raii-counter
Rust类型RAII Counter(计数持有实例的数量,在Drop
时减少计数),使用Arc<AtomicUsize>
实现。
用于跟踪句柄的持有者数量、跟踪正在进行的交易数量等。
示例
use raii_counter_futures::Counter;
let counter = Counter::new();
assert_eq!(counter.count(), 1);
let weak = counter.downgrade();
assert_eq!(weak.count(), 0);
{
let _counter1 = weak.spawn_upgrade();
assert_eq!(weak.count(), 1);
let _counter2 = weak.spawn_upgrade();
assert_eq!(weak.count(), 2);
}
assert_eq!(weak.count(), 0);
依赖项
~500KB