Skip to content

Commit a5006e0

Browse files
committed
Use common Timestamp impl in Hermit (attempt 2)
1 parent 562dee4 commit a5006e0

5 files changed

Lines changed: 17 additions & 125 deletions

File tree

library/std/src/sys/fs/hermit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ pub struct DirBuilder {
109109

110110
impl FileAttr {
111111
pub fn modified(&self) -> io::Result<SystemTime> {
112-
Ok(SystemTime::new(self.stat_val.st_mtim.tv_sec, self.stat_val.st_mtim.tv_nsec))
112+
SystemTime::new(self.stat_val.st_mtim.tv_sec, self.stat_val.st_mtim.tv_nsec.into())
113113
}
114114

115115
pub fn accessed(&self) -> io::Result<SystemTime> {
116-
Ok(SystemTime::new(self.stat_val.st_atim.tv_sec, self.stat_val.st_atim.tv_nsec))
116+
SystemTime::new(self.stat_val.st_atim.tv_sec, self.stat_val.st_atim.tv_nsec.into())
117117
}
118118

119119
pub fn created(&self) -> io::Result<SystemTime> {
120-
Ok(SystemTime::new(self.stat_val.st_ctim.tv_sec, self.stat_val.st_ctim.tv_nsec))
120+
SystemTime::new(self.stat_val.st_ctim.tv_sec, self.stat_val.st_ctim.tv_nsec.into())
121121
}
122122

123123
pub fn size(&self) -> u64 {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::sys::env;
2323

2424
pub mod futex;
2525
pub mod os;
26+
#[path = "../unix/time.rs"]
2627
pub mod time;
2728

2829
pub fn unsupported<T>() -> io::Result<T> {

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

Lines changed: 0 additions & 110 deletions
This file was deleted.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(in crate::sys) const TIMESPEC_MAX_CAPPED: libc::timespec = libc::timespec {
1717
tv_nsec: (u64::MAX % NSEC_PER_SEC) as i64,
1818
};
1919

20-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
2121
pub(crate) struct Timespec {
2222
pub tv_sec: i64,
2323
pub tv_nsec: Nanoseconds,
@@ -66,6 +66,7 @@ impl Timespec {
6666
}
6767
}
6868

69+
#[allow(dead_code)]
6970
pub fn now(clock: libc::clockid_t) -> Timespec {
7071
use crate::mem::MaybeUninit;
7172
use crate::sys::cvt;

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
use hermit_abi::{self, CLOCK_MONOTONIC, CLOCK_REALTIME};
22

3-
use crate::hash::Hash;
3+
use crate::io;
44
use crate::sys::pal::time::Timespec;
55
use crate::time::Duration;
66

7+
fn clock_gettime(clock: hermit_abi::clockid_t) -> Timespec {
8+
let mut t = hermit_abi::timespec { tv_sec: 0, tv_nsec: 0 };
9+
let _ = unsafe { hermit_abi::clock_gettime(clock, &raw mut t) };
10+
Timespec::new(t.tv_sec, t.tv_nsec.into()).unwrap()
11+
}
12+
713
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
814
pub struct Instant(Timespec);
915

1016
impl Instant {
1117
pub fn now() -> Instant {
12-
let mut time: Timespec = Timespec::zero();
13-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_MONOTONIC, &raw mut time.t) };
14-
15-
Instant(time)
18+
Instant(clock_gettime(CLOCK_MONOTONIC))
1619
}
1720

1821
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -38,15 +41,12 @@ impl SystemTime {
3841

3942
pub const MIN: SystemTime = SystemTime(Timespec::MIN);
4043

41-
pub fn new(tv_sec: i64, tv_nsec: i32) -> SystemTime {
42-
SystemTime(Timespec::new(tv_sec, tv_nsec))
44+
pub fn new(tv_sec: i64, tv_nsec: i64) -> Result<SystemTime, io::Error> {
45+
Ok(SystemTime(Timespec::new(tv_sec, tv_nsec)?))
4346
}
4447

4548
pub fn now() -> SystemTime {
46-
let mut time: Timespec = Timespec::zero();
47-
let _ = unsafe { hermit_abi::clock_gettime(CLOCK_REALTIME, &raw mut time.t) };
48-
49-
SystemTime(time)
49+
SystemTime(clock_gettime(CLOCK_REALTIME))
5050
}
5151

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

0 commit comments

Comments
 (0)