1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use bumpalo::Bump;
use core::fmt::Debug;
use core::hash::Hash;
use core::ops::Deref;
use core::ops::DerefMut;
use hashbrown::hash_map::DefaultHashBuilder;
use hashbrown::BumpWrapper;
#[cfg(feature = "serialize")]
use serde::ser::SerializeSeq;
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;

// Some of these are newtypes so that we can implement Serialize.

pub type SessionHashMap<'a, K, V> = hashbrown::HashMap<K, V, DefaultHashBuilder, BumpWrapper<'a>>;

pub type SessionHashSet<'a, T> = hashbrown::HashSet<T, DefaultHashBuilder, BumpWrapper<'a>>;

#[derive(PartialEq, Eq, Clone, PartialOrd, Ord)]
pub struct SessionVec<'a, T>(bumpalo::collections::Vec<'a, T>);

impl<'a, T: Debug> Debug for SessionVec<'a, T> {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    self.0.fmt(f)
  }
}

impl<'a, T> Deref for SessionVec<'a, T> {
  type Target = bumpalo::collections::Vec<'a, T>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<'a, T> DerefMut for SessionVec<'a, T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<'a, T> IntoIterator for SessionVec<'a, T> {
  type IntoIter = bumpalo::collections::vec::IntoIter<'a, T>;
  type Item = T;

  fn into_iter(self) -> Self::IntoIter {
    self.0.into_iter()
  }
}

impl<'a, 'item, T> IntoIterator for &'item SessionVec<'a, T> {
  type IntoIter = core::slice::Iter<'item, T>;
  type Item = &'item T;

  fn into_iter(self) -> Self::IntoIter {
    self.0.iter()
  }
}

impl<'a, 'item, T> IntoIterator for &'item mut SessionVec<'a, T> {
  type IntoIter = core::slice::IterMut<'item, T>;
  type Item = &'item mut T;

  fn into_iter(self) -> Self::IntoIter {
    self.0.iter_mut()
  }
}

#[cfg(feature = "serialize")]
impl<'a, T: serde::Serialize> serde::Serialize for SessionVec<'a, T> {
  fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
    let mut seq = serializer.serialize_seq(Some(self.0.len()))?;
    for e in self.0.iter() {
      seq.serialize_element(e)?;
    }
    seq.end()
  }
}

#[derive(Clone, Hash, PartialEq, Eq)]
pub struct SessionString<'a>(bumpalo::collections::String<'a>);

impl<'a> Debug for SessionString<'a> {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{:?}", self.0)
  }
}

impl<'a> Deref for SessionString<'a> {
  type Target = bumpalo::collections::String<'a>;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}

impl<'a> DerefMut for SessionString<'a> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.0
  }
}

impl<'a> Display for SessionString<'a> {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    write!(f, "{}", self.0)
  }
}

#[cfg(feature = "serialize")]
impl<'a> serde::Serialize for SessionString<'a> {
  fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
    serializer.serialize_str(&self.0)
  }
}

pub struct Session {
  pub(crate) mem: Bump,
}

impl Session {
  pub fn new() -> Session {
    Session { mem: Bump::new() }
  }

  pub fn reset(&mut self) {
    self.mem.reset();
  }

  pub fn get_allocator(&self) -> &Bump {
    &self.mem
  }

  pub fn new_vec<T>(&self) -> SessionVec<T> {
    SessionVec(bumpalo::collections::Vec::new_in(&self.mem))
  }

  pub fn new_string(&self) -> SessionString {
    SessionString(bumpalo::collections::String::new_in(&self.mem))
  }

  pub fn new_hashmap<K, V>(&self) -> SessionHashMap<K, V> {
    SessionHashMap::new_in(BumpWrapper(&self.mem))
  }

  pub fn new_hashset<T: Hash + Eq>(&self) -> SessionHashSet<T> {
    SessionHashSet::new_in(BumpWrapper(&self.mem))
  }
}