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
/// TODO: We could use async-std but this feature is available on unstable only.
/// https://docs.rs/async-std/latest/async_std/stream/fn.interval.html
use super::{Sleep, Timer};
use pin_project_lite::pin_project;

use std::{
    future::Future,
    pin::Pin,
    task::{Context, Poll},
    time::{Duration, Instant},
};

/// A Timer that uses the smol runtime.
#[derive(Clone, Debug)]
pub struct AsyncStdTimer;

impl Timer for AsyncStdTimer {
    fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> {
        Box::pin(SmolSleep {
            inner: smol::Timer::after(duration),
        })
    }

    fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> {
        Box::pin(SmolSleep {
            inner: smol::Timer::at(deadline),
        })
    }
}

struct SmolTimeout<T: Future> {
    inner: Pin<Box<smol_timeout::Timeout<T>>>,
}

impl<T> Future for SmolTimeout<T>
where
    T: Future,
{
    type Output = Result<T::Output, smol::io::ErrorKind>;

    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        self.inner
            .as_mut()
            .poll(context)
            .map(|res| res.ok_or(smol::io::ErrorKind::TimedOut))
    }
}

pin_project! {
    pub(crate) struct SmolSleep {
        #[pin]
        pub(crate) inner: smol::Timer,
    }
}

impl Future for SmolSleep {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        self.project().inner.poll(cx).map(|_| ())
    }
}
impl Sleep for SmolSleep {}