#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Ticks(u64);
impl Ticks {
pub const ZERO: Self = Self(0);
#[must_use]
pub const fn from_raw(raw: u64) -> Self {
Self(raw)
}
#[must_use]
pub const fn as_raw(self) -> u64 {
self.0
}
#[must_use]
pub const fn wrapping_sub(self, earlier: Self) -> u64 {
self.0.wrapping_sub(earlier.0)
}
}
#[cfg(test)]
mod tests {
use super::Ticks;
#[test]
fn wrapping_sub_handles_counter_rollover() {
let before_wrap = Ticks::from_raw(u64::MAX - 5);
let after_wrap = Ticks::from_raw(4);
let elapsed = after_wrap.wrapping_sub(before_wrap);
assert_eq!(
elapsed, 10,
"5 ticks to wrap plus 5 ticks past it, via modular arithmetic"
);
}
#[test]
fn wrapping_sub_is_zero_for_identical_reads() {
let reading = Ticks::from_raw(24_000_000);
assert_eq!(reading.wrapping_sub(reading), 0);
}
}