1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use futures_core::task::Waker;
use futures_util::task::{self, ArcWake};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
#[derive(Debug)]
pub struct AwokenCount {
inner: Arc<WakerInner>,
}
impl AwokenCount {
pub fn get(&self) -> usize {
self.inner.count.load(Ordering::SeqCst)
}
}
impl PartialEq<usize> for AwokenCount {
fn eq(&self, other: &usize) -> bool {
self.get() == *other
}
}
#[derive(Debug)]
struct WakerInner {
count: AtomicUsize,
}
impl ArcWake for WakerInner {
fn wake_by_ref(arc_self: &Arc<Self>) {
let _ = arc_self.count.fetch_add(1, Ordering::SeqCst);
}
}
pub fn new_count_waker() -> (Waker, AwokenCount) {
let inner = Arc::new(WakerInner { count: AtomicUsize::new(0) });
(task::waker(inner.clone()), AwokenCount { inner })
}