Coverage Report

Created: 2026-05-16 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/cap-primitives-3.4.5/src/time/instant.rs
Line
Count
Source
1
use crate::time::Duration;
2
use std::ops::{Add, AddAssign, Sub, SubAssign};
3
use std::{fmt, time};
4
5
/// A measurement of a monotonically nondecreasing clock.
6
///
7
/// This corresponds to [`std::time::Instant`].
8
///
9
/// This `Instant` has no `now` or `elapsed` methods. To obtain the current
10
/// time or measure the duration to the current time, first obtain a
11
/// [`MonotonicClock`], and then call [`MonotonicClock::now`] or
12
/// [`MonotonicClock::elapsed`] instead.
13
///
14
/// [`MonotonicClock`]: crate::time::MonotonicClock
15
/// [`MonotonicClock::now`]: crate::time::MonotonicClock::now
16
/// [`MonotonicClock::elapsed`]: crate::time::MonotonicClock::elapsed
17
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
18
pub struct Instant {
19
    pub(crate) std: time::Instant,
20
}
21
22
impl Instant {
23
    /// Constructs a new instance of `Self` from the given
24
    /// [`std::time::Instant`].
25
    #[inline]
26
0
    pub const fn from_std(std: time::Instant) -> Self {
27
0
        Self { std }
28
0
    }
29
30
    /// Returns the amount of time elapsed from another instant to this one.
31
    ///
32
    /// This corresponds to [`std::time::Instant::duration_since`].
33
    #[inline]
34
0
    pub fn duration_since(&self, earlier: Self) -> Duration {
35
0
        self.std.duration_since(earlier.std)
36
0
    }
37
38
    /// Returns the amount of time elapsed from another instant to this one, or
39
    /// None if that instant is later than this one.
40
    ///
41
    /// This corresponds to [`std::time::Instant::checked_duration_since`].
42
    #[inline]
43
0
    pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
44
0
        self.std.checked_duration_since(earlier.std)
45
0
    }
46
47
    /// Returns the amount of time elapsed from another instant to this one, or
48
    /// zero duration if that instant is later than this one.
49
    ///
50
    /// This corresponds to [`std::time::Instant::saturating_duration_since`].
51
    #[inline]
52
0
    pub fn saturating_duration_since(&self, earlier: Self) -> Duration {
53
0
        self.std.saturating_duration_since(earlier.std)
54
0
    }
55
56
    /// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be
57
    /// represented as `Instant` (which means it's inside the bounds of the
58
    /// underlying data structure), `None` otherwise.
59
    ///
60
    /// This corresponds to [`std::time::Instant::checked_add`].
61
    #[inline]
62
0
    pub fn checked_add(&self, duration: Duration) -> Option<Self> {
63
0
        self.std.checked_add(duration).map(Self::from_std)
64
0
    }
65
66
    /// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be
67
    /// represented as `Instant` (which means it's inside the bounds of the
68
    /// underlying data structure), `None` otherwise.
69
    ///
70
    /// This corresponds to [`std::time::Instant::checked_sub`].
71
    #[inline]
72
0
    pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
73
0
        self.std.checked_sub(duration).map(Self::from_std)
74
0
    }
75
}
76
77
impl Add<Duration> for Instant {
78
    type Output = Self;
79
80
    /// # Panics
81
    ///
82
    /// This function may panic if the resulting point in time cannot be
83
    /// represented by the underlying data structure. See
84
    /// [`Instant::checked_add`] for a version without panic.
85
    #[inline]
86
0
    fn add(self, other: Duration) -> Self {
87
0
        self.checked_add(other)
88
0
            .expect("overflow when adding duration to instant")
89
0
    }
90
}
91
92
impl AddAssign<Duration> for Instant {
93
    #[inline]
94
0
    fn add_assign(&mut self, other: Duration) {
95
0
        *self = *self + other;
96
0
    }
97
}
98
99
impl Sub<Duration> for Instant {
100
    type Output = Self;
101
102
    #[inline]
103
0
    fn sub(self, other: Duration) -> Self {
104
0
        self.checked_sub(other)
105
0
            .expect("overflow when subtracting duration from instant")
106
0
    }
107
}
108
109
impl SubAssign<Duration> for Instant {
110
    #[inline]
111
0
    fn sub_assign(&mut self, other: Duration) {
112
0
        *self = *self - other;
113
0
    }
114
}
115
116
impl Sub<Instant> for Instant {
117
    type Output = Duration;
118
119
    #[inline]
120
0
    fn sub(self, other: Self) -> Duration {
121
0
        self.duration_since(other)
122
0
    }
123
}
124
125
impl fmt::Debug for Instant {
126
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127
0
        self.std.fmt(f)
128
0
    }
129
}