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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use alloc::boxed::Box;
use core::{future::Future, pin::Pin, time::Duration};
use futures_util::FutureExt as _;
pub use tokio::time::Interval;
use crate::Intervalable;
impl Intervalable for Interval {
fn interval(dur: Duration) -> Self {
tokio::time::interval(tokio::time::Duration::from_micros(dur.as_micros() as u64))
}
fn wait<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
self.reset();
Box::pin(self.tick().map(|_| ()))
}
#[cfg(feature = "std")]
fn wait_for_std<'a>(
&'a mut self,
) -> Pin<Box<dyn Future<Output = Option<std::time::Instant>> + Send + 'a>> {
self.reset();
Box::pin(self.tick().map(|x| Some(x.into_std())))
}
}
#[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_some());
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);
}
}
#[cfg(all(feature = "std", feature = "stream"))]
#[tokio::test]
async fn test_intervalable_repeat_stream() {
use alloc::{vec, vec::Vec};
use futures_util::StreamExt as _;
let st = crate::intervalable_repeat_stream(
0,
<Interval as Intervalable>::interval(Duration::from_millis(100)),
);
#[cfg(feature = "std")]
let now = std::time::Instant::now();
assert_eq!(st.take(3).collect::<Vec<_>>().await, vec![0, 0, 0]);
#[cfg(feature = "std")]
{
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 300 && elapsed_dur.as_millis() <= 310);
}
}
#[cfg(all(feature = "std", feature = "stream"))]
#[tokio::test]
async fn test_intervalable_repeat_with_stream() {
use alloc::{vec, vec::Vec};
use futures_util::StreamExt as _;
let st = crate::intervalable_repeat_with_stream(
|| 0,
<Interval as Intervalable>::interval(Duration::from_millis(100)),
);
#[cfg(feature = "std")]
let now = std::time::Instant::now();
assert_eq!(st.take(3).collect::<Vec<_>>().await, vec![0, 0, 0]);
#[cfg(feature = "std")]
{
let elapsed_dur = now.elapsed();
assert!(elapsed_dur.as_millis() >= 300 && elapsed_dur.as_millis() <= 310);
}
}
}