1#![allow(
2 clippy::disallowed_types,
3 reason = "only allow std::time::Instant here when it's not WASM"
4)]
5#![allow(clippy::unchecked_time_subtraction, reason = "just forwarded")]
6
7use std::{
8 ops::{Add, AddAssign, Deref, Sub, SubAssign},
9 time::Duration,
10};
11
12#[cfg(not(target_arch = "wasm32"))]
13use std::time::Instant as InnerInstant;
14
15#[cfg(target_arch = "wasm32")]
16pub use web_time::Instant as InnerInstant;
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
28pub struct Instant(InnerInstant);
29
30impl Instant {
31 pub fn now() -> Self {
35 Instant(InnerInstant::now())
36 }
37
38 pub fn duration_since(&self, earlier: Instant) -> Duration {
43 self.0.duration_since(earlier.0)
44 }
45
46 pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
51 self.0.checked_duration_since(earlier.0)
52 }
53
54 pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
59 self.0.saturating_duration_since(earlier.0)
60 }
61
62 pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
68 self.0.checked_add(duration).map(Self)
69 }
70
71 pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
77 self.0.checked_sub(duration).map(Self)
78 }
79}
80
81impl Deref for Instant {
82 type Target = InnerInstant;
83
84 fn deref(&self) -> &Self::Target {
85 &self.0
86 }
87}
88
89impl Add<Duration> for Instant {
90 type Output = Instant;
91
92 fn add(self, rhs: Duration) -> Self::Output {
93 Instant(self.0.add(rhs))
94 }
95}
96
97impl AddAssign<Duration> for Instant {
98 fn add_assign(&mut self, rhs: Duration) {
99 self.0.add_assign(rhs)
100 }
101}
102
103impl Sub<Duration> for Instant {
104 type Output = Instant;
105
106 fn sub(self, rhs: Duration) -> Self::Output {
107 Instant(self.0.sub(rhs))
108 }
109}
110
111impl SubAssign<Duration> for Instant {
112 fn sub_assign(&mut self, rhs: Duration) {
113 self.0.sub_assign(rhs)
114 }
115}
116
117impl Sub<Instant> for Instant {
118 type Output = Duration;
119
120 fn sub(self, rhs: Instant) -> Self::Output {
121 self.0.sub(rhs.0)
122 }
123}
124
125impl From<InnerInstant> for Instant {
126 fn from(value: InnerInstant) -> Self {
127 Self(value)
128 }
129}