ogeom_core/clock.rs
1//! A monotonic clock for the kernel's diagnostic timings, on every target.
2//!
3//! [`Instant`] is `std::time::Instant` wherever the standard library has a
4//! clock: native targets and WASI. On `wasm32-unknown-unknown` the standard
5//! library has none and `std::time::Instant::now` panics there, so this one
6//! reads as a fixed point and every duration is zero. With the `web` feature
7//! it reads the browser's `performance.now()` instead.
8//!
9//! Timings are diagnostics. No answer of any operation depends on them, so a
10//! clock that reads zero changes what a report says about time and nothing
11//! else. `std::time::Instant::now` is refused by clippy
12//! (`disallowed-methods` in `clippy.toml`) in the kernel's crates.
13
14use std::time::Duration;
15
16#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
17type Inner = std::time::Instant;
18
19#[cfg(all(target_arch = "wasm32", target_os = "unknown", feature = "web"))]
20type Inner = web_time::Instant;
21
22/// No clock: every instant is the same instant.
23#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "web")))]
24#[derive(Clone, Copy, Debug)]
25struct Inner;
26
27#[cfg(all(target_arch = "wasm32", target_os = "unknown", not(feature = "web")))]
28impl Inner {
29 fn now() -> Self {
30 Self
31 }
32
33 fn elapsed(self) -> Duration {
34 Duration::ZERO
35 }
36}
37
38/// A point in time, measured by the platform's monotonic clock where it has
39/// one.
40#[derive(Clone, Copy, Debug)]
41pub struct Instant(Inner);
42
43impl Instant {
44 /// Now, or a fixed point where the platform has no clock.
45 #[must_use]
46 #[inline]
47 #[allow(
48 clippy::disallowed_methods,
49 reason = "the one place the platform clock is read"
50 )]
51 pub fn now() -> Self {
52 Self(Inner::now())
53 }
54
55 /// Time since `self`; zero where the platform has no clock.
56 #[must_use]
57 #[inline]
58 pub fn elapsed(&self) -> Duration {
59 self.0.elapsed()
60 }
61}