use crate::time::timestamp::Timestamp;
use core::{
task::{Context, Poll},
time::Duration,
};
#[cfg(any(test, feature = "std"))]
mod std;
#[cfg(any(test, feature = "testing"))]
pub mod testing;
#[cfg(any(test, feature = "std"))]
pub use self::std::*;
pub trait Clock {
fn get_time(&self) -> Timestamp;
}
pub trait ClockWithTimer: Clock {
type Timer: Timer;
fn timer(&self) -> Self::Timer;
}
pub trait Timer: Sized {
#[inline]
fn ready(&mut self) -> TimerReady<Self> {
TimerReady(self)
}
fn poll_ready(&mut self, cx: &mut Context) -> Poll<()>;
fn update(&mut self, timestamp: Timestamp);
}
impl_ready_future!(Timer, TimerReady, ());
#[derive(Clone, Copy, Debug)]
pub struct NoopClock;
impl Clock for NoopClock {
fn get_time(&self) -> Timestamp {
unsafe { Timestamp::from_duration(Duration::from_micros(1)) }
}
}
impl Clock for Timestamp {
#[inline]
fn get_time(&self) -> Timestamp {
*self
}
}
pub struct Cached<'a, C: Clock> {
clock: &'a C,
cached_value: core::cell::Cell<Option<Timestamp>>,
}
impl<'a, C: Clock> Cached<'a, C> {
#[inline]
pub fn new(clock: &'a C) -> Self {
Self {
clock,
cached_value: Default::default(),
}
}
}
impl<'a, C: Clock> Clock for Cached<'a, C> {
#[inline]
fn get_time(&self) -> Timestamp {
if let Some(time) = self.cached_value.get() {
return time;
}
let now = self.clock.get_time();
self.cached_value.set(Some(now));
now
}
}