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
//!Interval module

use core::future::Future;
use core::{task, time, mem};
use core::pin::Pin;

use crate::oneshot::Oneshot;
use crate::oneshot::Timer as PlatformTimer;

///Periodic Timer
///
///On each completition user receives new instance that can be polled once again
///
///## Note
///
///Returned Interval is already armed, so it don't need initial poll to start over.
///
///## Usage
///
///```rust, no_run
///#![feature(async_await)]
///
///async fn job() {
///}
///
///async fn do_a_while() {
///    let mut times: u8 = 0;
///    let mut interval = async_timer::Interval::platform_new(core::time::Duration::from_secs(1));
///
///    while times < 5 {
///        job().await;
///        interval = interval.await;
///        times += 1;
///    }
///}
///```
#[must_use = "Interval does nothing unless polled"]
pub enum Interval<T=PlatformTimer> {
    #[doc(hidden)]
    Ongoing(T, time::Duration),
    #[doc(hidden)]
    Stopped,
}

impl Interval {
    #[inline(always)]
    ///Creates new instance using platform timer
    pub fn platform_new(interval: time::Duration) -> Self {
        Interval::<PlatformTimer>::new(interval)
    }
}

impl<T: Oneshot> Interval<T> {
    ///Creates new instance with specified timer type.
    pub fn new(interval: time::Duration) -> Self {
        Interval::Ongoing(T::new(interval), interval)
    }
}

impl<T: Oneshot> Future for Interval<T> {
    type Output = Self;

    fn poll(mut self: Pin<&mut Self>, ctx: &mut task::Context) -> task::Poll<Self::Output> {
        let mut state = Interval::Stopped;
        mem::swap(self.as_mut().get_mut(), &mut state);
        match state {
            Interval::Ongoing(mut timer, interval) => match Future::poll(Pin::new(&mut timer), ctx) {
                task::Poll::Ready(()) => {
                    timer.restart(&interval, ctx.waker());
                    task::Poll::Ready(Interval::Ongoing(timer, interval))
                },
                task::Poll::Pending => {
                    *self = Interval::Ongoing(timer, interval);
                    task::Poll::Pending
                },
            },
            Interval::Stopped => task::Poll::Pending
        }
    }
}