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
use core::{future::Future, task::{Poll, Context}, time::Duration, pin::Pin};
use std::time::Instant;
use async_io::Timer;

#[derive(Copy, Clone, Default)]
pub struct Runtime {
    _private: (),
}

impl Runtime {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }
}

impl super::Runtime for Runtime {
    type Sleep = Timer;
    type Instant = Instant;
    fn create_sleep(&self, timeout: Duration) -> Self::Sleep {
        Timer::after(timeout)
    }
    fn now(&self) -> Self::Instant {
        Instant::now()
    }
}

impl super::Instant for Instant {
    fn duration_since(&self, earlier: &Self) -> Duration {
        self.duration_since(*earlier)
    }
}

struct PendingFuture;
impl Future for PendingFuture {
    type Output = ();
    fn poll(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Self::Output> {
        Poll::Pending
    }
}

impl super::Sleep for Timer {
    fn poll_sleep(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
        match self.poll(cx) {
            Poll::Ready(_) => Poll::Ready(()),
            Poll::Pending => Poll::Pending,
        }
    }
    fn reset(mut self: Pin<&mut Self>, timeout: Duration) {
        self.set_after(timeout);
    }
}