1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Traits that provide an abstract interface for time-related operations, modelled after [`std::time`].
//!
//! ```
//! fn add_durations<T: tcp::util::time::Duration>(x1: T, x2: T) -> T {
//!     x1 + x2
//! }
//!
//! use std::time::Duration;
//! assert_eq!(
//!     add_durations(Duration::from_secs(1), Duration::from_millis(2)),
//!     Duration::from_secs(1) + Duration::from_millis(2),
//! );
//! ```

// this is only used in doc comments.
#[allow(unused)]
use crate::Dependencies;

use std::fmt::Debug;

/// A trait for time instants that follow the API of [`std::time::Instant`]. This is useful for code
/// that should work with not just the real time, but also simulated time.
pub trait Instant:
    'static
    + Sized
    + Copy
    + Clone
    + Debug
    + std::ops::Add<Self::Duration, Output = Self>
    + std::ops::AddAssign<Self::Duration>
    + std::ops::Sub<Self::Duration, Output = Self>
    + std::ops::Sub<Self, Output = Self::Duration>
    + std::ops::SubAssign<Self::Duration>
    + std::cmp::PartialOrd
    + std::cmp::Ord
    + std::cmp::PartialEq
    + std::cmp::Eq
    + std::hash::Hash
{
    type Duration: Duration;

    /// See [`std::time::Instant::duration_since`].
    fn duration_since(&self, earlier: Self) -> Self::Duration;
    /// See [`std::time::Instant::saturating_duration_since`].
    fn saturating_duration_since(&self, earlier: Self) -> Self::Duration;
    /// See [`std::time::Instant::checked_duration_since`].
    fn checked_duration_since(&self, earlier: Self) -> Option<Self::Duration>;
    /// See [`std::time::Instant::checked_add`].
    fn checked_add(&self, duration: Self::Duration) -> Option<Self>;
    /// See [`std::time::Instant::checked_sub`].
    fn checked_sub(&self, duration: Self::Duration) -> Option<Self>;
}

/// A trait for time durations that follow the API of [`std::time::Duration`]. This is useful for
/// code that should work with not just the real time, but also simulated time.
pub trait Duration:
    'static
    + Sized
    + Copy
    + Clone
    + Debug
    + std::ops::Add<Output = Self>
    + std::ops::AddAssign
    + std::ops::Sub<Output = Self>
    + std::ops::SubAssign
    + std::ops::Mul<u32>
    + std::ops::MulAssign<u32>
    + std::ops::Div<u32>
    + std::ops::DivAssign<u32>
    + std::cmp::PartialOrd
    + std::cmp::Ord
    + std::cmp::PartialEq
    + std::cmp::Eq
    + std::hash::Hash
// it would also be nice to include the bound `u32: std::ops::Mul<Self>`, but we can't do that
// without a where clause, and the where clause would need to be duplicated everywhere that we use
// the trait
{
    /// See [`std::time::Duration::MAX`].
    const MAX: Self;
    /// See [`std::time::Duration::NANOSECOND`].
    const NANOSECOND: Self;
    /// See [`std::time::Duration::MICROSECOND`].
    const MICROSECOND: Self;
    /// See [`std::time::Duration::MILLISECOND`].
    const MILLISECOND: Self;
    /// See [`std::time::Duration::SECOND`].
    const SECOND: Self;
    /// See [`std::time::Duration::ZERO`].
    const ZERO: Self;

    /// See [`std::time::Duration::as_micros`].
    fn as_micros(&self) -> u128;
    /// See [`std::time::Duration::as_millis`].
    fn as_millis(&self) -> u128;
    /// See [`std::time::Duration::as_nanos`].
    fn as_nanos(&self) -> u128;
    /// See [`std::time::Duration::as_secs`].
    fn as_secs(&self) -> u64;
    /// See [`std::time::Duration::checked_add`].
    fn checked_add(self, rhs: Self) -> Option<Self>;
    /// See [`std::time::Duration::checked_div`].
    fn checked_div(self, rhs: u32) -> Option<Self>;
    /// See [`std::time::Duration::checked_mul`].
    fn checked_mul(self, rhs: u32) -> Option<Self>;
    /// See [`std::time::Duration::checked_sub`].
    fn checked_sub(self, rhs: Self) -> Option<Self>;
    /// See [`std::time::Duration::from_micros`].
    fn from_micros(micros: u64) -> Self;
    /// See [`std::time::Duration::from_millis`].
    fn from_millis(millis: u64) -> Self;
    /// See [`std::time::Duration::from_nanos`].
    fn from_nanos(nanos: u64) -> Self;
    /// See [`std::time::Duration::from_secs`].
    fn from_secs(secs: u64) -> Self;
    /// See [`std::time::Duration::is_zero`].
    fn is_zero(&self) -> bool;
    /// See [`std::time::Duration::saturating_add`].
    fn saturating_add(self, rhs: Self) -> Self;
    /// See [`std::time::Duration::saturating_mul`].
    fn saturating_mul(self, rhs: u32) -> Self;
    /// See [`std::time::Duration::saturating_sub`].
    fn saturating_sub(self, rhs: Self) -> Self;
    /// See [`std::time::Duration::subsec_micros`].
    fn subsec_micros(&self) -> u32;
    /// See [`std::time::Duration::subsec_millis`].
    fn subsec_millis(&self) -> u32;
    /// See [`std::time::Duration::subsec_nanos`].
    fn subsec_nanos(&self) -> u32;
}

