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
use std::time::{Duration, Instant};
pub trait IntoInstantIter {
type IterType: Iterator<Item = Instant>;
fn into_instant_iter(self) -> Self::IterType;
}
impl IntoInstantIter for Vec<Instant> {
type IterType = ::std::vec::IntoIter<Instant>;
fn into_instant_iter(self) -> Self::IterType { self.into_iter() }
}
impl IntoInstantIter for Vec<Duration> {
type IterType = DurationToInstantIter<::std::vec::IntoIter<Duration>>;
fn into_instant_iter(self) -> Self::IterType {
DurationToInstantIter::new(self.into_iter())
}
}
impl IntoInstantIter for Instant {
type IterType = ::std::vec::IntoIter<Instant>;
fn into_instant_iter(self) -> Self::IterType { vec![self].into_iter() }
}
impl IntoInstantIter for Duration {
type IterType = ::std::vec::IntoIter<Instant>;
fn into_instant_iter(self) -> Self::IterType {
vec![Instant::now() + self].into_iter()
}
}
pub struct DurationToInstantIter<I: Iterator<Item = Duration>> {
prev_iter: I,
now: Instant,
}
impl<I: Iterator<Item = Duration>> DurationToInstantIter<I> {
fn new(prev_iter: I) -> Self {
DurationToInstantIter {
prev_iter,
now: Instant::now(),
}
}
}
impl<I: Iterator<Item = Duration>> Iterator for DurationToInstantIter<I> {
type Item = Instant;
fn next(&mut self) -> Option<Instant> {
self.prev_iter.next().map(|duration| self.now + duration)
}
}