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
use std::time::{Instant, Duration};
pub struct Until<I> {
inner: I,
until: Instant,
}
pub trait UntilExt<T: Iterator> {
fn until(self, until: Instant) -> Until<Self>
where
Self: Sized,
{
Until::new(self, until)
}
fn do_for(self, until: Duration) -> Until<Self>
where
Self: Sized,
{
Until::new(self, Instant::now() + until)
}
}
impl<I> Until<I> {
pub fn new(iter: I, until: Instant) -> Self {
Self { inner: iter, until }
}
}
impl<I: Iterator> Iterator for Until<I> {
type Item = I::Item;
fn next(&mut self) -> Option<Self::Item> {
if Instant::now() > self.until {
None
} else {
self.inner.next()
}
}
}
impl<I: Iterator> UntilExt<I> for I {}