/// Calls into [`std::time::Instant`] methods of the same name.
impl Instant for std::time::Instant {
    type Duration = std::time::Duration;

    #[inline]
    fn duration_since(&self, earlier: Self) -> Self::Duration {
        self.duration_since(earlier)
    }

    #[inline]
    fn saturating_duration_since(&self, earlier: Self) -> Self::Duration {
        self.saturating_duration_since(earlier)
    }

    #[inline]
    fn checked_duration_since(&self, earlier: Self) -> Option<Self::Duration> {
        self.checked_duration_since(earlier)
    }

    #[inline]
    fn checked_add(&self, duration: Self::Duration) -> Option<Self> {
        self.checked_add(duration)
    }

    #[inline]
    fn checked_sub(&self, duration: Self::Duration) -> Option<Self> {
        self.checked_sub(duration)
    }
}

/// Calls into [`std::time::Duration`] methods of the same name.
impl Duration for std::time::Duration {
    const MAX: Self = Self::MAX;
    // the std lib does contain these constants on nightly
    const NANOSECOND: Self = Self::from_nanos(1);
    const MICROSECOND: Self = Self::from_micros(1);
    const MILLISECOND: Self = Self::from_millis(1);
    const SECOND: Self = Self::from_secs(1);
    const ZERO: Self = Self::ZERO;

    #[inline]
    fn as_micros(&self) -> u128 {
        self.as_micros()
    }

    #[inline]
    fn as_millis(&self) -> u128 {
        self.as_millis()
    }

    #[inline]
    fn as_nanos(&self) -> u128 {
        self.as_nanos()
    }

    #[inline]
    fn as_secs(&self) -> u64 {
        self.as_secs()
    }

    #[inline]
    fn checked_add(self, rhs: Self) -> Option<Self> {
        self.checked_add(rhs)
    }

    #[inline]
    fn checked_div(self, rhs: u32) -> Option<Self> {
        self.checked_div(rhs)
    }

    #[inline]
    fn checked_mul(self, rhs: u32) -> Option<Self> {
        self.checked_mul(rhs)
    }

    #[inline]
    fn checked_sub(self, rhs: Self) -> Option<Self> {
        self.checked_sub(rhs)
    }

    #[inline]
    fn from_micros(micros: u64) -> Self {
        Self::from_micros(micros)
    }

    #[inline]
    fn from_millis(millis: u64) -> Self {
        Self::from_millis(millis)
    }

    #[inline]
    fn from_nanos(nanos: u64) -> Self {
        Self::from_nanos(nanos)
    }

    #[inline]
    fn from_secs(secs: u64) -> Self {
        Self::from_secs(secs)
    }

    #[inline]
    fn is_zero(&self) -> bool {
        self.is_zero()
    }

    #[inline]
    fn saturating_add(self, rhs: Self) -> Self {
        self.saturating_add(rhs)
    }

    #[inline]
    fn saturating_mul(self, rhs: u32) -> Self {
        self.saturating_mul(rhs)
    }

    #[inline]
    fn saturating_sub(self, rhs: Self) -> Self {
        self.saturating_sub(rhs)
    }

    #[inline]
    fn subsec_micros(&self) -> u32 {
        self.subsec_micros()
    }

    #[inline]
    fn subsec_millis(&self) -> u32 {
        self.subsec_millis()
    }

    #[inline]
    fn subsec_nanos(&self) -> u32 {
        self.subsec_nanos()
    }
}