cap_time_ext/
monotonic_clock.rs1#[cfg(not(windows))]
2use rustix::time::{clock_getres, ClockId};
3use std::time;
4use std::time::Duration;
5#[cfg(windows)]
6use {once_cell::sync::Lazy, winx::time::perf_counter_frequency};
7
8pub trait MonotonicClockExt {
10 type Instant;
12
13 fn now_with(&self, precision: Duration) -> Self::Instant;
18
19 fn resolution(&self) -> Duration;
21}
22
23#[cfg(not(windows))]
24impl MonotonicClockExt for cap_primitives::time::MonotonicClock {
25 type Instant = cap_primitives::time::Instant;
26
27 #[cfg(not(target_os = "wasi"))]
28 #[inline]
29 fn now_with(&self, _precision: Duration) -> Self::Instant {
30 Self::Instant::from_std(time::Instant::now())
33 }
34
35 fn resolution(&self) -> Duration {
36 let spec = clock_getres(ClockId::Realtime);
37 Duration::new(
38 spec.tv_sec.try_into().unwrap(),
39 spec.tv_nsec.try_into().unwrap(),
40 )
41 }
42}
43
44#[cfg(windows)]
45impl MonotonicClockExt for cap_primitives::time::MonotonicClock {
46 type Instant = cap_primitives::time::Instant;
47
48 #[inline]
49 fn now_with(&self, _precision: Duration) -> Self::Instant {
50 Self::Instant::from_std(time::Instant::now())
53 }
54
55 fn resolution(&self) -> Duration {
56 Duration::new(0, (*PERF_COUNTER_RES).try_into().unwrap())
57 }
58}
59
60#[cfg(windows)]
61static PERF_COUNTER_RES: Lazy<u64> =
62 Lazy::new(|| 1_000_000_000 / perf_counter_frequency().unwrap());