diff options
| author | Mara Bos <m-ou.se@m-ou.se> | 2022-03-24 11:11:03 +0100 |
|---|---|---|
| committer | Mara Bos <m-ou.se@m-ou.se> | 2022-03-24 11:11:03 +0100 |
| commit | 23badeb4cb22afd09e0264348800e44cda80af38 (patch) | |
| tree | b2eaf18a47788ae20292a196a12ea4a161e9f59e /library/std/src/sys | |
| parent | 87299298d925af0943c94d2cc5bb8a2711d9f6b4 (diff) | |
| download | rust-23badeb4cb22afd09e0264348800e44cda80af38.tar.gz rust-23badeb4cb22afd09e0264348800e44cda80af38.zip | |
Make Timespec available in sys::unix.
Diffstat (limited to 'library/std/src/sys')
| -rw-r--r-- | library/std/src/sys/unix/time.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/library/std/src/sys/unix/time.rs b/library/std/src/sys/unix/time.rs index 64c249f33eb..8741e568c67 100644 --- a/library/std/src/sys/unix/time.rs +++ b/library/std/src/sys/unix/time.rs @@ -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 + } } } |
