#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct WidgetId(pub u64);
const FNV_OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
const FNV_PRIME: u64 = 0x0000_0100_0000_01b3;
fn fnv1a(mut hash: u64, bytes: &[u8]) -> u64 {
for &b in bytes {
hash ^= u64::from(b);
hash = hash.wrapping_mul(FNV_PRIME);
}
hash
}
impl WidgetId {
pub const ROOT: Self = Self(FNV_OFFSET);
pub fn child(self, index: usize, key: Option<&str>) -> Self {
let hash = fnv1a(FNV_OFFSET, &self.0.to_le_bytes());
let hash = match key {
Some(key) => fnv1a(fnv1a(hash, &[0x4b]), key.as_bytes()),
None => fnv1a(fnv1a(hash, &[0x49]), &(index as u64).to_le_bytes()),
};
Self(hash)
}
}