]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/wasm/time.rs
Rollup merge of #59432 - phansch:compiletest_docs, r=alexcrichton
[rust.git] / src / libstd / sys / wasm / time.rs
1 use crate::time::Duration;
2 use crate::sys::{TimeSysCall, TimeClock};
3
4 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
5 pub struct Instant(Duration);
6
7 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8 pub struct SystemTime(Duration);
9
10 pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0));
11
12 impl Instant {
13     pub fn now() -> Instant {
14         Instant(TimeSysCall::perform(TimeClock::Monotonic))
15     }
16
17     pub const fn zero() -> Instant {
18         Instant(Duration::from_secs(0))
19     }
20
21     pub fn actually_monotonic() -> bool {
22         false
23     }
24
25     pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
26         self.0.checked_sub(other.0)
27     }
28
29     pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> {
30         Some(Instant(self.0.checked_add(*other)?))
31     }
32
33     pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
34         Some(Instant(self.0.checked_sub(*other)?))
35     }
36 }
37
38 impl SystemTime {
39     pub fn now() -> SystemTime {
40         SystemTime(TimeSysCall::perform(TimeClock::System))
41     }
42
43     pub fn sub_time(&self, other: &SystemTime)
44                     -> Result<Duration, Duration> {
45         self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
46     }
47
48     pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
49         Some(SystemTime(self.0.checked_add(*other)?))
50     }
51
52     pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
53         Some(SystemTime(self.0.checked_sub(*other)?))
54     }
55 }