1#[allow(unused)]
17use crate::Dependencies;
18
19use std::fmt::Debug;
20
21pub trait Instant:
24    'static
25    + Sized
26    + Copy
27    + Clone
28    + Debug
29    + std::ops::Add<Self::Duration, Output = Self>
30    + std::ops::AddAssign<Self::Duration>
31    + std::ops::Sub<Self::Duration, Output = Self>
32    + std::ops::Sub<Self, Output = Self::Duration>
33    + std::ops::SubAssign<Self::Duration>
34    + std::cmp::PartialOrd
35    + std::cmp::Ord
36    + std::cmp::PartialEq
37    + std::cmp::Eq
38    + std::hash::Hash
39{
40    type Duration: Duration;
41
42    fn duration_since(&self, earlier: Self) -> Self::Duration;
44    fn saturating_duration_since(&self, earlier: Self) -> Self::Duration;
46    fn checked_duration_since(&self, earlier: Self) -> Option<Self::Duration>;
48    fn checked_add(&self, duration: Self::Duration) -> Option<Self>;
50    fn checked_sub(&self, duration: Self::Duration) -> Option<Self>;
52}
53
54pub trait Duration:
57    'static
58    + Sized
59    + Copy
60    + Clone
61    + Debug
62    + std::ops::Add<Output = Self>
63    + std::ops::AddAssign
64    + std::ops::Sub<Output = Self>
65    + std::ops::SubAssign
66    + std::ops::Mul<u32>
67    + std::ops::MulAssign<u32>
68    + std::ops::Div<u32>
69    + std::ops::DivAssign<u32>
70    + std::cmp::PartialOrd
71    + std::cmp::Ord
72    + std::cmp::PartialEq
73    + std::cmp::Eq
74    + std::hash::Hash
75{
79    const MAX: Self;
81    const NANOSECOND: Self;
83    const MICROSECOND: Self;
85    const MILLISECOND: Self;
87    const SECOND: Self;
89    const ZERO: Self;
91
92    fn as_micros(&self) -> u128;
94    fn as_millis(&self) -> u128;
96    fn as_nanos(&self) -> u128;
98    fn as_secs(&self) -> u64;
100    fn checked_add(self, rhs: Self) -> Option<Self>;
102    fn checked_div(self, rhs: u32) -> Option<Self>;
104    fn checked_mul(self, rhs: u32) -> Option<Self>;
106    fn checked_sub(self, rhs: Self) -> Option<Self>;
108    fn from_micros(micros: u64) -> Self;
110    fn from_millis(millis: u64) -> Self;
112    fn from_nanos(nanos: u64) -> Self;
114    fn from_secs(secs: u64) -> Self;
116    fn is_zero(&self) -> bool;
118    fn saturating_add(self, rhs: Self) -> Self;
120    fn saturating_mul(self, rhs: u32) -> Self;
122    fn saturating_sub(self, rhs: Self) -> Self;
124    fn subsec_micros(&self) -> u32;
126    fn subsec_millis(&self) -> u32;
128    fn subsec_nanos(&self) -> u32;
130}
131
132impl Instant for std::time::Instant {
134    type Duration = std::time::Duration;
135
136    #[inline]
137    fn duration_since(&self, earlier: Self) -> Self::Duration {
138        self.duration_since(earlier)
139    }
140
141    #[inline]
142    fn saturating_duration_since(&self, earlier: Self) -> Self::Duration {
143        self.saturating_duration_since(earlier)
144    }
145
146    #[inline]
147    fn checked_duration_since(&self, earlier: Self) -> Option<Self::Duration> {
148        self.checked_duration_since(earlier)
149    }
150
151    #[inline]
152    fn checked_add(&self, duration: Self::Duration) -> Option<Self> {
153        self.checked_add(duration)
154    }
155
156    #[inline]
157    fn checked_sub(&self, duration: Self::Duration) -> Option<Self> {
158        self.checked_sub(duration)
159    }
160}
161
162impl Duration for std::time::Duration {
164    const MAX: Self = Self::MAX;
165    const NANOSECOND: Self = Self::from_nanos(1);
167    const MICROSECOND: Self = Self::from_micros(1);
168    const MILLISECOND: Self = Self::from_millis(1);
169    const SECOND: Self = Self::from_secs(1);
170    const ZERO: Self = Self::ZERO;
171
172    #[inline]
173    fn as_micros(&self) -> u128 {
174        self.as_micros()
175    }
176
177    #[inline]
178    fn as_millis(&self) -> u128 {
179        self.as_millis()
180    }
181
182    #[inline]
183    fn as_nanos(&self) -> u128 {
184        self.as_nanos()
185    }
186
187    #[inline]
188    fn as_secs(&self) -> u64 {
189        self.as_secs()
190    }
191
192    #[inline]
193    fn checked_add(self, rhs: Self) -> Option<Self> {
194        self.checked_add(rhs)
195    }
196
197    #[inline]
198    fn checked_div(self, rhs: u32) -> Option<Self> {
199        self.checked_div(rhs)
200    }
201
202    #[inline]
203    fn checked_mul(self, rhs: u32) -> Option<Self> {
204        self.checked_mul(rhs)
205    }
206
207    #[inline]
208    fn checked_sub(self, rhs: Self) -> Option<Self> {
209        self.checked_sub(rhs)
210    }
211
212    #[inline]
213    fn from_micros(micros: u64) -> Self {
214        Self::from_micros(micros)
215    }
216
217    #[inline]
218    fn from_millis(millis: u64) -> Self {
219        Self::from_millis(millis)
220    }
221
222    #[inline]
223    fn from_nanos(nanos: u64) -> Self {
224        Self::from_nanos(nanos)
225    }
226
227    #[inline]
228    fn from_secs(secs: u64) -> Self {
229        Self::from_secs(secs)
230    }
231
232    #[inline]
233    fn is_zero(&self) -> bool {
234        self.is_zero()
235    }
236
237    #[inline]
238    fn saturating_add(self, rhs: Self) -> Self {
239        self.saturating_add(rhs)
240    }
241
242    #[inline]
243    fn saturating_mul(self, rhs: u32) -> Self {
244        self.saturating_mul(rhs)
245    }
246
247    #[inline]
248    fn saturating_sub(self, rhs: Self) -> Self {
249        self.saturating_sub(rhs)
250    }
251
252    #[inline]
253    fn subsec_micros(&self) -> u32 {
254        self.subsec_micros()
255    }
256
257    #[inline]
258    fn subsec_millis(&self) -> u32 {
259        self.subsec_millis()
260    }
261
262    #[inline]
263    fn subsec_nanos(&self) -> u32 {
264        self.subsec_nanos()
265    }
266}