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
#![allow(
clippy::disallowed_types,
reason = "only allow std::time::Instant here when it's not WASM"
)]
#![allow(clippy::unchecked_time_subtraction, reason = "just forwarded")]
use std::{
ops::{Add, AddAssign, Deref, Sub, SubAssign},
time::Duration,
};
#[cfg(not(target_arch = "wasm32"))]
use std::time::Instant as InnerInstant;
#[cfg(target_arch = "wasm32")]
pub use web_time::Instant as InnerInstant;
/// WASM-safe alternative to [`std::time::Instant`].
///
/// This is a thin wrapper around either `std::time::Instant` or `web_time::Instant` to allow
/// compiling for WASM without issues.
/// `web_time::Instant` usually re-exports `std::time::Instant` on non-WASM targets but this does
/// not allow usage of `clippy::disallowed-types` properly.
/// This wrapper fixes that.
///
/// For any reference, see [`std::time::Instant`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Instant(InnerInstant);
impl Instant {
/// Returns an instant corresponding to "now".
///
/// See [`Instant::now`](InnerInstant::now).
pub fn now() -> Self {
Instant(InnerInstant::now())
}
/// Returns the amount of time elapsed from another instant to this one, or zero duration if
/// that instant is later than this one.
///
/// See [`Instant::duration_since`](InnerInstant::duration_since).
pub fn duration_since(&self, earlier: Instant) -> Duration {
self.0.duration_since(earlier.0)
}
/// Returns the amount of time elapsed from another instant to this one, or None if that instant
/// is later than this one.
///
/// See [`Instant::checked_duration_since`](InnerInstant::checked_duration_since).
pub fn checked_duration_since(&self, earlier: Instant) -> Option<Duration> {
self.0.checked_duration_since(earlier.0)
}
/// Returns the amount of time elapsed from another instant to this one, or zero duration if
/// that instant is later than this one.
///
/// See [`Instant::saturating_duration_since`](InnerInstant::saturating_duration_since).
pub fn saturating_duration_since(&self, earlier: Instant) -> Duration {
self.0.saturating_duration_since(earlier.0)
}
/// Returns `Some(t)` where `t` is the time `self + duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
///
/// See [`Instant::checked_add`](InnerInstant::checked_add).
pub fn checked_add(&self, duration: Duration) -> Option<Instant> {
self.0.checked_add(duration).map(Self)
}
/// Returns `Some(t)` where `t` is the time `self - duration` if `t` can be represented as
/// `Instant` (which means it's inside the bounds of the underlying data structure), `None`
/// otherwise.
///
/// See [`Instant::checked_sub`](InnerInstant::checked_sub).
pub fn checked_sub(&self, duration: Duration) -> Option<Instant> {
self.0.checked_sub(duration).map(Self)
}
}
impl Deref for Instant {
type Target = InnerInstant;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Self::Output {
Instant(self.0.add(rhs))
}
}
impl AddAssign<Duration> for Instant {
fn add_assign(&mut self, rhs: Duration) {
self.0.add_assign(rhs)
}
}
impl Sub<Duration> for Instant {
type Output = Instant;
fn sub(self, rhs: Duration) -> Self::Output {
Instant(self.0.sub(rhs))
}
}
impl SubAssign<Duration> for Instant {
fn sub_assign(&mut self, rhs: Duration) {
self.0.sub_assign(rhs)
}
}
impl Sub<Instant> for Instant {
type Output = Duration;
fn sub(self, rhs: Instant) -> Self::Output {
self.0.sub(rhs.0)
}
}
impl From<InnerInstant> for Instant {
fn from(value: InnerInstant) -> Self {
Self(value)
}
}