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 #[must_use]
17 fn now() -> Self;
18
19 fn elapsed(&self) -> Duration;
21
22 fn checked_add(&self, duration: Duration) -> Option<Self>;
26
27 fn checked_sub(&self, duration: Duration) -> Option<Self>;
31
32 fn checked_duration_since(&self, earlier: Self) -> Option<Duration>;
35
36 fn duration_since(&self, earlier: Self) -> Duration {
39 self.checked_duration_since(earlier).unwrap_or_default()
40 }
41
42 fn saturating_duration_since(&self, earlier: Self) -> Duration {
45 self.checked_duration_since(earlier).unwrap_or_default()
46 }
47 };
48}
49
50
51#[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#[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};