Skip to main content

hashiverse_lib/tools/time_provider/
stop_watch.rs

1//! # Stop-watch — elapsed duration against any `TimeProvider`
2//!
3//! Constructed with an `Arc<dyn TimeProvider>`, records "now" at creation, and
4//! returns the delta since then on demand. Used wherever a piece of code needs
5//! "how long did this take" in a way that plays nicely with both real and virtual
6//! clocks — e.g. PoW progress reporting and cache decimation throttles.
7
8use std::sync::Arc;
9use crate::tools::time::{DurationMillis, TimeMillis};
10use crate::tools::time_provider::time_provider::TimeProvider;
11
12pub struct StopWatch {
13    time_provider: Arc<dyn TimeProvider>,
14    start_time_millis: TimeMillis,
15}
16
17impl StopWatch {
18    pub fn new(time_provider: Arc<dyn TimeProvider>) -> Self {
19        let start_time_millis = time_provider.current_time_millis();
20        StopWatch { time_provider, start_time_millis }
21    }
22    pub fn elapsed_time_millis(&self) -> DurationMillis {
23        self.time_provider.current_time_millis() - self.start_time_millis
24    }
25}