async_shared_timeout/runtime/
mod.rs

1//! Traits needed for runtime-agnostic time measurement and sleeping
2use core::{
3    pin::Pin,
4    task::{Context, Poll},
5    time::Duration,
6};
7
8#[cfg(feature = "tokio")]
9mod tokio;
10#[cfg(feature = "tokio")]
11pub use self::tokio::Runtime as Tokio;
12
13#[cfg(feature = "async-io")]
14mod async_io;
15#[cfg(feature = "async-io")]
16pub use self::async_io::Runtime as AsyncIo;
17
18/// A sleep future
19pub trait Sleep {
20    /// Set the future to time out in `timeout` from now
21    fn reset(self: Pin<&mut Self>, timeout: Duration);
22    /// Wait for the timeout to expire
23    fn poll_sleep(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()>;
24}
25
26/// Async runtime
27pub trait Runtime {
28    /// Sleep future type associated with this runtime
29    type Sleep: Sleep;
30    /// Instant type associated with this runtime
31    type Instant: Instant;
32    /// Create a new sleep future with given timeout
33    fn create_sleep(&self, timeout: Duration) -> Self::Sleep;
34    /// Get current time
35    fn now(&self) -> Self::Instant;
36}
37
38/// An instant representing a point in time.
39pub trait Instant {
40    /// Duration since an earlier instant.
41    fn duration_since(&self, earlier: &Self) -> Duration;
42}