Skip to main content

rajac_types/
type_arena.rs

1use crate::{Type, TypeId};
2
3#[derive(Debug, Clone)]
4pub struct TypeArena {
5    types: Vec<Type>,
6}
7
8impl TypeArena {
9    pub fn new() -> Self {
10        Self { types: Vec::new() }
11    }
12
13    pub fn alloc(&mut self, ty: Type) -> TypeId {
14        let id = TypeId(self.types.len() as u32);
15        self.types.push(ty);
16        id
17    }
18
19    pub fn get(&self, id: TypeId) -> &Type {
20        &self.types[id.0 as usize]
21    }
22
23    pub fn get_mut(&mut self, id: TypeId) -> &mut Type {
24        &mut self.types[id.0 as usize]
25    }
26
27    pub fn len(&self) -> usize {
28        self.types.len()
29    }
30
31    pub fn is_empty(&self) -> bool {
32        self.types.is_empty()
33    }
34}
35
36impl Default for TypeArena {
37    fn default() -> Self {
38        Self::new()
39    }
40}