use std::collections::HashMap;
use cpu_time::ProcessTime;
use std::time::Duration;
pub type BreezyTimer = HashMap<&'static str, TimerState>;
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct TimerState {
process_time: ProcessTime,
total_elapsed: Duration
}
impl TimerState {
pub fn new() -> Self {
TimerState {
process_time: ProcessTime::now(),
total_elapsed: Duration::new(0, 0)
}
}
pub fn add_time(&mut self, up_to: ProcessTime){
self.total_elapsed += up_to.duration_since(self.process_time);
}
pub fn reset_time(&mut self, new_time: ProcessTime){
self.process_time = new_time;
}
pub fn get_total_elapsed(&self) -> Duration {
self.total_elapsed.clone()
}
}
pub trait Timer {
fn start(&mut self, _: &'static str) { return }
fn stop(&mut self, _: &'static str) { return }
fn elapsed(&self, _: &'static str) -> Option<Duration> { return None }
}
impl Timer for HashMap<&'static str, TimerState> {
#[cfg(feature="breezy_timer")]
fn start(&mut self, name: &'static str) {
self.entry(name)
.and_modify(|entry| entry.reset_time(ProcessTime::now()))
.or_insert(TimerState::new());
}
#[cfg(feature="breezy_timer")]
fn stop(&mut self, name: &'static str) {
let before = ProcessTime::now();
match self.get_mut(name) {
None => println!("Warning: timer {} was stopped but does not exist", name),
Some(entry) => {
entry.add_time(before);
}
}
}
#[cfg(feature="breezy_timer")]
fn elapsed(&self, name: &'static str) -> Option<Duration> {
match self.get(&name) {
None => None,
Some(ts) => Some(ts.total_elapsed)
}
}
}