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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use alloc::boxed::Box;
use core::{future::Future, pin::Pin, time::Duration};
pub use async_timer::Interval;
use crate::Intervalable;
impl Intervalable for Interval {
fn interval(dur: Duration) -> Self {
Self::new(dur)
}
fn wait<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
Box::pin(self.wait())
}
#[cfg(feature = "std")]
fn wait_for_std<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Option<std::time::Instant>> + Send + 'a>> {
use futures_util::FutureExt as _;
Box::pin(self.wait().map(|_| None))
}
}
#[cfg(test)]
mod tests {
#[allow(unused_imports)]
use super::*;
#[cfg(feature = "std")]
#[tokio::test]
async fn test_impl() {
#[cfg(feature = "std")]
let now = std::time::Instant::now();
let mut interval = <Interval as Intervalable>::interval(Duration::from_millis(100));
interval.wait().await;
#[cfg(feature = "std")]
{
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 100 && elapsed_dur.as_millis() <= 105);
}
interval.wait().await;
#[cfg(feature = "std")]
{
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 200 && elapsed_dur.as_millis() <= 210);
}
#[cfg(feature = "std")]
{
assert!(interval.wait_for_std().await.is_none());
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 300 && elapsed_dur.as_millis() <= 315);
}
}
#[cfg(all(feature = "std", feature = "stream"))]
#[tokio::test]
async fn test_intervalable_iter_stream() {
use alloc::{vec, vec::Vec};
use futures_util::StreamExt as _;
let st = crate::intervalable_iter_stream(
0..=2,
<Interval as Intervalable>::interval(Duration::from_millis(100)),
);
#[cfg(feature = "std")]
let now = std::time::Instant::now();
assert_eq!(st.collect::<Vec<_>>().await, vec![0, 1, 2]);
#[cfg(feature = "std")]
{
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 300 && elapsed_dur.as_millis() <= 310);
}
}
}