Make Timespec available in sys::unix.

This commit is contained in:
Mara Bos 2022-03-24 11:11:03 +01:00
parent 87299298d9
commit 23badeb4cb

View file

@ -9,8 +9,8 @@ use crate::convert::TryInto;
const NSEC_PER_SEC: u64 = 1_000_000_000;
#[derive(Copy, Clone)]
struct Timespec {
t: libc::timespec,
pub(in crate::sys::unix) struct Timespec {
pub t: libc::timespec,
}
impl Timespec {
@ -18,7 +18,7 @@ impl Timespec {
Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }
}
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
if self >= other {
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
// to optimize it into a branchless form (see also #75545):
@ -51,7 +51,7 @@ impl Timespec {
}
}
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = other
.as_secs()
.try_into() // <- target type would be `libc::time_t`
@ -68,7 +68,7 @@ impl Timespec {
Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } })
}
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
let mut secs = other
.as_secs()
.try_into() // <- target type would be `libc::time_t`
@ -285,7 +285,7 @@ mod inner {
impl Instant {
pub fn now() -> Instant {
Instant { t: now(libc::CLOCK_MONOTONIC) }
Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
}
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@ -299,10 +299,6 @@ mod inner {
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
Some(Instant { t: self.t.checked_sub_duration(other)? })
}
pub(in crate::sys::unix) fn as_timespec(&self) -> libc::timespec {
self.t.t
}
}
impl fmt::Debug for Instant {
@ -316,7 +312,7 @@ mod inner {
impl SystemTime {
pub fn now() -> SystemTime {
SystemTime { t: now(libc::CLOCK_REALTIME) }
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
}
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
@ -352,9 +348,11 @@ mod inner {
#[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
impl Timespec {
pub 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
}
}
}