#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NodeId(pub(crate) u32);
impl NodeId {
pub fn as_u32(self) -> u32 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PortId(pub(crate) u32);
impl PortId {
pub fn as_u32(self) -> u32 {
self.0
}
}
impl From<usize> for NodeId {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<u32> for NodeId {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<usize> for PortId {
fn from(value: usize) -> Self {
Self(value as u32)
}
}
impl From<u32> for PortId {
fn from(value: u32) -> Self {
Self(value)
}
}
impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Node#{}", self.0)
}
}
impl std::fmt::Display for PortId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Port#{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn node_id_from_usize() {
let id: NodeId = 3usize.into();
assert_eq!(id, NodeId(3));
}
#[test]
fn node_id_from_u32() {
let id: NodeId = 42u32.into();
assert_eq!(id, NodeId(42));
}
#[test]
fn port_id_from_usize() {
let id: PortId = 1usize.into();
assert_eq!(id, PortId(1));
}
#[test]
fn port_id_from_u32() {
let id: PortId = 7u32.into();
assert_eq!(id, PortId(7));
}
#[test]
fn equality() {
assert_eq!(NodeId(5), NodeId(5));
assert_ne!(NodeId(5), NodeId(6));
assert_eq!(PortId(2), PortId(2));
assert_ne!(PortId(2), PortId(3));
}
#[test]
fn ordering() {
let mut ids = [NodeId(3), NodeId(1), NodeId(2)];
ids.sort();
assert_eq!(ids, [NodeId(1), NodeId(2), NodeId(3)]);
let mut ports = [PortId(9), PortId(0), PortId(4)];
ports.sort();
assert_eq!(ports, [PortId(0), PortId(4), PortId(9)]);
}
#[test]
fn node_id_display() {
assert_eq!(format!("{}", NodeId(3)), "Node#3");
assert_eq!(format!("{}", NodeId(0)), "Node#0");
}
#[test]
fn port_id_display() {
assert_eq!(format!("{}", PortId(1)), "Port#1");
assert_eq!(format!("{}", PortId(12)), "Port#12");
}
}