use crate::NodeId;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct PackedNodeId(NodeId);
const NONE: NodeId = NodeId(usize::MAX);
impl PackedNodeId {
pub const NONE: Self = Self(NONE);
pub const fn new(id: Option<NodeId>) -> Self {
match id {
Some(id) => Self(id),
None => Self(NONE),
}
}
#[inline]
pub fn get(self) -> Option<NodeId> {
Some(self.0).filter(|&id| id != NONE)
}
}
impl From<Option<NodeId>> for PackedNodeId {
fn from(id: Option<NodeId>) -> Self {
debug_assert_ne!(id, Some(NONE), "no node takes the none id");
Self::new(id)
}
}
impl std::fmt::Debug for PackedNodeId {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.get().fmt(formatter)
}
}