1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 23:13:13 -04:00
Files
egui/crates/eframe/src/stopwatch.rs
Emil Ernerfeldt 8d98763fe1 Replace #[allow attributes with expect (#7796)
We do have `clippy::allow_attributes` turned on, but it doesn't seem to
work properly
2025-12-19 20:55:50 +01:00

51 lines
1.2 KiB
Rust

#![allow(clippy::allow_attributes, dead_code)] // not used on all platforms
use web_time::Instant;
pub struct Stopwatch {
total_time_ns: u128,
/// None = not running
start: Option<Instant>,
}
impl Stopwatch {
pub fn new() -> Self {
Self {
total_time_ns: 0,
start: None,
}
}
pub fn start(&mut self) {
assert!(self.start.is_none(), "Stopwatch already running");
self.start = Some(Instant::now());
}
pub fn pause(&mut self) {
let start = self.start.take().expect("Stopwatch is not running");
let duration = start.elapsed();
self.total_time_ns += duration.as_nanos();
}
pub fn resume(&mut self) {
assert!(self.start.is_none(), "Stopwatch still running");
self.start = Some(Instant::now());
}
pub fn total_time_ns(&self) -> u128 {
if let Some(start) = self.start {
// Running
let duration = start.elapsed();
self.total_time_ns + duration.as_nanos()
} else {
// Paused
self.total_time_ns
}
}
pub fn total_time_sec(&self) -> f32 {
self.total_time_ns() as f32 * 1e-9
}
}