async_shared_timeout/runtime/
async_io.rs

1use async_io::Timer;
2use core::{
3    future::Future,
4    pin::Pin,
5    task::{Context, Poll},
6    time::Duration,
7};
8use std::time::Instant;
9
10/// async-io runtime implementation
11#[derive(Copy, Clone, Default)]
12#[cfg_attr(docsrs, doc(cfg(feature = "async-io")))]
13pub struct Runtime {
14    _private: (),
15}
16
17impl Runtime {
18    /// Create a new async-io runtime object
19    #[must_use]
20    pub fn new() -> Self {
21        Self::default()
22    }
23}
24
25impl super::Runtime for Runtime {
26    type Sleep = Timer;
27    type Instant = Instant;
28    fn create_sleep(&self, timeout: Duration) -> Self::Sleep {
29        Timer::after(timeout)
30    }
31    fn now(&self) -> Self::Instant {
32        Instant::now()
33    }
34}
35
36impl super::Instant for Instant {
37    fn duration_since(&self, earlier: &Self) -> Duration {
38        self.duration_since(*earlier)
39    }
40}
41
42impl super::Sleep for Timer {
43    fn poll_sleep(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
44        self.poll(cx).map(|_| ())
45    }
46    fn reset(mut self: Pin<&mut Self>, timeout: Duration) {
47        self.set_after(timeout);
48    }
49}