Skip to content

Commit

Permalink
Update use of libc::timespec to prepare for future libc version (#528)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wesleywiser committed Nov 26, 2022
1 parent 4235bd8 commit 6a06308
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,19 +512,22 @@ mod inner {
mod unix {
use std::fmt;
use std::cmp::Ordering;
use std::mem::zeroed;
use std::ops::{Add, Sub};
use libc;

use Duration;

pub fn get_time() -> (i64, i32) {
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
// SAFETY: libc::timespec is zero initializable.
let mut tv: libc::timespec = unsafe { zeroed() };
unsafe { libc::clock_gettime(libc::CLOCK_REALTIME, &mut tv); }
(tv.tv_sec as i64, tv.tv_nsec as i32)
}

pub fn get_precise_ns() -> u64 {
let mut ts = libc::timespec { tv_sec: 0, tv_nsec: 0 };
// SAFETY: libc::timespec is zero initializable.
let mut ts: libc::timespec = unsafe { zeroed() };
unsafe {
libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts);
}
Expand Down Expand Up @@ -552,10 +555,8 @@ mod inner {
impl SteadyTime {
pub fn now() -> SteadyTime {
let mut t = SteadyTime {
t: libc::timespec {
tv_sec: 0,
tv_nsec: 0,
}
// SAFETY: libc::timespec is zero initializable.
t: unsafe { zeroed() }
};
unsafe {
assert_eq!(0, libc::clock_gettime(libc::CLOCK_MONOTONIC,
Expand Down

0 comments on commit 6a06308

Please sign in to comment.