use crate::platform::MaybeSend;
pub trait Clock: Clone + MaybeSend + 'static {
fn now_ms(&self) -> u64;
}
#[cfg(any(test, feature = "testing"))]
#[derive(Debug, Default)]
pub struct MockClock {
inner: std::cell::Cell<u64>,
}
#[cfg(any(test, feature = "testing"))]
impl MockClock {
pub fn new(initial_ms: u64) -> Self {
Self {
inner: std::cell::Cell::new(initial_ms),
}
}
pub fn advance(&self, delta_ms: u64) {
self.inner.set(self.inner.get() + delta_ms);
}
pub fn set(&self, ms: u64) {
self.inner.set(ms);
}
}
#[cfg(any(test, feature = "testing"))]
impl Clone for MockClock {
fn clone(&self) -> Self {
Self {
inner: std::cell::Cell::new(self.inner.get()),
}
}
}
#[cfg(any(test, feature = "testing"))]
impl Clock for MockClock {
fn now_ms(&self) -> u64 {
self.inner.get()
}
}