1use std::{
5 ops::{Add, Sub},
6 time::Duration,
7};
8#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct HostInstant(Duration);
10impl HostInstant {
11 pub const fn from_duration(ticks: Duration) -> Self {
12 Self(ticks)
13 }
14 pub const fn as_duration(self) -> Duration {
15 self.0
16 }
17 pub fn checked_add(self, d: Duration) -> Option<Self> {
18 self.0.checked_add(d).map(Self)
19 }
20 pub fn checked_sub(self, d: Duration) -> Option<Self> {
21 self.0.checked_sub(d).map(Self)
22 }
23 pub fn saturating_duration_since(self, earlier: Self) -> Duration {
24 self.0.saturating_sub(earlier.0)
25 }
26 pub fn duration_since(self, earlier: Self) -> Duration {
27 self.saturating_duration_since(earlier)
28 }
29}
30impl Add<Duration> for HostInstant {
31 type Output = Self;
32 fn add(self, d: Duration) -> Self {
33 self.checked_add(d).expect("Host instant overflow")
34 }
35}
36impl Sub<Duration> for HostInstant {
37 type Output = Self;
38 fn sub(self, d: Duration) -> Self {
39 self.checked_sub(d).expect("Host instant underflow")
40 }
41}
42impl Sub for HostInstant {
43 type Output = Duration;
44 fn sub(self, earlier: Self) -> Duration {
45 self.duration_since(earlier)
46 }
47}
48#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
49pub use HostInstant as Instant;
50#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
51pub use std::time::Instant;