use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
pub struct Lsn(pub u64);
impl Lsn {
pub const NONE: Lsn = Lsn(0);
pub const FIRST: Lsn = Lsn(1);
#[inline]
#[must_use]
pub const fn next(self) -> Lsn {
Lsn(self.0.saturating_add(1))
}
#[inline]
#[must_use]
pub const fn is_none(self) -> bool {
self.0 == 0
}
}
impl fmt::Display for Lsn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn none_and_first() {
assert_eq!(Lsn::NONE, Lsn(0));
assert_eq!(Lsn::FIRST, Lsn(1));
assert!(Lsn::NONE.is_none());
assert!(!Lsn::FIRST.is_none());
}
#[test]
fn next_is_monotone_and_dense() {
assert_eq!(Lsn::NONE.next(), Lsn::FIRST);
assert_eq!(Lsn(41).next(), Lsn(42));
}
#[test]
fn next_saturates_without_panicking() {
assert_eq!(Lsn(u64::MAX).next(), Lsn(u64::MAX));
}
#[test]
fn ordering() {
assert!(Lsn(1) < Lsn(2));
assert!(Lsn::NONE < Lsn::FIRST);
}
#[test]
fn display_is_the_underlying_integer() {
assert_eq!(Lsn(0).to_string(), "0");
assert_eq!(Lsn(100001).to_string(), "100001");
}
}