use core::time::Duration;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp(u64);
impl Timestamp {
pub const fn from_micros(micros: u64) -> Self {
Self(micros)
}
pub const fn as_micros(self) -> u64 {
self.0
}
pub const fn checked_duration_since(self, earlier: Timestamp) -> Option<Duration> {
match self.0.checked_sub(earlier.0) {
Some(micros) => Some(Duration::from_micros(micros)),
None => None,
}
}
pub const fn saturating_duration_since(self, earlier: Timestamp) -> Duration {
Duration::from_micros(self.0.saturating_sub(earlier.0))
}
pub const fn checked_add(self, duration: Duration) -> Option<Timestamp> {
let micros = duration.as_nanos().div_ceil(1_000);
if micros > u64::MAX as u128 {
return None;
}
match self.0.checked_add(micros as u64) {
Some(micros) => Some(Self(micros)),
None => None,
}
}
}
pub trait Clock {
fn now(&self) -> Timestamp;
}
pub trait Delay {
fn delay(&mut self, duration: Duration);
}
#[cfg(test)]
mod tests {
use super::*;
use core::cell::Cell;
#[test]
fn timestamp_round_trips_micros() {
const T: Timestamp = Timestamp::from_micros(1_500);
assert_eq!(T.as_micros(), 1_500);
assert_eq!(Timestamp::default().as_micros(), 0);
}
#[test]
fn duration_since_is_checked_or_saturating() {
let a = Timestamp::from_micros(1_000);
let b = Timestamp::from_micros(3_500);
assert_eq!(
b.checked_duration_since(a),
Some(Duration::from_micros(2_500))
);
assert_eq!(a.checked_duration_since(b), None);
assert_eq!(a.checked_duration_since(a), Some(Duration::ZERO));
assert_eq!(b.saturating_duration_since(a), Duration::from_micros(2_500));
assert_eq!(a.saturating_duration_since(b), Duration::ZERO);
}
#[test]
fn checked_add_rounds_up_to_the_microsecond() {
let t = Timestamp::from_micros(10);
assert_eq!(
t.checked_add(Duration::from_millis(1)),
Some(Timestamp::from_micros(1_010))
);
assert_eq!(
t.checked_add(Duration::from_nanos(1)),
Some(Timestamp::from_micros(11))
);
assert_eq!(t.checked_add(Duration::ZERO), Some(t));
}
#[test]
fn checked_add_reports_overflow() {
let t = Timestamp::from_micros(u64::MAX - 1);
assert_eq!(
t.checked_add(Duration::from_micros(1)),
Some(Timestamp::from_micros(u64::MAX))
);
assert_eq!(t.checked_add(Duration::from_micros(2)), None);
assert_eq!(Timestamp::default().checked_add(Duration::MAX), None);
}
#[test]
fn timestamps_order_by_time() {
assert!(Timestamp::from_micros(1) < Timestamp::from_micros(2));
}
struct SimTime {
now: Cell<u64>,
}
impl Clock for SimTime {
fn now(&self) -> Timestamp {
Timestamp::from_micros(self.now.get())
}
}
impl Delay for SimTime {
fn delay(&mut self, duration: Duration) {
let us = duration.as_nanos().div_ceil(1_000) as u64;
self.now.set(self.now.get() + us);
}
}
fn wait_until<T: Clock + Delay>(time: &mut T, deadline: Timestamp, poll: Duration) -> u32 {
let mut polls = 0;
while time.now() < deadline {
time.delay(poll);
polls += 1;
}
polls
}
#[test]
fn clock_and_delay_compose_on_one_type() {
let mut time = SimTime { now: Cell::new(0) };
let start = time.now();
let deadline = start.checked_add(Duration::from_millis(5)).unwrap();
assert_eq!(wait_until(&mut time, deadline, Duration::from_millis(1)), 5);
assert_eq!(
time.now().checked_duration_since(start),
Some(Duration::from_millis(5))
);
}
}