Skip to main content

rajac_types/
method_arena.rs

1use crate::{MethodId, MethodSignature};
2
3/// Arena storage for method signatures.
4#[derive(Debug, Clone)]
5pub struct MethodArena {
6    methods: Vec<MethodSignature>,
7}
8
9impl MethodArena {
10    pub fn new() -> Self {
11        Self {
12            methods: Vec::new(),
13        }
14    }
15
16    pub fn alloc(&mut self, method: MethodSignature) -> MethodId {
17        let id = MethodId(self.methods.len() as u32);
18        self.methods.push(method);
19        id
20    }
21
22    pub fn get(&self, id: MethodId) -> &MethodSignature {
23        &self.methods[id.0 as usize]
24    }
25
26    pub fn get_mut(&mut self, id: MethodId) -> &mut MethodSignature {
27        &mut self.methods[id.0 as usize]
28    }
29
30    pub fn len(&self) -> usize {
31        self.methods.len()
32    }
33
34    pub fn is_empty(&self) -> bool {
35        self.methods.is_empty()
36    }
37}
38
39impl Default for MethodArena {
40    fn default() -> Self {
41        Self::new()
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use crate::MethodModifiers;
49    use rajac_base::shared_string::SharedString;
50
51    #[test]
52    fn allocates_and_returns_method_signatures() {
53        let mut arena = MethodArena::new();
54        let signature = MethodSignature::new(
55            SharedString::new("run"),
56            Vec::new(),
57            crate::TypeId(0),
58            MethodModifiers(MethodModifiers::PUBLIC),
59        );
60
61        let id = arena.alloc(signature.clone());
62
63        assert_eq!(arena.get(id), &signature);
64        assert_eq!(arena.len(), 1);
65    }
66}