diff options
author | Dan Gohman <dev@sunfishcode.online> | 2021-11-06 15:19:47 -0700 |
---|---|---|
committer | Dan Gohman <dev@sunfishcode.online> | 2021-12-02 13:03:32 -0800 |
commit | c4a149f3b74591e0694fe2a6a257cda587d5c5ec (patch) | |
tree | 495c815346a2c330ed0bb776f15a93ebb22c4472 | |
parent | abc7dc493bb60eeb418eeb1274be93f9cd04feda (diff) | |
download | rust-c4a149f3b74591e0694fe2a6a257cda587d5c5ec.tar.gz |
Port `Instant::new` to use rustix.
-rw-r--r-- | library/std/src/sys/unix/fs.rs | 18 | ||||
-rw-r--r-- | library/std/src/sys/unix/time.rs | 34 |
2 files changed, 22 insertions, 30 deletions
diff --git a/library/std/src/sys/unix/fs.rs b/library/std/src/sys/unix/fs.rs index d77e5cae3ad..35ce6105d8d 100644 --- a/library/std/src/sys/unix/fs.rs +++ b/library/std/src/sys/unix/fs.rs @@ -290,21 +290,21 @@ impl FileAttr { #[cfg(target_os = "netbsd")] impl FileAttr { pub fn modified(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_mtime as libc::time_t, tv_nsec: self.stat.st_mtimensec as libc::c_long, })) } pub fn accessed(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_atime as libc::time_t, tv_nsec: self.stat.st_atimensec as libc::c_long, })) } pub fn created(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_birthtime as libc::time_t, tv_nsec: self.stat.st_birthtimensec as libc::c_long, })) @@ -315,7 +315,7 @@ impl FileAttr { impl FileAttr { #[cfg(all(not(target_os = "vxworks"), not(target_os = "espidf")))] pub fn modified(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_mtime as libc::time_t, tv_nsec: self.stat.st_mtime_nsec as _, })) @@ -323,7 +323,7 @@ impl FileAttr { #[cfg(any(target_os = "vxworks", target_os = "espidf"))] pub fn modified(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_mtime as libc::time_t, tv_nsec: 0, })) @@ -331,7 +331,7 @@ impl FileAttr { #[cfg(all(not(target_os = "vxworks"), not(target_os = "espidf")))] pub fn accessed(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_atime as libc::time_t, tv_nsec: self.stat.st_atime_nsec as _, })) @@ -339,7 +339,7 @@ impl FileAttr { #[cfg(any(target_os = "vxworks", target_os = "espidf"))] pub fn accessed(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_atime as libc::time_t, tv_nsec: 0, })) @@ -352,7 +352,7 @@ impl FileAttr { target_os = "ios" ))] pub fn created(&self) -> io::Result<SystemTime> { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: self.stat.st_birthtime as libc::time_t, tv_nsec: self.stat.st_birthtime_nsec as libc::c_long, })) @@ -368,7 +368,7 @@ impl FileAttr { cfg_has_statx! { if let Some(ext) = &self.statx_extra_fields { return if (ext.stx_mask & libc::STATX_BTIME) != 0 { - Ok(SystemTime::from(libc::timespec { + Ok(SystemTime::from(rustix::time::Timespec { tv_sec: ext.stx_btime.tv_sec as libc::time_t, tv_nsec: ext.stx_btime.tv_nsec as _, })) diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 824283ef6c4..e35537fb119 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -10,12 +10,12 @@ const NSEC_PER_SEC: u64 = 1_000_000_000; #[derive(Copy, Clone)] struct Timespec { - t: libc::timespec, + t: rustix::time::Timespec, } impl Timespec { const fn zero() -> Timespec { - Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } } + Timespec { t: rustix::time::Timespec { tv_sec: 0, tv_nsec: 0 } } } fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> { @@ -65,7 +65,7 @@ impl Timespec { nsec -= NSEC_PER_SEC as u32; secs = secs.checked_add(1)?; } - Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } }) + Some(Timespec { t: rustix::time::Timespec { tv_sec: secs, tv_nsec: nsec as _ } }) } fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> { @@ -81,7 +81,7 @@ impl Timespec { nsec += NSEC_PER_SEC as i32; secs = secs.checked_sub(1)?; } - Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } }) + Some(Timespec { t: rustix::time::Timespec { tv_sec: secs, tv_nsec: nsec as _ } }) } } @@ -202,15 +202,15 @@ mod inner { impl From<libc::timeval> for SystemTime { fn from(t: libc::timeval) -> SystemTime { - SystemTime::from(libc::timespec { + SystemTime::from(rustix::time::Timespec { tv_sec: t.tv_sec, tv_nsec: (t.tv_usec * 1000) as libc::c_long, }) } } - impl From<libc::timespec> for SystemTime { - fn from(t: libc::timespec) -> SystemTime { + impl From<rustix::time::Timespec> for SystemTime { + fn from(t: rustix::time::Timespec) -> SystemTime { SystemTime { t: Timespec { t } } } } @@ -274,7 +274,6 @@ mod inner { #[cfg(not(any(target_os = "macos", target_os = "ios")))] mod inner { use crate::fmt; - use crate::sys::cvt; use crate::time::Duration; use super::Timespec; @@ -293,7 +292,7 @@ mod inner { impl Instant { pub fn now() -> Instant { - Instant { t: now(libc::CLOCK_MONOTONIC) } + Instant { t: now(rustix::time::ClockId::Monotonic) } } pub const fn zero() -> Instant { @@ -331,7 +330,7 @@ mod inner { impl SystemTime { pub fn now() -> SystemTime { - SystemTime { t: now(libc::CLOCK_REALTIME) } + SystemTime { t: now(rustix::time::ClockId::Realtime) } } pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> { @@ -347,8 +346,8 @@ mod inner { } } - impl From<libc::timespec> for SystemTime { - fn from(t: libc::timespec) -> SystemTime { + impl From<rustix::time::Timespec> for SystemTime { + fn from(t: rustix::time::Timespec) -> SystemTime { SystemTime { t: Timespec { t } } } } @@ -362,14 +361,7 @@ mod inner { } } - #[cfg(not(any(target_os = "dragonfly", target_os = "espidf")))] - pub type clock_t = libc::c_int; - #[cfg(any(target_os = "dragonfly", target_os = "espidf"))] - pub type clock_t = libc::c_ulong; - - fn now(clock: clock_t) -> Timespec { - let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }; - cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap(); - t + fn now(clock: rustix::time::ClockId) -> Timespec { + Timespec { t: rustix::time::clock_gettime(clock) } } } |