1use std::collections::HashMap;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct Symbol(pub u32);
10
11#[derive(Debug, Clone)]
13pub struct StringPool {
14 map: HashMap<String, Symbol>,
15 strings: Vec<String>,
16}
17
18impl Default for StringPool {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24impl StringPool {
25 pub fn new() -> Self {
26 Self {
27 map: HashMap::new(),
28 strings: Vec::new(),
29 }
30 }
31
32 pub fn intern(&mut self, s: &str) -> Symbol {
34 if let Some(&sym) = self.map.get(s) {
35 return sym;
36 }
37 let sym = Symbol(self.strings.len() as u32);
38 self.strings.push(s.to_string());
39 self.map.insert(s.to_string(), sym);
40 sym
41 }
42
43 pub fn resolve(&self, sym: Symbol) -> &str {
45 &self.strings[sym.0 as usize]
46 }
47
48 pub fn map_get(&self, s: &str) -> Option<Symbol> {
50 self.map.get(s).copied()
51 }
52}