Fix timer.rs cross-platform compilation

* macOS does not have RUSAGE_THREAD
* tv_sec and tv_usec may be i32 instead of i64
This commit is contained in:
Mahmoud Al-Qudsi 2023-02-14 16:25:53 -06:00
parent a1a8bc3d8d
commit b5ff175b45
2 changed files with 2 additions and 3 deletions

View File

@ -3,7 +3,7 @@
use std::time::Duration; use std::time::Duration;
pub const fn timeval_to_duration(val: &libc::timeval) -> Duration { pub const fn timeval_to_duration(val: &libc::timeval) -> Duration {
let micros = val.tv_sec * (1E6 as i64) + val.tv_usec; let micros = val.tv_sec as i64 * (1E6 as i64) + val.tv_usec as i64;
Duration::from_micros(micros as u64) Duration::from_micros(micros as u64)
} }

View File

@ -68,10 +68,10 @@ fn push_timer_ffi(enabled: bool) -> Box<PrintElapsedOnDropFfi> {
} }
/// An enumeration of supported libc rusage types used by [`getrusage()`]. /// An enumeration of supported libc rusage types used by [`getrusage()`].
/// NB: RUSAGE_THREAD is not supported on macOS.
enum RUsage { enum RUsage {
RSelf, // "Self" is a reserved keyword RSelf, // "Self" is a reserved keyword
RChildren, RChildren,
RThread,
} }
/// A safe wrapper around `libc::getrusage()` /// A safe wrapper around `libc::getrusage()`
@ -81,7 +81,6 @@ fn getrusage(resource: RUsage) -> libc::rusage {
match resource { match resource {
RUsage::RSelf => libc::getrusage(libc::RUSAGE_SELF, rusage.as_mut_ptr()), RUsage::RSelf => libc::getrusage(libc::RUSAGE_SELF, rusage.as_mut_ptr()),
RUsage::RChildren => libc::getrusage(libc::RUSAGE_CHILDREN, rusage.as_mut_ptr()), RUsage::RChildren => libc::getrusage(libc::RUSAGE_CHILDREN, rusage.as_mut_ptr()),
RUsage::RThread => libc::getrusage(libc::RUSAGE_THREAD, rusage.as_mut_ptr()),
} }
}; };