futures_test/task/
wake_counter.rsuse 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 })
}