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
//! Methods for expressing sequences of times

use std::time::{Duration, Instant};

use instant_iter::IntoInstantIter;

/// Times occuring at fixed intervals
pub struct Every {
    duration: Duration,
    start: Instant,
}

impl Every {
    #[allow(missing_docs)]
    pub fn new(duration: Duration) -> Self {
        Every {
            duration,
            start: Instant::now(),
        }
    }
}

impl Iterator for Every {
    type Item = Instant;
    fn next(&mut self) -> Option<Instant> {
        self.start += self.duration;
        Some(self.start)
    }
}

impl IntoInstantIter for Every {
    type IterType = Self;
    fn into_instant_iter(self) -> Self::IterType { self }
}

/// Single time occuring after a fixed duration
pub struct After {
    duration: Duration,
    now: Instant,
}

impl After {
    #[allow(missing_docs)]
    pub fn new(duration: Duration) -> Self {
        After {
            duration,
            now: Instant::now(),
        }
    }
}

impl IntoInstantIter for After {
    type IterType = ::std::vec::IntoIter<Instant>;
    fn into_instant_iter(self) -> Self::IterType {
        vec![self.now + self.duration].into_iter()
    }
}