1
0
Fork 0
forked from wry/wry

all: fetch current time only once per iteration

This commit is contained in:
Julian Orth 2024-07-11 17:39:18 +02:00
parent d8d6be1ef3
commit bb9e6ba3b5
21 changed files with 99 additions and 97 deletions

View file

@ -5,16 +5,9 @@ use {
ops::{Add, Sub},
time::Duration,
},
thiserror::Error,
uapi::c,
};
#[derive(Debug, Error)]
pub enum TimeError {
#[error("clock_gettime failed: {0}")]
ClockGettime(crate::utils::oserror::OsError),
}
#[derive(Copy, Clone)]
pub struct Time(pub c::timespec);
@ -28,20 +21,6 @@ impl Debug for Time {
}
impl Time {
pub fn now() -> Result<Time, TimeError> {
let mut time = uapi::pod_zeroed();
if let Err(e) = uapi::clock_gettime(c::CLOCK_MONOTONIC, &mut time) {
return Err(TimeError::ClockGettime(e.into()));
}
Ok(Self(time))
}
pub fn in_ms(ms: u64) -> Result<Time, TimeError> {
let now = Self::now()?;
Ok(now + Duration::from_millis(ms))
}
#[allow(dead_code)]
pub fn now_unchecked() -> Time {
let mut time = uapi::pod_zeroed();
let _ = uapi::clock_gettime(c::CLOCK_MONOTONIC, &mut time);
@ -73,6 +52,12 @@ impl Time {
let nsec = self.0.tv_nsec as u64 / 1_000;
sec + nsec
}
pub fn msec(self) -> u64 {
let sec = self.0.tv_sec as u64 * 1_000;
let nsec = self.0.tv_nsec as u64 / 1_000_000;
sec + nsec
}
}
impl Eq for Time {}
@ -124,14 +109,6 @@ impl Add<Duration> for Time {
}
}
pub fn now_nsec() -> u64 {
Time::now_unchecked().nsec()
}
pub fn now_usec() -> u64 {
Time::now_unchecked().usec()
}
pub fn usec_to_msec(usec: u64) -> u32 {
(usec / 1000) as u32
}