u_nesting_core/timing.rs
1//! WASM-compatible timing abstraction.
2//!
3//! On native targets, uses [`std::time::Instant`] for wall-clock timing.
4//! On WASM (`wasm32`), provides a no-op timer that always reports zero elapsed time.
5//! This allows algorithms to compile and run on WASM without panicking,
6//! while still supporting time-based termination on native platforms.
7
8#[cfg(not(target_arch = "wasm32"))]
9mod inner {
10 use std::time::{Duration, Instant};
11
12 /// A wall-clock timer backed by [`Instant`] on native targets.
13 #[derive(Debug, Clone, Copy)]
14 pub struct Timer(Instant);
15
16 impl Timer {
17 /// Starts the timer.
18 pub fn now() -> Self {
19 Timer(Instant::now())
20 }
21
22 /// Returns the elapsed time since the timer was started.
23 pub fn elapsed(&self) -> Duration {
24 self.0.elapsed()
25 }
26
27 /// Returns elapsed time in milliseconds.
28 pub fn elapsed_ms(&self) -> u64 {
29 self.0.elapsed().as_millis() as u64
30 }
31 }
32}
33
34#[cfg(target_arch = "wasm32")]
35mod inner {
36 use std::time::Duration;
37
38 /// A no-op timer for WASM targets.
39 ///
40 /// Always reports zero elapsed time. Algorithms terminate via
41 /// iteration limits instead of wall-clock time on WASM.
42 #[derive(Debug, Clone, Copy)]
43 pub struct Timer;
44
45 impl Timer {
46 /// No-op: returns immediately.
47 pub fn now() -> Self {
48 Timer
49 }
50
51 /// Always returns [`Duration::ZERO`].
52 pub fn elapsed(&self) -> Duration {
53 Duration::ZERO
54 }
55
56 /// Always returns `0`.
57 pub fn elapsed_ms(&self) -> u64 {
58 0
59 }
60 }
61}
62
63pub use inner::Timer;