#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(transparent)]
pub struct NodeId(u32);
impl NodeId {
#[inline]
pub const fn new(id: u32) -> Self {
Self(id)
}
#[inline]
pub const fn from_le_bytes(bytes: [u8; 4]) -> Self {
Self(u32::from_le_bytes(bytes))
}
#[inline]
pub const fn from_be_bytes(bytes: [u8; 4]) -> Self {
Self(u32::from_be_bytes(bytes))
}
#[inline]
pub const fn as_u32(self) -> u32 {
self.0
}
#[inline]
pub const fn to_le_bytes(self) -> [u8; 4] {
self.0.to_le_bytes()
}
#[inline]
pub const fn to_be_bytes(self) -> [u8; 4] {
self.0.to_be_bytes()
}
#[inline]
pub const fn is_null(self) -> bool {
self.0 == 0
}
pub const NULL: Self = Self(0);
}
impl From<u32> for NodeId {
#[inline]
fn from(id: u32) -> Self {
Self(id)
}
}
impl From<NodeId> for u32 {
#[inline]
fn from(id: NodeId) -> Self {
id.0
}
}
#[cfg(feature = "std")]
impl core::fmt::Display for NodeId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:08X}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_node_id_roundtrip() {
let id = NodeId::new(0xDEADBEEF);
let bytes = id.to_le_bytes();
let recovered = NodeId::from_le_bytes(bytes);
assert_eq!(id, recovered);
}
#[test]
fn test_node_id_null() {
assert!(NodeId::NULL.is_null());
assert!(NodeId::new(0).is_null());
assert!(!NodeId::new(1).is_null());
}
}