pub trait Clock {
fn now_ms(&self) -> u64;
}
pub trait KorriTimer: Clock {
async fn delay_ms(&mut self, millis: u32);
}
#[cfg(feature = "tokio")]
pub struct TokioTimer {
start: tokio::time::Instant,
}
#[cfg(feature = "tokio")]
impl TokioTimer {
pub fn new() -> Self {
Self {
start: tokio::time::Instant::now(),
}
}
}
#[cfg(feature = "tokio")]
impl Default for TokioTimer {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "tokio")]
impl Clock for TokioTimer {
fn now_ms(&self) -> u64 {
self.start.elapsed().as_millis() as u64
}
}
#[cfg(feature = "tokio")]
impl KorriTimer for TokioTimer {
async fn delay_ms(&mut self, millis: u32) {
tokio::time::sleep(tokio::time::Duration::from_millis(millis as u64)).await;
}
}