use std::{time};
pub enum ClockState {
Finish,
Progress
}
#[derive(Clone, Copy)]
pub struct Clock {
pub cooldown: time::Duration,
pub instant: time::Instant
}
impl Clock {
pub fn new(micros: u64) -> Self {
Self {
cooldown: time::Duration::from_micros(micros),
instant: time::Instant::now()
}
}
pub fn reset(&mut self) -> &mut Self {
self.instant = time::Instant::now();
self
}
pub fn try_reset(&mut self) -> bool {
if *self == ClockState::Finish {
self.reset();
true
} else {
false
}
}
}
impl PartialEq<ClockState> for Clock {
fn eq(&self, state: &ClockState) -> bool {
let elapsed = self.instant.elapsed().as_micros();
let cooldown = self.cooldown.as_micros();
match state {
ClockState::Finish => elapsed >= cooldown,
ClockState::Progress => elapsed < cooldown,
}
}
}