1use bumpalo::Bump;
4use std::ops::Deref;
5
6#[derive(Default)]
22pub struct Allocator {
23 bump: Bump,
24}
25
26impl Allocator {
27 #[inline]
29 pub fn new() -> Self {
30 Self { bump: Bump::new() }
31 }
32
33 #[inline]
35 pub fn with_capacity(capacity: usize) -> Self {
36 Self {
37 bump: Bump::with_capacity(capacity),
38 }
39 }
40
41 #[inline]
43 pub fn alloc_str(&self, s: &str) -> &str {
44 self.bump.alloc_str(s)
45 }
46
47 #[inline]
51 pub fn as_bump(&self) -> &Bump {
52 &self.bump
53 }
54
55 #[inline]
60 pub fn reset(&mut self) {
61 self.bump.reset();
62 }
63
64 #[inline]
66 pub fn allocated_bytes(&self) -> usize {
67 self.bump.allocated_bytes()
68 }
69}
70
71impl Deref for Allocator {
72 type Target = Bump;
73
74 #[inline]
75 fn deref(&self) -> &Self::Target {
76 &self.bump
77 }
78}
79
80impl AsRef<Bump> for Allocator {
82 #[inline]
83 fn as_ref(&self) -> &Bump {
84 &self.bump
85 }
86}
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_allocator_new() {
94 let allocator = Allocator::new();
95 assert_eq!(allocator.allocated_bytes(), 0);
96 }
97
98 #[test]
99 fn test_allocator_default() {
100 let allocator = Allocator::default();
101 assert_eq!(allocator.allocated_bytes(), 0);
102 }
103
104 #[test]
105 fn test_alloc_str() {
106 let allocator = Allocator::new();
107 let s = allocator.alloc_str("hello world");
108 assert_eq!(s, "hello world");
109 }
110
111 #[test]
112 fn test_reset() {
113 let mut allocator = Allocator::new();
114 let _ = allocator.alloc_str("hello");
115 assert!(allocator.allocated_bytes() > 0);
116 allocator.reset();
117 }
119}