rsjsonnet_lang/
arena.rs

1//! Arena allocator.
2//!
3//! This modules provides [`Arena`], an arena allocator that can be used to
4//! allocate values in heap that will all be freed at once when the [`Arena`]
5//! object is dropped.
6
7/// Arena allocator.
8///
9/// See the [module-level documentation](self) for more information.
10pub struct Arena {
11    bump: bumpalo::Bump,
12}
13
14impl Default for Arena {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl Arena {
21    pub fn new() -> Self {
22        Self {
23            bump: bumpalo::Bump::new(),
24        }
25    }
26
27    pub fn alloc<T: Copy>(&self, value: T) -> &T {
28        self.bump.alloc(value)
29    }
30
31    pub fn alloc_slice<T: Copy>(&self, slice: &[T]) -> &[T] {
32        self.bump.alloc_slice_copy(slice)
33    }
34
35    pub fn alloc_str(&self, value: &str) -> &str {
36        self.bump.alloc_str(value)
37    }
38}