This repository has been archived on 2026-03-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
alchemy/styles/src/stretch/id.rs

35 lines
877 B
Rust

///! This module is included while awaiting an upstream merge in stretch proper.
///! You should not rely on it, and consider it an implementation detail.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct Id {
id: u32,
generation: u32,
}
pub(crate) struct Allocator {
new_id: u32,
free_ids: Vec<Id>,
}
impl Allocator {
pub fn new() -> Self {
Allocator { new_id: 0, free_ids: Vec::new() }
}
pub fn allocate(&mut self) -> Id {
// TODO: better balancing
match self.free_ids.pop() {
Some(id) => Id { id: id.id, generation: id.generation + 1 },
None => {
let id = self.new_id;
self.new_id += 1;
Id { id, generation: 0 }
}
}
}
pub fn free(&mut self, ids: &[Id]) {
self.free_ids.extend(ids);
}
}