use std::time::{Instant, SystemTime};
#[must_use]
#[inline]
#[expect(
clippy::disallowed_methods,
reason = "the probe clock is the real clock unless a harness installs its virtual one"
)]
pub fn now() -> Instant {
#[cfg(feature = "control")]
if let Some(hooks) = hooks::get() {
return (hooks.now)();
}
Instant::now()
}
#[must_use]
#[inline]
#[expect(
clippy::disallowed_methods,
reason = "the probe clock is the real clock unless a harness installs its virtual one"
)]
pub fn system_now() -> SystemTime {
#[cfg(feature = "control")]
if let Some(hooks) = hooks::get() {
return (hooks.system_now)();
}
SystemTime::now()
}
#[inline]
pub fn real_timer(component: &'static str) {
#[cfg(feature = "control")]
if let Some(controller) = crate::control::current() {
controller.real_timer(component);
}
#[cfg(not(feature = "control"))]
let _ = component;
}
#[cfg(feature = "control")]
pub use hooks::install;
#[cfg(feature = "control")]
mod hooks {
use std::sync::{PoisonError, RwLock};
use std::time::{Instant, SystemTime};
#[derive(Clone, Copy)]
pub(super) struct Hooks {
pub(super) now: fn() -> Instant,
pub(super) system_now: fn() -> SystemTime,
}
static HOOKS: RwLock<Option<Hooks>> = RwLock::new(None);
#[doc(hidden)]
pub fn install(now: fn() -> Instant, system_now: fn() -> SystemTime) {
*HOOKS.write().unwrap_or_else(PoisonError::into_inner) = Some(Hooks { now, system_now });
}
pub(super) fn get() -> Option<Hooks> {
*HOOKS.read().unwrap_or_else(PoisonError::into_inner)
}
}