#[doc = crate::_tags!(uid)]
#[doc = crate::_doc_location!("data/id")]
#[derive(Clone, Debug)]
pub struct IdRegistry<Id, const MAX: usize> {
next: u32,
entries: [(u32, Id); MAX],
}
impl<Id: Copy, const MAX: usize> IdRegistry<Id, MAX> {
pub const fn new(default: Id) -> Self {
Self { next: 0, entries: [(0, default); MAX] }
}
pub fn intern(&mut self, native: u32, mk: impl Fn(u32) -> Id) -> Id {
for (raw, id) in self.entries.iter() {
if *raw == native {
return *id;
}
}
let id = mk(self.next);
self.next += 1;
for slot in self.entries.iter_mut() {
if slot.0 == 0 {
*slot = (native, id);
return id;
}
}
panic!("IdRegistry FULL"); }
}