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
use std::ops::{Add, AddAssign, Sub, SubAssign};
use std::time::Duration;
pub trait Instant<S>
where
S: Sized,
{
fn elapsed(&self) -> Duration;
fn now() -> Self;
fn duration_since(&self, earlier: UniversalInstant) -> Duration;
fn checked_add(&self, duration: Duration) -> Option<S>;
fn checked_sub(&self, duration: Duration) -> Option<S>;
}
#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UniversalInstant(std::time::Instant);
#[cfg(not(target_arch = "wasm32"))]
impl Instant<Self> for UniversalInstant {
fn now() -> Self {
Self(std::time::Instant::now())
}
fn duration_since(&self, earlier: UniversalInstant) -> Duration {
self.0.duration_since(earlier.0)
}
fn elapsed(&self) -> Duration {
self.0.elapsed()
}
fn checked_add(&self, duration: Duration) -> Option<Self> {
self.0.checked_add(duration).map(Self)
}
fn checked_sub(&self, duration: Duration) -> Option<Self> {
self.0.checked_sub(duration).map(Self)
}
}
#[cfg(target_arch = "wasm32")]
use std::convert::TryInto;
#[cfg(target_arch = "wasm32")]
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct UniversalInstant(u64);
#[cfg(target_arch = "wasm32")]
impl Instant<Self> for UniversalInstant {
fn now() -> Self {
Self((unsafe { crate::cx_wasm32::performanceNow() } * 1000.0) as u64)
}
fn duration_since(&self, earlier: UniversalInstant) -> Duration {
Duration::from_micros(self.0 - earlier.0)
}
fn elapsed(&self) -> Duration {
Self::now().duration_since(*self)
}
fn checked_add(&self, duration: Duration) -> Option<Self> {
match duration.as_micros().try_into() {
Ok(duration) => self.0.checked_add(duration).map(|i| Self(i)),
Err(_) => None,
}
}
fn checked_sub(&self, duration: Duration) -> Option<Self> {
match duration.as_micros().try_into() {
Ok(duration) => self.0.checked_sub(duration).map(|i| Self(i)),
Err(_) => None,
}
}
}
impl Add<Duration> for UniversalInstant {
type Output = UniversalInstant;
fn add(self, other: Duration) -> UniversalInstant {
self.checked_add(other).unwrap()
}
}
impl Sub<Duration> for UniversalInstant {
type Output = UniversalInstant;
fn sub(self, other: Duration) -> UniversalInstant {
self.checked_sub(other).unwrap()
}
}
impl Sub<UniversalInstant> for UniversalInstant {
type Output = Duration;
fn sub(self, other: UniversalInstant) -> Duration {
self.duration_since(other)
}
}
impl AddAssign<Duration> for UniversalInstant {
fn add_assign(&mut self, other: Duration) {
*self = *self + other;
}
}
impl SubAssign<Duration> for UniversalInstant {
fn sub_assign(&mut self, other: Duration) {
*self = *self - other;
}
}