43 lines
801 B
Rust
43 lines
801 B
Rust
//! Provides time related functionality for Clocks.
|
|
use std::time::{Duration, Instant};
|
|
|
|
use flax::component;
|
|
|
|
component! {
|
|
pub clock: Clock,
|
|
}
|
|
|
|
/// Measures high precision time
|
|
#[allow(dead_code)]
|
|
pub struct Clock {
|
|
start: Instant,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
impl Clock {
|
|
// Creates and starts a new clock
|
|
pub fn new() -> Self {
|
|
Clock {
|
|
start: Instant::now(),
|
|
}
|
|
}
|
|
|
|
// Returns the elapsed time
|
|
pub fn elapsed(&self) -> Duration {
|
|
Instant::now() - self.start
|
|
}
|
|
|
|
// Resets the clock and returns the elapsed time
|
|
pub fn reset(&mut self) -> Duration {
|
|
let elapsed = self.elapsed();
|
|
|
|
self.start = Instant::now();
|
|
elapsed
|
|
}
|
|
}
|
|
|
|
impl Default for Clock {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|