use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Timestamp(u64);
impl Timestamp {
pub const ZERO: Timestamp = Timestamp(0);
#[inline]
#[must_use]
pub const fn from_raw(value: u64) -> Self {
Timestamp(value)
}
#[inline]
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
impl fmt::Display for Timestamp {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "@{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_zero_is_least() {
assert!(Timestamp::ZERO <= Timestamp::from_raw(0));
assert!(Timestamp::ZERO < Timestamp::from_raw(1));
}
#[test]
fn test_roundtrips_through_raw() {
for v in [0u64, 1, 2, u64::MAX] {
assert_eq!(Timestamp::from_raw(v).get(), v);
}
}
#[test]
fn test_ordering_matches_integer_ordering() {
assert!(Timestamp::from_raw(5) < Timestamp::from_raw(9));
assert!(Timestamp::from_raw(9) > Timestamp::from_raw(5));
}
#[test]
fn test_display_prefixes_at_sign() {
assert_eq!(Timestamp::from_raw(12).to_string(), "@12");
}
}