Skip to content

Commit d013b19

Browse files
committed
Use common Timestamp impl in Hermit
1 parent a3b8e72 commit d013b19

File tree

1 file changed

+9
-113
lines changed
  • library/std/src/sys/pal/hermit

1 file changed

+9
-113
lines changed

library/std/src/sys/pal/hermit/time.rs

Lines changed: 9 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,23 @@
11
#![allow(dead_code)]
22

3-
use core::hash::{Hash, Hasher};
3+
use core::hash::Hash;
44

55
use super::hermit_abi::{self, CLOCK_MONOTONIC, CLOCK_REALTIME, timespec};
6-
use crate::cmp::Ordering;
76
use crate::ops::{Add, AddAssign, Sub, SubAssign};
7+
use crate::sys::common::timespec::Timespec;
88
use crate::time::Duration;
99

1010
const NSEC_PER_SEC: i32 = 1_000_000_000;
1111

12-
#[derive(Copy, Clone, Debug)]
13-
struct Timespec {
14-
t: timespec,
15-
}
16-
17-
impl Timespec {
18-
const MAX: Timespec = Self::new(i64::MAX, 1_000_000_000 - 1);
19-
20-
const MIN: Timespec = Self::new(i64::MIN, 0);
21-
22-
const fn zero() -> Timespec {
23-
Timespec { t: timespec { tv_sec: 0, tv_nsec: 0 } }
24-
}
25-
26-
const fn new(tv_sec: i64, tv_nsec: i32) -> Timespec {
27-
assert!(tv_nsec >= 0 && tv_nsec < NSEC_PER_SEC);
28-
// SAFETY: The assert above checks tv_nsec is within the valid range
29-
Timespec { t: timespec { tv_sec, tv_nsec } }
30-
}
31-
32-
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
33-
fn sub_ge_to_unsigned(a: i64, b: i64) -> u64 {
34-
debug_assert!(a >= b);
35-
a.wrapping_sub(b).cast_unsigned()
36-
}
37-
38-
if self >= other {
39-
// Logic here is identical to Unix version of `Timestamp::sub_timespec`,
40-
// check comments there why operations do not overflow.
41-
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
42-
Duration::new(
43-
sub_ge_to_unsigned(self.t.tv_sec, other.t.tv_sec),
44-
(self.t.tv_nsec - other.t.tv_nsec) as u32,
45-
)
46-
} else {
47-
Duration::new(
48-
sub_ge_to_unsigned(self.t.tv_sec - 1, other.t.tv_sec),
49-
(self.t.tv_nsec + NSEC_PER_SEC - other.t.tv_nsec) as u32,
50-
)
51-
})
52-
} else {
53-
match other.sub_timespec(self) {
54-
Ok(d) => Err(d),
55-
Err(d) => Ok(d),
56-
}
57-
}
58-
}
59-
60-
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
61-
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
62-
63-
// Nano calculations can't overflow because nanos are <1B which fit
64-
// in a u32.
65-
let mut nsec = other.subsec_nanos() + u32::try_from(self.t.tv_nsec).unwrap();
66-
if nsec >= NSEC_PER_SEC.try_into().unwrap() {
67-
nsec -= u32::try_from(NSEC_PER_SEC).unwrap();
68-
secs = secs.checked_add(1)?;
69-
}
70-
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
71-
}
72-
73-
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
74-
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
75-
76-
// Similar to above, nanos can't overflow.
77-
let mut nsec = self.t.tv_nsec as i32 - other.subsec_nanos() as i32;
78-
if nsec < 0 {
79-
nsec += NSEC_PER_SEC as i32;
80-
secs = secs.checked_sub(1)?;
81-
}
82-
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
83-
}
84-
}
85-
86-
impl PartialEq for Timespec {
87-
fn eq(&self, other: &Timespec) -> bool {
88-
self.t.tv_sec == other.t.tv_sec && self.t.tv_nsec == other.t.tv_nsec
89-
}
90-
}
91-
92-
impl Eq for Timespec {}
93-
94-
impl PartialOrd for Timespec {
95-
fn partial_cmp(&self, other: &Timespec) -> Option<Ordering> {
96-
Some(self.cmp(other))
97-
}
98-
}
99-
100-
impl Ord for Timespec {
101-
fn cmp(&self, other: &Timespec) -> Ordering {
102-
let me = (self.t.tv_sec, self.t.tv_nsec);
103-
let other = (other.t.tv_sec, other.t.tv_nsec);
104-
me.cmp(&other)
105-
}
106-
}
107-
108-
impl Hash for Timespec {
109-
fn hash<H: Hasher>(&self, state: &mut H) {
110-
self.t.tv_sec.hash(state);
111-
self.t.tv_nsec.hash(state);
112-
}
113-
}
114-
11512
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
11613
pub struct Instant(Timespec);
11714

11815
impl Instant {
11916
pub fn now() -> Instant {
120-
let mut time: Timespec = Timespec::zero();
121-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_MONOTONIC, &raw mut time.t) };
17+
let mut time: timespec = timespec { tv_sec: 0, tv_nsec: 0 };
18+
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_MONOTONIC, &raw mut time) };
12219

123-
Instant(time)
20+
Instant(Timespec::new(time.tv_sec, time.tv_nsec as i64).unwrap())
12421
}
12522

12623
#[stable(feature = "time2", since = "1.8.0")]
@@ -218,14 +115,13 @@ impl SystemTime {
218115
pub const MIN: SystemTime = SystemTime(Timespec::MIN);
219116

220117
pub fn new(tv_sec: i64, tv_nsec: i32) -> SystemTime {
221-
SystemTime(Timespec::new(tv_sec, tv_nsec))
118+
SystemTime(Timespec::new(tv_sec, tv_nsec as i64).unwrap())
222119
}
223120

224121
pub fn now() -> SystemTime {
225-
let mut time: Timespec = Timespec::zero();
226-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_REALTIME, &raw mut time.t) };
227-
228-
SystemTime(time)
122+
let mut time: timespec = timespec { tv_sec: 0, tv_nsec: 0 };
123+
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_REALTIME, &raw mut time) };
124+
SystemTime::new(time.tv_sec, time.tv_nsec)
229125
}
230126

231127
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {

0 commit comments

Comments
 (0)