Skip to main content

u_nesting_core/
timing.rs

1//! WASM-compatible timing abstraction.
2//!
3//! Wall-clock timing backed by [`web_time::Instant`], which transparently maps
4//! to [`std::time::Instant`] on native targets and to `performance.now()` on
5//! `wasm32`. This keeps time-based termination (`time_limit_ms`) working on both
6//! native and WASM — iteration limits remain the safety net that bounds every
7//! algorithm regardless of clock resolution.
8//!
9//! A previous WASM build used a no-op timer that always reported zero elapsed
10//! time; that silently disabled `time_limit_ms` on WASM, so strategies ran to
11//! their full iteration cap (multi-second freezes in the browser).
12
13mod inner {
14    use web_time::{Duration, Instant};
15
16    /// A wall-clock timer backed by [`web_time::Instant`].
17    ///
18    /// On native targets this is exactly [`std::time::Instant`]; on `wasm32` it
19    /// uses the JS `performance.now()` clock via `web-time`.
20    #[derive(Debug, Clone, Copy)]
21    pub struct Timer(Instant);
22
23    impl Timer {
24        /// Starts the timer.
25        pub fn now() -> Self {
26            Timer(Instant::now())
27        }
28
29        /// Returns the elapsed time since the timer was started.
30        pub fn elapsed(&self) -> Duration {
31            self.0.elapsed()
32        }
33
34        /// Returns elapsed time in milliseconds.
35        pub fn elapsed_ms(&self) -> u64 {
36            self.0.elapsed().as_millis() as u64
37        }
38    }
39}
40
41pub use inner::Timer;