#![forbid(unsafe_code)]
use futures::future::FusedFuture;
use std::time::Duration;
use wgame_app::{
runtime::sleep_until,
sleep,
time::{Instant, Timer},
};
pub struct PeriodicTimer {
timer: Timer,
period: Duration,
}
impl PeriodicTimer {
pub fn new(period: Duration) -> Self {
assert!(!period.is_zero(), "Timer period must be positive");
Self {
timer: sleep(period),
period,
}
}
pub fn period(&self) -> Duration {
self.period
}
pub fn elapsed_periods(&mut self) -> Duration {
if self.timer.is_terminated() {
let now = Instant::now();
let (next_timestamp, elapsed) = advance(self.timer.timestamp(), now, self.period);
self.timer = sleep_until(next_timestamp);
elapsed
} else {
Duration::ZERO
}
}
pub async fn wait_next(&mut self) -> Duration {
(&mut self.timer).await;
self.elapsed_periods()
}
}
fn advance(deadline: Instant, now: Instant, period: Duration) -> (Instant, Duration) {
let late = now - deadline;
let rem = late.as_nanos() % period.as_nanos();
let remainder = Duration::new((rem / 1_000_000_000) as u64, (rem % 1_000_000_000) as u32);
let elapsed = late - remainder + period;
(deadline + elapsed, elapsed)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn periodic_deadlines_keep_phase_and_count_missed_ticks() {
let start = Instant::now();
let period = Duration::from_millis(10);
assert_eq!(advance(start, start, period), (start + period, period));
assert_eq!(
advance(start, start + Duration::from_millis(25), period),
(start + Duration::from_millis(30), Duration::from_millis(30))
);
}
}