1#![allow(deprecated)]
4
5use core::borrow::Borrow;
6use core::cmp::{Ord, Ordering, PartialEq, PartialOrd};
7use core::ops::{Add, Sub};
8use core::time::Duration as StdDuration;
9use std::time::Instant as StdInstant;
10
11use crate::internal_macros::{impl_add_assign, impl_sub_assign};
12use crate::Duration;
13
14#[deprecated(since = "0.3.35", note = "import `time::ext::InstantExt` instead")]
32#[repr(transparent)]
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
34pub struct Instant(pub StdInstant);
35
36impl Instant {
37 pub fn now() -> Self {
46 Self(StdInstant::now())
47 }
48
49 pub fn elapsed(self) -> Duration {
61 Self::now() - self
62 }
63 pub fn checked_add(self, duration: Duration) -> Option<Self> {
78 if duration.is_zero() {
79 Some(self)
80 } else if duration.is_positive() {
81 self.0.checked_add(duration.unsigned_abs()).map(Self)
82 } else {
83 debug_assert!(duration.is_negative());
84 self.0.checked_sub(duration.unsigned_abs()).map(Self)
85 }
86 }
87
88 pub fn checked_sub(self, duration: Duration) -> Option<Self> {
100 if duration.is_zero() {
101 Some(self)
102 } else if duration.is_positive() {
103 self.0.checked_sub(duration.unsigned_abs()).map(Self)
104 } else {
105 debug_assert!(duration.is_negative());
106 self.0.checked_add(duration.unsigned_abs()).map(Self)
107 }
108 }
109 pub const fn into_inner(self) -> StdInstant {
120 self.0
121 }
122}
123
124impl From<StdInstant> for Instant {
126 fn from(instant: StdInstant) -> Self {
127 Self(instant)
128 }
129}
130
131impl From<Instant> for StdInstant {
132 fn from(instant: Instant) -> Self {
133 instant.0
134 }
135}
136
137impl Sub for Instant {
138 type Output = Duration;
139
140 fn sub(self, other: Self) -> Self::Output {
144 match self.0.cmp(&other.0) {
145 Ordering::Equal => Duration::ZERO,
146 Ordering::Greater => (self.0 - other.0)
147 .try_into()
148 .expect("overflow converting `std::time::Duration` to `time::Duration`"),
149 Ordering::Less => -Duration::try_from(other.0 - self.0)
150 .expect("overflow converting `std::time::Duration` to `time::Duration`"),
151 }
152 }
153}
154
155impl Sub<StdInstant> for Instant {
156 type Output = Duration;
157
158 fn sub(self, other: StdInstant) -> Self::Output {
159 self - Self(other)
160 }
161}
162
163impl Sub<Instant> for StdInstant {
164 type Output = Duration;
165
166 fn sub(self, other: Instant) -> Self::Output {
167 Instant(self) - other
168 }
169}
170
171impl Add<Duration> for Instant {
172 type Output = Self;
173
174 fn add(self, duration: Duration) -> Self::Output {
179 if duration.is_positive() {
180 Self(self.0 + duration.unsigned_abs())
181 } else if duration.is_negative() {
182 #[allow(clippy::unchecked_duration_subtraction)]
183 Self(self.0 - duration.unsigned_abs())
184 } else {
185 debug_assert!(duration.is_zero());
186 self
187 }
188 }
189}
190
191impl Add<Duration> for StdInstant {
192 type Output = Self;
193
194 fn add(self, duration: Duration) -> Self::Output {
195 (Instant(self) + duration).0
196 }
197}
198
199impl Add<StdDuration> for Instant {
200 type Output = Self;
201
202 fn add(self, duration: StdDuration) -> Self::Output {
203 Self(self.0 + duration)
204 }
205}
206
207impl_add_assign!(Instant: Duration, StdDuration);
208impl_add_assign!(StdInstant: Duration);
209
210impl Sub<Duration> for Instant {
211 type Output = Self;
212
213 fn sub(self, duration: Duration) -> Self::Output {
218 if duration.is_positive() {
219 #[allow(clippy::unchecked_duration_subtraction)]
220 Self(self.0 - duration.unsigned_abs())
221 } else if duration.is_negative() {
222 Self(self.0 + duration.unsigned_abs())
223 } else {
224 debug_assert!(duration.is_zero());
225 self
226 }
227 }
228}
229
230impl Sub<Duration> for StdInstant {
231 type Output = Self;
232
233 fn sub(self, duration: Duration) -> Self::Output {
234 (Instant(self) - duration).0
235 }
236}
237
238impl Sub<StdDuration> for Instant {
239 type Output = Self;
240
241 fn sub(self, duration: StdDuration) -> Self::Output {
246 #[allow(clippy::unchecked_duration_subtraction)]
247 Self(self.0 - duration)
248 }
249}
250
251impl_sub_assign!(Instant: Duration, StdDuration);
252impl_sub_assign!(StdInstant: Duration);
253
254impl PartialEq<StdInstant> for Instant {
255 fn eq(&self, rhs: &StdInstant) -> bool {
256 self.0.eq(rhs)
257 }
258}
259
260impl PartialEq<Instant> for StdInstant {
261 fn eq(&self, rhs: &Instant) -> bool {
262 self.eq(&rhs.0)
263 }
264}
265
266impl PartialOrd<StdInstant> for Instant {
267 fn partial_cmp(&self, rhs: &StdInstant) -> Option<Ordering> {
268 self.0.partial_cmp(rhs)
269 }
270}
271
272impl PartialOrd<Instant> for StdInstant {
273 fn partial_cmp(&self, rhs: &Instant) -> Option<Ordering> {
274 self.partial_cmp(&rhs.0)
275 }
276}
277
278impl AsRef<StdInstant> for Instant {
279 fn as_ref(&self) -> &StdInstant {
280 &self.0
281 }
282}
283
284impl Borrow<StdInstant> for Instant {
285 fn borrow(&self) -> &StdInstant {
286 &self.0
287 }
288}
289