fast_time 0.1.26

An efficient low-precision timestamp source suitable for high-frequency querying
Documentation
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::time::Duration;

use crate::Clock;

/// An instant returned by the fast-time clock.
///
/// Represents a specific point in time captured from a [`Clock`]. Use this to measure
/// elapsed time or to compare with other instants from the same clock.
///
/// You may convert the instant into an [`std::time::Instant`] for arithmetic operations
/// and interoperability with standard library code.
///
/// # Examples
///
/// Basic elapsed time measurement:
///
/// ```rust
/// use std::time::Duration;
///
/// use fast_time::Clock;
///
/// let mut clock = Clock::new();
/// let start = clock.now();
///
/// // Simulate some work
/// std::thread::sleep(Duration::from_millis(10));
///
/// let elapsed = start.elapsed(&mut clock);
/// // Note: fast_time prioritizes efficiency over precision
/// assert!(elapsed <= Duration::from_secs(1)); // Generous upper bound
/// ```
///
/// Comparing instants:
///
/// ```rust
/// use fast_time::Clock;
///
/// let mut clock = Clock::new();
/// let instant1 = clock.now();
/// let instant2 = clock.now();
///
/// // instant2 should be later than instant1
/// assert!(instant2 >= instant1);
///
/// let duration = instant2.saturating_duration_since(instant1);
/// println!("Time between captures: {:?}", duration);
/// ```
///
/// Converting to standard library types:
///
/// ```rust
/// use std::time::Instant as StdInstant;
///
/// use fast_time::Clock;
///
/// let mut clock = Clock::new();
/// let fast_instant = clock.now();
///
/// // Convert to std::time::Instant
/// let std_instant: StdInstant = fast_instant.into();
///
/// // Convert back
/// let converted_back: fast_time::Instant = std_instant.into();
/// ```
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Instant {
    inner: std::time::Instant,
}

impl Instant {
    /// Calculates the elapsed time since this instant using the provided clock.
    ///
    /// This method captures a new timestamp from the clock and calculates the duration
    /// that has passed since this instant was created.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let start = clock.now();
    ///
    /// // Simulate some work
    /// std::thread::sleep(Duration::from_millis(5));
    ///
    /// let elapsed = start.elapsed(&mut clock);
    /// // Note: fast_time prioritizes efficiency over precision
    /// assert!(elapsed <= Duration::from_secs(1)); // Generous upper bound
    /// ```
    #[must_use]
    #[inline]
    pub fn elapsed(&self, clock: &mut Clock) -> Duration {
        clock.now().saturating_duration_since(*self)
    }

    /// Calculates the duration since an earlier instant.
    ///
    /// Returns the amount of time that passed between the `earlier` instant and this instant.
    /// If the earlier instant is actually later than this instant, returns a duration of zero
    /// (saturating behavior).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let start = clock.now();
    ///
    /// // Simulate some work
    /// std::thread::sleep(Duration::from_millis(8));
    ///
    /// let end = clock.now();
    /// let duration = end.saturating_duration_since(start);
    /// // Note: fast_time prioritizes efficiency over precision
    /// assert!(duration <= Duration::from_secs(1)); // Generous upper bound
    /// ```
    ///
    /// Saturating behavior with reversed order:
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let instant1 = clock.now();
    /// let instant2 = clock.now();
    ///
    /// // instant1 is earlier, so this returns zero
    /// let duration = instant1.saturating_duration_since(instant2);
    /// assert_eq!(duration, Duration::ZERO);
    /// ```
    #[must_use]
    #[inline]
    pub fn saturating_duration_since(&self, earlier: Self) -> Duration {
        self.inner.saturating_duration_since(earlier.inner)
    }

    /// Calculates the duration since an earlier instant.
    ///
    /// This is an alias for [`saturating_duration_since`](Self::saturating_duration_since)
    /// to maintain compatibility with `std::time::Instant`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let start = clock.now();
    /// let end = clock.now();
    ///
    /// let duration = end.duration_since(start);
    /// ```
    #[must_use]
    pub fn duration_since(&self, earlier: Self) -> Duration {
        self.saturating_duration_since(earlier)
    }

