1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
use std::time::{Duration, Instant};
/// A Timer with a given duration after which it will enter into a "Ringing"
/// state. The Timer can be reset at an given time, or manually set to start
/// "Ringing" again.
pub struct Timer {
duration: Duration,
last: Instant,
}
impl Timer {
/// Creates a new Timer with a given Duration
pub fn new(duration: Duration) -> Self {
Timer {
last: Instant::now(),
duration,
}
}
/// Reset the Timer to stop ringing and wait till 'Duration' has elapsed
/// again
pub fn reset(&mut self) {
self.last = Instant::now();
}
/// Gets whether or not the Timer is "Ringing" (i.e. the given Duration has
/// elapsed since the last "reset")
pub fn ringing(&self) -> bool {
Instant::now().saturating_duration_since(self.last) > self.duration
}
/// Manually causes the Timer to enter into a "Ringing" state
pub fn ring_manual(&mut self) {
self.last -= self.duration;
}
}