Skip to content

Commit 23badeb

Browse files
committed
Make Timespec available in sys::unix.
1 parent 8729929 commit 23badeb

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

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

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ use crate::convert::TryInto;
99
const NSEC_PER_SEC: u64 = 1_000_000_000;
1010

1111
#[derive(Copy, Clone)]
12-
struct Timespec {
13-
t: libc::timespec,
12+
pub(in crate::sys::unix) struct Timespec {
13+
pub t: libc::timespec,
1414
}
1515

1616
impl Timespec {
1717
const fn zero() -> Timespec {
1818
Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } }
1919
}
2020

21-
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
21+
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
2222
if self >= other {
2323
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
2424
// to optimize it into a branchless form (see also #75545):
@@ -51,7 +51,7 @@ impl Timespec {
5151
}
5252
}
5353

54-
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
54+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
5555
let mut secs = other
5656
.as_secs()
5757
.try_into() // <- target type would be `libc::time_t`
@@ -68,7 +68,7 @@ impl Timespec {
6868
Some(Timespec { t: libc::timespec { tv_sec: secs, tv_nsec: nsec as _ } })
6969
}
7070

71-
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
71+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
7272
let mut secs = other
7373
.as_secs()
7474
.try_into() // <- target type would be `libc::time_t`
@@ -285,7 +285,7 @@ mod inner {
285285

286286
impl Instant {
287287
pub fn now() -> Instant {
288-
Instant { t: now(libc::CLOCK_MONOTONIC) }
288+
Instant { t: Timespec::now(libc::CLOCK_MONOTONIC) }
289289
}
290290

291291
pub fn checked_sub_instant(&self, other: &Instant) -> Option<Duration> {
@@ -299,10 +299,6 @@ mod inner {
299299
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Instant> {
300300
Some(Instant { t: self.t.checked_sub_duration(other)? })
301301
}
302-
303-
pub(in crate::sys::unix) fn as_timespec(&self) -> libc::timespec {
304-
self.t.t
305-
}
306302
}
307303

308304
impl fmt::Debug for Instant {
@@ -316,7 +312,7 @@ mod inner {
316312

317313
impl SystemTime {
318314
pub fn now() -> SystemTime {
319-
SystemTime { t: now(libc::CLOCK_REALTIME) }
315+
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
320316
}
321317

322318
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
@@ -352,9 +348,11 @@ mod inner {
352348
#[cfg(any(target_os = "dragonfly", target_os = "espidf"))]
353349
pub type clock_t = libc::c_ulong;
354350

355-
fn now(clock: clock_t) -> Timespec {
356-
let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } };
357-
cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap();
358-
t
351+
impl Timespec {
352+
pub fn now(clock: clock_t) -> Timespec {
353+
let mut t = Timespec { t: libc::timespec { tv_sec: 0, tv_nsec: 0 } };
354+
cvt(unsafe { libc::clock_gettime(clock, &mut t.t) }).unwrap();
355+
t
356+
}
359357
}
360358
}

0 commit comments

Comments
 (0)