#[derive(Copy, Clone, Default, Eq, PartialEq, Hash)]
pub struct NodeId(u64);
impl NodeId {
#[inline(always)]
pub fn as_u64(self) -> u64 {
self.0
}
#[inline(always)]
pub fn from_u64(raw: u64) -> Self {
Self(raw)
}
#[inline(always)]
fn index(self) -> u32 {
self.0 as u32
}
#[inline(always)]
fn version(self) -> u32 {
(self.0 >> 32) as u32
}
}
impl Ord for NodeId {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(self.index(), self.version()).cmp(&(other.index(), other.version()))
}
}
impl PartialOrd for NodeId {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl serde::Serialize for NodeId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_u64(self.0)
}
}
impl<'de> serde::Deserialize<'de> for NodeId {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
<u64 as serde::Deserialize>::deserialize(deserializer).map(Self)
}
}
impl std::fmt::Debug for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "NodeId({}v{})", self.index(), self.version())
}
}
impl std::fmt::Display for NodeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}