    /// Calculates the duration since an earlier instant, returning `None` if the earlier
    /// instant is actually later than this instant.
    ///
    /// Unlike [`saturating_duration_since`](Self::saturating_duration_since), this method
    /// returns `None` instead of zero when the earlier instant is later.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let start = clock.now();
    ///
    /// // Simulate some work to ensure time passes
    /// std::thread::sleep(Duration::from_millis(10));
    ///
    /// let end = clock.now();
    ///
    /// // Normal case: end should be later than start
    /// match end.checked_duration_since(start) {
    ///     Some(duration) => println!("Elapsed: {:?}", duration),
    ///     None => println!("Time went backwards"),
    /// }
    ///
    /// // Test with the same instant - should return Some(Duration::ZERO)
    /// let same_instant = clock.now();
    /// assert_eq!(
    ///     same_instant.checked_duration_since(same_instant),
    ///     Some(Duration::ZERO)
    /// );
    /// ```
    #[must_use]
    pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
        self.inner.checked_duration_since(earlier.inner)
    }

    /// Adds a duration to this instant, returning `None` if overflow occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let instant = clock.now();
    ///
    /// let later = instant.checked_add(Duration::from_secs(5)).unwrap();
    /// assert!(later > instant);
    ///
    /// // This would overflow
    /// assert!(instant.checked_add(Duration::MAX).is_none());
    /// ```
    #[must_use]
    pub fn checked_add(&self, duration: Duration) -> Option<Self> {
        self.inner.checked_add(duration).map(Self::from)
    }

    /// Subtracts a duration from this instant, returning `None` if underflow occurred.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use std::time::Duration;
    ///
    /// use fast_time::Clock;
    ///
    /// let mut clock = Clock::new();
    /// let instant = clock.now();
    ///
    /// let earlier = instant.checked_sub(Duration::from_secs(5)).unwrap();
    /// assert!(earlier < instant);
    ///
    /// // This would underflow
    /// assert!(instant.checked_sub(Duration::MAX).is_none());
    /// ```
    #[must_use]
    pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
        self.inner.checked_sub(duration).map(Self::from)
    }
}

impl From<std::time::Instant> for Instant {
    #[inline]
    fn from(inner: std::time::Instant) -> Self {
        Self { inner }
    }
}

