From ea10383399f5d3f5d778fe9b74163dc55c8e3d29 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Wed, 23 Nov 2022 10:50:09 -0500 Subject: [PATCH] Update use of libc::timespec to prepare for future libc version 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. --- src/sys.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/sys.rs b/src/sys.rs index 22b77e1079..c5765ca895 100644 --- a/src/sys.rs +++ b/src/sys.rs @@ -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); } @@ -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,