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>,
    // Hmm, maybe a Vec is enough here, since String is already heap-allocated so we can take &str
    // from it anyway. That is, when you push extra strings on and the Vec reallocates the content
    // to a larger array, the Strings should stay in the same place.
    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(|| {
            // Extending the lifetime of the allocated string here, so we can put it into the map
            // without issues and return a reference to it. The lifetime of the return value is
            // constrained to the lifetime of self, so that should be fine.
            let itemref: &'static str = unsafe {
                extend_lifetime(self.arena.alloc(itemcow.into_owned()))
            };
            self.map.insert(itemref, itemref);
            itemref
        })
    }
}