1use core::fmt;
5use core::ops::{Add, AddAssign, Sub, SubAssign};
6use std::time::Duration;
7
8#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
15pub struct Instant(std::time::Instant);
16
17impl Instant {
18 #[inline]
20 pub fn now() -> Self {
21 Self(super::clock::now())
22 }
23
24 pub fn from_std(std: std::time::Instant) -> Self {
26 Self(std)
27 }
28
29 pub fn into_std(self) -> std::time::Instant {
31 self.0
32 }
33
34 pub fn duration_since(&self, earlier: Self) -> Duration {
36 self.0.saturating_duration_since(earlier.0)
37 }
38
39 pub fn checked_duration_since(&self, earlier: Self) -> Option<Duration> {
41 self.0.checked_duration_since(earlier.0)
42 }
43
44 pub fn saturating_duration_since(&self, earlier: Self) -> Duration {
46 self.0.saturating_duration_since(earlier.0)
47 }
48
49 pub fn elapsed(&self) -> Duration {
51 Self::now().saturating_duration_since(*self)
52 }
53
54 pub fn checked_add(&self, duration: Duration) -> Option<Self> {
56 self.0.checked_add(duration).map(Self)
57 }
58
59 pub fn checked_sub(&self, duration: Duration) -> Option<Self> {
61 self.0.checked_sub(duration).map(Self)
62 }
63}
64
65impl From<std::time::Instant> for Instant {
66 fn from(std: std::time::Instant) -> Self {
67 Self(std)
68 }
69}
70
71impl From<Instant> for std::time::Instant {
72 fn from(instant: Instant) -> Self {
73 instant.0
74 }
75}
76
77impl Add<Duration> for Instant {
78 type Output = Self;
79
80 fn add(self, duration: Duration) -> Self {
84 Self(self.0 + duration)
85 }
86}
87
88impl AddAssign<Duration> for Instant {
89 fn add_assign(&mut self, duration: Duration) {
90 self.0 += duration;
91 }
92}
93
94impl Sub<Duration> for Instant {
95 type Output = Self;
96
97 fn sub(self, duration: Duration) -> Self {
101 Self(self.0 - duration)
102 }
103}
104
105impl SubAssign<Duration> for Instant {
106 fn sub_assign(&mut self, duration: Duration) {
107 self.0 -= duration;
108 }
109}
110
111impl Sub for Instant {
112 type Output = Duration;
113
114 fn sub(self, earlier: Self) -> Duration {
116 self.saturating_duration_since(earlier)
117 }
118}
119
120impl fmt::Debug for Instant {
121 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122 self.0.fmt(f)
123 }
124}