impl From<Instant> for std::time::Instant {
    #[inline]
    fn from(instant: Instant) -> Self {
        instant.inner
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use std::panic::{RefUnwindSafe, UnwindSafe};

    use mockall::Sequence;

    use super::*;
    use crate::pal::{MockPlatform, MockTimeSource};

    static_assertions::assert_impl_all!(Instant: UnwindSafe, RefUnwindSafe);

    #[test]
    fn saturating_duration_since_can_math() {
        use std::time::Duration;

        // Create synthetic test data using arbitrary durations added to a base instant
        // Note: In real code, we would use Clock::now(), but for testing we create arbitrary instants
        let base_instant = std::time::Instant::now();
        let instant1 = Instant::from(base_instant);
        let instant2 = Instant::from(base_instant + Duration::from_secs(10));
        let instant3 = Instant::from(base_instant + Duration::from_secs(50));

        // Test normal case: later instant minus earlier instant
        let duration_1_to_2 = instant2.saturating_duration_since(instant1);
        assert_eq!(duration_1_to_2, Duration::from_secs(10));

        let duration_1_to_3 = instant3.saturating_duration_since(instant1);
        assert_eq!(duration_1_to_3, Duration::from_secs(50));

        let duration_2_to_3 = instant3.saturating_duration_since(instant2);
        assert_eq!(duration_2_to_3, Duration::from_secs(40));

        // Test saturating behavior: earlier instant minus later instant should return zero
        let duration_reverse_1 = instant1.saturating_duration_since(instant2);
        assert_eq!(duration_reverse_1, Duration::ZERO);

        let duration_reverse_2 = instant1.saturating_duration_since(instant3);
        assert_eq!(duration_reverse_2, Duration::ZERO);

        let duration_reverse_3 = instant2.saturating_duration_since(instant3);
        assert_eq!(duration_reverse_3, Duration::ZERO);

        // Test same instant
        let duration_same = instant1.saturating_duration_since(instant1);
        assert_eq!(duration_same, Duration::ZERO);
    }

    #[test]
    fn duration_since_can_math() {
        let a = std::time::Instant::now();
        let b = a.checked_add(Duration::from_millis(100)).unwrap();

        let mut time_source = MockTimeSource::new();

        let mut seq = Sequence::new();

        time_source
            .expect_now()
            .once()
            .in_sequence(&mut seq)
            .return_once(move || a);

        time_source
            .expect_now()
            .once()
            .in_sequence(&mut seq)
            .return_once(move || b);

        let mut platform = MockPlatform::new();

        platform
            .expect_new_time_source()
            .once()
            .return_once(move || time_source);

        let mut clock = Clock::from_pal(platform.into());

        let instant1 = clock.now();
        let instant2 = clock.now();

        // instant1 is earlier, so this returns zero
        let duration = instant1.saturating_duration_since(instant2);
        assert_eq!(duration, Duration::ZERO);

        let duration = instant2.saturating_duration_since(instant1);
        assert_eq!(duration, Duration::from_millis(100));
    }

    #[test]
    fn elapsed_can_math() {
        let a = std::time::Instant::now();
        let b = a.checked_add(Duration::from_millis(100)).unwrap();

        let mut time_source = MockTimeSource::new();

        let mut seq = Sequence::new();

        time_source
            .expect_now()
            .once()
            .in_sequence(&mut seq)
            .return_once(move || a);

        time_source
            .expect_now()
            .once()
            .in_sequence(&mut seq)
            .return_once(move || b);

        let mut platform = MockPlatform::new();

        platform
            .expect_new_time_source()
            .once()
            .return_once(move || time_source);

        let mut clock = Clock::from_pal(platform.into());

        let instant1 = clock.now();

        let duration = instant1.elapsed(&mut clock);
        assert_eq!(duration, Duration::from_millis(100));
    }

    #[test]
    fn checked_duration_since_can_math() {
        use std::time::Duration;

        // Create synthetic test data using arbitrary durations added to a base instant
        let base_instant = std::time::Instant::now();
        let instant1 = Instant::from(base_instant);
        let instant2 = Instant::from(base_instant + Duration::from_secs(10));

        // Test normal case: later instant minus earlier instant
        let duration = instant2.checked_duration_since(instant1);
        assert_eq!(duration, Some(Duration::from_secs(10)));

        // Test reverse case: earlier instant minus later instant should return None
        let duration_reverse = instant1.checked_duration_since(instant2);
        assert_eq!(duration_reverse, None);

        // Test same instant
        let duration_same = instant1.checked_duration_since(instant1);
        assert_eq!(duration_same, Some(Duration::ZERO));
    }

    #[test]
    fn duration_since_is_alias_for_saturating_duration_since() {
        use std::time::Duration;

        let base_instant = std::time::Instant::now();
        let instant1 = Instant::from(base_instant);
        let instant2 = Instant::from(base_instant + Duration::from_secs(5));

        // Both methods should return the same result
        let duration_saturating = instant2.saturating_duration_since(instant1);
        let duration_alias = instant2.duration_since(instant1);
        assert_eq!(duration_saturating, duration_alias);

        // Test reverse case: both should return zero
        let duration_saturating_reverse = instant1.saturating_duration_since(instant2);
        let duration_alias_reverse = instant1.duration_since(instant2);
        assert_eq!(duration_saturating_reverse, duration_alias_reverse);
        assert_eq!(duration_alias_reverse, Duration::ZERO);
    }

    #[test]
    fn checked_add_can_math() {
        use std::time::Duration;

        let base_instant = std::time::Instant::now();
        let instant = Instant::from(base_instant);

        // Test normal addition
        let duration = Duration::from_secs(5);
        let later_instant = instant.checked_add(duration).unwrap();
        let expected = Instant::from(base_instant + duration);
        assert_eq!(later_instant, expected);

        // Test that the result is indeed later
        assert!(later_instant > instant);

        // Test zero addition
        let same_instant = instant.checked_add(Duration::ZERO).unwrap();
        assert_eq!(same_instant, instant);
    }

    #[test]
    fn checked_sub_can_math() {
        use std::time::Duration;

        let base_instant = std::time::Instant::now();
        let duration = Duration::from_secs(10);
        let later_instant = base_instant + duration;
        let instant = Instant::from(later_instant);

        // Test normal subtraction
        let earlier_instant = instant.checked_sub(Duration::from_secs(5)).unwrap();
        let expected = Instant::from(later_instant.checked_sub(Duration::from_secs(5)).unwrap());
        assert_eq!(earlier_instant, expected);

        // Test that the result is indeed earlier
        assert!(earlier_instant < instant);

        // Test zero subtraction
        let same_instant = instant.checked_sub(Duration::ZERO).unwrap();
        assert_eq!(same_instant, instant);

        // Test subtracting the full duration
        let base_again = instant.checked_sub(duration).unwrap();
        let expected_base = Instant::from(base_instant);
        assert_eq!(base_again, expected_base);
    }
}