use std::collections::HashSet;
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct IdPool {
highest: u64,
unused: HashSet<u64>,
}
impl IdPool {
pub fn new() -> Self {
Self::default()
}
pub fn next(&mut self) -> u64 {
match self.unused.iter().next().copied() {
Some(id) => {
self.unused.remove(&id);
id
}
None => {
self.highest += 1;
self.highest
}
}
}
pub fn contains(&self, id: u64) -> bool {
id <= self.highest && !self.unused.contains(&id)
}
pub fn recycle(&mut self, id: u64) -> bool {
if !self.contains(id) {
return false;
}
self.unused.insert(id);
true
}
}