|
3 | 3 | use crate::time::Duration; |
4 | 4 |
|
5 | 5 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
6 | | -pub struct Instant(Duration); |
| 6 | +pub struct Instant { |
| 7 | + nanos: wasi::Timestamp, |
| 8 | +} |
7 | 9 |
|
8 | 10 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] |
9 | | -pub struct SystemTime(Duration); |
| 11 | +pub struct SystemTime { |
| 12 | + nanos: wasi::Timestamp, |
| 13 | +} |
10 | 14 |
|
11 | | -pub const UNIX_EPOCH: SystemTime = SystemTime(Duration::from_secs(0)); |
| 15 | +pub const UNIX_EPOCH: SystemTime = SystemTime { nanos: 0 }; |
12 | 16 |
|
13 | | -fn current_time(clock: wasi::Clockid) -> Duration { |
14 | | - let ts = unsafe { |
| 17 | +fn current_time(clock: wasi::Clockid) -> wasi::Timestamp { |
| 18 | + unsafe { |
15 | 19 | wasi::clock_time_get( |
16 | 20 | clock, 1, // precision... seems ignored though? |
17 | 21 | ) |
18 | 22 | .unwrap() |
19 | | - }; |
20 | | - Duration::new((ts / 1_000_000_000) as u64, (ts % 1_000_000_000) as u32) |
| 23 | + } |
21 | 24 | } |
22 | 25 |
|
23 | 26 | impl Instant { |
24 | 27 | pub fn now() -> Instant { |
25 | | - Instant(current_time(wasi::CLOCKID_MONOTONIC)) |
| 28 | + Instant { nanos: current_time(wasi::CLOCKID_MONOTONIC) } |
26 | 29 | } |
27 | 30 |
|
28 | 31 | pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> { |
29 | | - self.0.checked_sub(other.0) |
| 32 | + let nanos = self.nanos.checked_sub(other.nanos)?; |
| 33 | + Some(Duration::from_nanos(nanos)) |
30 | 34 | } |
31 | 35 |
|
32 | 36 | pub fn checked_add_duration(&self, other: &Duration) -> Option<Instant> { |
33 | | - Some(Instant(self.0.checked_add(*other)?)) |
| 37 | + let to_add = other.as_nanos().try_into().ok()?; |
| 38 | + let nanos = self.nanos.checked_add(to_add)?; |
| 39 | + Some(Instant { nanos }) |
34 | 40 | } |
35 | 41 |
|
36 | 42 | pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> { |
37 | | - Some(Instant(self.0.checked_sub(*other)?)) |
| 43 | + let to_sub = other.as_nanos().try_into().ok()?; |
| 44 | + let nanos = self.nanos.checked_sub(to_sub)?; |
| 45 | + Some(Instant { nanos }) |
38 | 46 | } |
39 | 47 | } |
40 | 48 |
|
41 | 49 | impl SystemTime { |
42 | 50 | pub fn now() -> SystemTime { |
43 | | - SystemTime(current_time(wasi::CLOCKID_REALTIME)) |
| 51 | + SystemTime { nanos: current_time(wasi::CLOCKID_REALTIME) } |
44 | 52 | } |
45 | 53 |
|
46 | 54 | pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime { |
47 | | - SystemTime(Duration::from_nanos(ts)) |
| 55 | + SystemTime { nanos: ts } |
48 | 56 | } |
49 | 57 |
|
50 | | - pub fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> { |
51 | | - self.0.as_nanos().try_into().ok() |
| 58 | + pub fn to_wasi_timestamp(self) -> wasi::Timestamp { |
| 59 | + self.nanos |
52 | 60 | } |
53 | 61 |
|
54 | 62 | pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { |
55 | | - self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0) |
| 63 | + self.nanos |
| 64 | + .checked_sub(other.nanos) |
| 65 | + .map(Duration::from_nanos) |
| 66 | + .ok_or_else(|| Duration::from_nanos(other.nanos - self.nanos)) |
56 | 67 | } |
57 | 68 |
|
58 | 69 | pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> { |
59 | | - Some(SystemTime(self.0.checked_add(*other)?)) |
| 70 | + let to_add = other.as_nanos().try_into().ok()?; |
| 71 | + let nanos = self.nanos.checked_add(to_add)?; |
| 72 | + Some(SystemTime { nanos }) |
60 | 73 | } |
61 | 74 |
|
62 | 75 | pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> { |
63 | | - Some(SystemTime(self.0.checked_sub(*other)?)) |
| 76 | + let to_sub = other.as_nanos().try_into().ok()?; |
| 77 | + let nanos = self.nanos.checked_sub(to_sub)?; |
| 78 | + Some(SystemTime { nanos }) |
64 | 79 | } |
65 | 80 | } |
0 commit comments