Skip to content

Commit 6a06308

Browse files
authored
Update use of libc::timespec to prepare for future libc version (#528)
In a future release of the `libc` crate, `libc::timespec` will contains private padding fields on 32-bit `*-linux-musl` targets and so the struct will no longer be able to be created using the literal initializer syntax. The only uses in this crate of `libc::timespec` in this way were zero initializing the struct. Thus, these can be replaced by a call to `std::mem::zeroed()` which is compatible with both current versions of the `libc` crate as well as the future version which will contain those private padding fields.
1 parent 4235bd8 commit 6a06308

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/sys.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,22 @@ mod inner {
512512
mod unix {
513513
use std::fmt;
514514
use std::cmp::Ordering;
515+
use std::mem::zeroed;
515516
use std::ops::{Add, Sub};
516517
use libc;
517518

518519
use Duration;
519520

520521
pub fn get_time() -> (i64, i32) {
521-
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
522+
// SAFETY: libc::timespec is zero initializable.
523+
let mut tv: libc::timespec = unsafe { zeroed() };
522524
unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut tv); }
523525
(tv.tv_sec as i64, tv.tv_nsec as i32)
524526
}
525527

526528
pub fn get_precise_ns() -> u64 {
527-
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
529+
// SAFETY: libc::timespec is zero initializable.
530+
let mut ts: libc::timespec = unsafe { zeroed() };
528531
unsafe {
529532
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
530533
}
@@ -552,10 +555,8 @@ mod inner {
552555
impl SteadyTime {
553556
pub fn now() -> SteadyTime {
554557
let mut t = SteadyTime {
555-
t: libc::timespec {
556-
tv_sec: 0,
557-
tv_nsec: 0,
558-
}
558+
// SAFETY: libc::timespec is zero initializable.
559+
t: unsafe { zeroed() }
559560
};
560561
unsafe {
561562
assert_eq!(0, libc::clock_gettime(libc::CLOCK_MONOTONIC,

0 commit comments

Comments
 (0)