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
use std::collections::HashMap;
use std::hash::BuildHasher;
use std::borrow::{Borrow, Cow};
use typed_arena::Arena;
pub struct StringShare<H: BuildHasher> {
map: HashMap<&'static str, &'static str, H>,
arena: Arena<String>,
}
impl<H: BuildHasher + Default> StringShare<H> {
pub fn new() -> Self {
StringShare {
map: HashMap::default(),
arena: Arena::new(),
}
}
pub fn with_capacity(cap: usize) -> Self {
StringShare {
map: HashMap::with_capacity_and_hasher(cap, Default::default()),
arena: Arena::with_capacity(cap),
}
}
}
impl<H: BuildHasher + Default> Default for StringShare<H> {
fn default() -> Self {
Self::new()
}
}
impl<H: BuildHasher> StringShare<H> {
pub fn insert<'s, Q>(&mut self, item: Q) -> &str
where
Q: Into<Cow<'s, str>>,
{
use utils::extend_lifetime;
let itemcow = item.into();
self.map.get::<str>(itemcow.borrow()).map(|&s| s).unwrap_or_else(|| {
let itemref: &'static str = unsafe {
extend_lifetime(self.arena.alloc(itemcow.into_owned()))
};
self.map.insert(itemref, itemref);
itemref
})
}
}