use serde::{Deserialize, Serialize};
pub mod mem;
pub mod persistent;
pub mod repository;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Time(u64);
impl Time {
#[must_use]
pub fn from(value: u64) -> Self {
Self(value)
}
#[must_use]
pub fn value(self) -> u64 {
self.0
}
}
pub trait Clock {
type IncrementError: std::error::Error;
type WitnessError: std::error::Error;
fn time(&self) -> Time;
fn increment(&mut self) -> Result<Time, Self::IncrementError>;
fn witness(&mut self, other: Time) -> Result<(), Self::WitnessError>;
}
#[cfg(test)]
pub mod test {
use super::{Clock, Time};
pub fn test_clock<C>(mut clock: C)
where
C: Clock,
{
assert_eq!(Time(1), clock.time());
let time = clock.increment().unwrap();
assert_eq!(Time(1), time);
assert_eq!(Time(2), clock.time());
clock.witness(Time(42)).unwrap();
assert_eq!(Time(42), clock.time());
clock.witness(Time(42)).unwrap();
assert_eq!(Time(42), clock.time());
clock.witness(Time(30)).unwrap();
assert_eq!(Time(42), clock.time());
}
}