use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Lsn(pub u64);
impl Lsn {
pub const ZERO: Lsn = Lsn(0);
#[must_use]
pub const fn next(self) -> Lsn {
Lsn(self.0 + 1)
}
#[must_use]
pub const fn value(self) -> u64 {
self.0
}
}
impl std::fmt::Display for Lsn {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct CollectionId(pub u64);
impl CollectionId {
#[must_use]
pub const fn value(self) -> u64 {
self.0
}
}
impl std::fmt::Display for CollectionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn lsn_orders_and_increments() {
assert_eq!(Lsn::ZERO.value(), 0);
assert_eq!(Lsn::ZERO.next(), Lsn(1));
assert!(Lsn(1) < Lsn(2));
assert_eq!(Lsn(41).next().value(), 42);
}
#[test]
fn ids_display() {
assert_eq!(Lsn(7).to_string(), "7");
assert_eq!(CollectionId(3).to_string(), "3");
assert_eq!(CollectionId(3).value(), 3);
}
}