use std::hash::Hash;
#[derive(Clone, Copy, Hash, Eq, PartialEq)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub struct Id(u64);
impl Id {
pub(crate) fn background() -> Self {
Self(0)
}
pub fn new(source: impl Hash) -> Id {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
let mut hasher = DefaultHasher::default();
source.hash(&mut hasher);
Id(hasher.finish())
}
pub fn with(self, child: impl Hash) -> Id {
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
let mut hasher = DefaultHasher::default();
hasher.write_u64(self.0);
child.hash(&mut hasher);
Id(hasher.finish())
}
pub(crate) fn short_debug_format(&self) -> String {
format!("{:04X}", self.0 as u16)
}
#[inline(always)]
pub(crate) fn value(&self) -> u64 {
self.0
}
}
impl std::fmt::Debug for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:X}", self.0)
}
}