use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Value(u32);
impl Value {
pub(crate) const fn from_raw(raw: u32) -> Self {
Self(raw)
}
#[must_use]
pub const fn index(self) -> usize {
self.0 as usize
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "v{}", self.0)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Block(u32);
impl Block {
pub(crate) const fn from_raw(raw: u32) -> Self {
Self(raw)
}
#[must_use]
pub const fn index(self) -> usize {
self.0 as usize
}
}
impl fmt::Display for Block {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "b{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_display_and_index_agree() {
let v = Value::from_raw(7);
assert_eq!(v.to_string(), "v7");
assert_eq!(v.index(), 7);
}
#[test]
fn test_block_display_and_index_agree() {
let b = Block::from_raw(3);
assert_eq!(b.to_string(), "b3");
assert_eq!(b.index(), 3);
}
#[test]
fn test_handles_order_by_raw_index() {
assert!(Value::from_raw(0) < Value::from_raw(1));
assert!(Block::from_raw(2) > Block::from_raw(1));
}
}