diff --git a/Cargo.toml b/Cargo.toml index 3d8057b5c837..d6def0588d25 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,7 +46,7 @@ libc = "0.2.103" libseat = { version = "0.2.1", optional = true, default_features = false } libloading = { version="0.8.0", optional = true } nix = { version = "0.27.0" } -rustix = { version = "0.38.18", features = ["event", "fs", "mm", "net"] } +rustix = { version = "0.38.18", features = ["event", "fs", "mm", "net", "time"] } once_cell = "1.8.0" rand = "0.8.4" scopeguard = { version = "1.1.0", optional = true } diff --git a/anvil/src/state.rs b/anvil/src/state.rs index 8732ad9cab1d..41b64ac046c3 100644 --- a/anvil/src/state.rs +++ b/anvil/src/state.rs @@ -511,7 +511,7 @@ impl AnvilState { ) -> AnvilState { let dh = display.handle(); - let clock = Clock::new().expect("failed to initialize clock"); + let clock = Clock::new(); // init wayland clients let socket_name = if listen_on_socket { diff --git a/src/desktop/wayland/utils.rs b/src/desktop/wayland/utils.rs index 3a0499900550..03db92169af7 100644 --- a/src/desktop/wayland/utils.rs +++ b/src/desktop/wayland/utils.rs @@ -404,7 +404,7 @@ impl OutputPresentationFeedback { { let time = time.into(); let refresh = refresh.into(); - let clk_id = Kind::id() as u32; + let clk_id = Kind::ID as u32; if let Some(output) = self.output.upgrade() { for mut callback in self.callbacks.drain(..) { callback.presented(&output, clk_id, time, refresh, seq, flags); diff --git a/src/utils/clock.rs b/src/utils/clock.rs index 475a01801425..fd35062e7f99 100644 --- a/src/utils/clock.rs +++ b/src/utils/clock.rs @@ -1,4 +1,5 @@ -use std::{cmp::Ordering, marker::PhantomData, mem::MaybeUninit, time::Duration}; +use rustix::time::{ClockId, Timespec}; +use std::{cmp::Ordering, marker::PhantomData, time::Duration}; /// Marker for clock source that never returns a negative [`Time`] pub trait NonNegativeClockSource: ClockSource {} @@ -8,9 +9,7 @@ pub trait NonNegativeClockSource: ClockSource {} pub struct Monotonic; impl ClockSource for Monotonic { - fn id() -> libc::clockid_t { - libc::CLOCK_MONOTONIC - } + const ID: ClockId = ClockId::Monotonic; } impl NonNegativeClockSource for Monotonic {} @@ -20,51 +19,41 @@ impl NonNegativeClockSource for Monotonic {} pub struct Realtime; impl ClockSource for Realtime { - fn id() -> libc::clockid_t { - libc::CLOCK_REALTIME - } + const ID: ClockId = ClockId::Realtime; } /// Id for a clock according to unix clockid_t pub trait ClockSource { /// Gets the id of the clock source - fn id() -> libc::clockid_t; + const ID: ClockId; } /// Defines a clock with a specific kind #[derive(Debug)] -pub struct Clock { - clk_id: libc::clockid_t, +pub struct Clock { _kind: PhantomData, } impl Clock { /// Initialize a new clock - pub fn new() -> std::io::Result { - let clk_id = Kind::id(); - clock_get_time(clk_id)?; - Ok(Clock { - clk_id, - _kind: PhantomData, - }) + pub fn new() -> Self { + Clock { _kind: PhantomData } } /// Returns the current time pub fn now(&self) -> Time { - clock_get_time(self.clk_id) - .expect("failed to get clock time") - .into() + rustix::time::clock_gettime(Kind::ID).into() } /// Gets the id of the clock - pub fn id(&self) -> libc::clockid_t { - Kind::id() + pub fn id(&self) -> ClockId { + Kind::ID } } /// A point in time for a clock with a specific kind pub struct Time { - tp: libc::timespec, + tp: Timespec, _kind: PhantomData, } @@ -128,7 +117,7 @@ impl Ord for Time { impl From for Time { fn from(tp: Duration) -> Self { - let tp = libc::timespec { + let tp = Timespec { tv_sec: tp.as_secs() as libc::time_t, #[cfg(all(target_arch = "x86_64", target_pointer_width = "32"))] tv_nsec: tp.subsec_nanos() as i64, @@ -142,8 +131,8 @@ impl From for Time { } } -impl From for Time { - fn from(tp: libc::timespec) -> Self { +impl From for Time { + fn from(tp: Timespec) -> Self { Time { tp, _kind: PhantomData, @@ -157,7 +146,7 @@ const NANOS_PER_SEC: i64 = 1_000_000_000; #[cfg(not(all(target_arch = "x86_64", target_pointer_width = "32")))] const NANOS_PER_SEC: std::os::raw::c_long = 1_000_000_000; -fn saturating_sub_timespec(lhs: libc::timespec, rhs: libc::timespec) -> Option { +fn saturating_sub_timespec(lhs: Timespec, rhs: Timespec) -> Option { if let Some(mut secs) = lhs.tv_sec.checked_sub(rhs.tv_sec) { let nanos = if lhs.tv_nsec >= rhs.tv_nsec { lhs.tv_nsec - rhs.tv_nsec @@ -174,19 +163,6 @@ fn saturating_sub_timespec(lhs: libc::timespec, rhs: libc::timespec) -> Option Result { - let mut tp = MaybeUninit::zeroed(); - unsafe { - let res = libc::clock_gettime(clk_id, tp.as_mut_ptr()); - - if res < 0 { - return Err(std::io::Error::last_os_error()); - } - - Ok(tp.assume_init()) - } -} - #[cfg(test)] mod test { use std::time::Duration; @@ -195,7 +171,7 @@ mod test { #[test] fn monotonic() { - let clock_source: Clock = Clock::new().unwrap(); + let clock_source: Clock = Clock::new(); let now = clock_source.now(); let zero = Time::::from(Duration::ZERO); assert_eq!(Time::::elapsed(&zero, now), now.into());