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
use std::{
    future::Future,
    task::Poll,
    time::{Duration, Instant},
};

/// A timer future that returns after a specified period of time.
#[must_use = "futures do nothing unless you `.await` or poll them"]
pub struct Timer {
    id: Option<usize>,
    when: Instant,
}

impl Timer {
    /// Make a new timer future that returns Ready after a specified duration.
    pub fn after(dur: Duration) -> Timer {
        Timer::at(Instant::now() + dur)
    }

    /// Make a new timer future that returns Ready at or after a specified instant in time.
    pub fn at(when: Instant) -> Timer {
        Timer { id: None, when }
    }
}

impl Drop for Timer {
    fn drop(&mut self) {
        if let Some(id) = self.id.take() {
            crate::reactor::Reactor::get().remove_timer(self.when, id);
        }
    }
}

impl Future for Timer {
    type Output = Instant;

    fn poll(
        mut self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        if Instant::now() >= self.when {
            if let Some(id) = self.id.take() {
                crate::reactor::Reactor::get().remove_timer(self.when, id);
            }
            Poll::Ready(self.when)
        } else {
            if self.id.is_none() {
                self.id = Some(crate::reactor::Reactor::get().insert_timer(self.when, cx.waker()));
            }
            Poll::Pending
        }
    }
}