Skip to main content

agnostic_lite/
time.rs

1use core::time::Duration;
2
3mod delay;
4mod interval;
5mod sleep;
6mod timeout;
7
8pub use delay::*;
9pub use interval::*;
10pub use sleep::*;
11pub use timeout::*;
12
13macro_rules! instant_methods {
14  () => {
15    /// Returns an instant corresponding to "now".
16    #[must_use]
17    fn now() -> Self;
18
19    /// Returns the amount of time elapsed since this instant.
20    fn elapsed(&self) -> Duration;
21
22    /// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
23    /// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
24    /// otherwise.
25    fn checked_add(&self, duration: Duration) -> Option<Self>;
26
27    /// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
28    /// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
29    /// otherwise.
30    fn checked_sub(&self, duration: Duration) -> Option<Self>;
31
32    /// Returns the amount of time elapsed from another instant to this one,
33    /// or None if that instant is later than this one.
34    fn checked_duration_since(&self, earlier: Self) -> Option<Duration>;
35
36    /// Returns the amount of time elapsed from another instant to this one,
37    /// or zero duration if that instant is later than this one.
38    fn duration_since(&self, earlier: Self) -> Duration {
39      self.checked_duration_since(earlier).unwrap_or_default()
40    }
41    
42    /// Returns the amount of time elapsed from another instant to this one,
43    /// or zero duration if that instant is later than this one.
44    fn saturating_duration_since(&self, earlier: Self) -> Duration {
45      self.checked_duration_since(earlier).unwrap_or_default()
46    }  
47  };
48}
49
50
51/// A measurement of a monotonically nondecreasing clock.
52/// Opaque and useful only with [`Duration`].
53#[cfg(not(feature = "std"))]
54pub trait Instant:
55  Copy
56  + Clone
57  + PartialEq
58  + Eq
59  + PartialOrd
60  + Ord
61  + core::fmt::Debug
62  + core::hash::Hash
63  + core::ops::Add<Duration, Output = Self>
64  + core::ops::AddAssign<Duration>
65  + core::ops::Sub<Self>
66  + core::ops::Sub<Duration>
67  + core::ops::SubAssign<Duration>
68  + Send
69  + Sync
70  + Unpin
71  + 'static
72{
73  instant_methods!();
74}
75
76/// A measurement of a monotonically nondecreasing clock.
77/// Opaque and useful only with [`Duration`].
78#[cfg(feature = "std")]
79pub trait Instant:
80  Copy
81  + Clone
82  + PartialEq
83  + Eq
84  + PartialOrd
85  + Ord
86  + core::fmt::Debug
87  + core::hash::Hash
88  + core::ops::Add<Duration, Output = Self>
89  + core::ops::AddAssign<Duration>
90  + core::ops::Sub<Self>
91  + core::ops::Sub<Duration>
92  + core::ops::SubAssign<Duration>
93  + From<std::time::Instant>
94  + Into<std::time::Instant>
95  + Send
96  + Sync
97  + Unpin
98  + 'static
99{
100  instant_methods!();
101}
102
103#[cfg(feature = "std")]
104const _: () = {
105  use std::time::Instant as StdInstant;
106
107  impl Instant for StdInstant {
108    #[inline]
109    fn now() -> Self {
110      StdInstant::now()
111    }
112
113    #[inline]
114    fn elapsed(&self) -> Duration {
115      StdInstant::elapsed(self)
116    }
117
118    #[inline]
119    fn checked_add(&self, duration: Duration) -> Option<Self> {
120      StdInstant::checked_add(self, duration)
121    }
122
123    #[inline]
124    fn checked_sub(&self, duration: Duration) -> Option<Self> {
125      StdInstant::checked_sub(self, duration)
126    }
127
128    #[inline]
129    fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
130      StdInstant::checked_duration_since(self, earlier)
131    }
132  }
133};
134
135#[cfg(all(feature = "tokio", feature = "time", feature = "std"))]
136const _: () = {
137  use tokio::time::Instant as TokioInstant;
138
139  impl Instant for TokioInstant {
140    #[inline]
141    fn now() -> Self {
142      TokioInstant::now()
143    }
144
145    #[inline]
146    fn elapsed(&self) -> Duration {
147      TokioInstant::elapsed(self)
148    }
149
150    #[inline]
151    fn checked_add(&self, duration: Duration) -> Option<Self> {
152      TokioInstant::checked_add(self, duration)
153    }
154
155    #[inline]
156    fn checked_sub(&self, duration: Duration) -> Option<Self> {
157      TokioInstant::checked_sub(self, duration)
158    }
159
160    #[inline]
161    fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
162      TokioInstant::checked_duration_since(self, earlier)
163    }
164  }
165};