1 use crate::syntax::set::UnorderedSet as Set; 2 use lazy_static::lazy_static; 3 use std::sync::{Mutex, PoisonError}; 4 5 #[derive(Copy, Clone, Default)] 6 pub struct InternedString(&'static str); 7 8 impl InternedString { str(self) -> &'static str9 pub fn str(self) -> &'static str { 10 self.0 11 } 12 } 13 intern(s: &str) -> InternedString14pub fn intern(s: &str) -> InternedString { 15 lazy_static! { 16 static ref INTERN: Mutex<Set<&'static str>> = Mutex::new(Set::new()); 17 } 18 19 let mut set = INTERN.lock().unwrap_or_else(PoisonError::into_inner); 20 InternedString(match set.get(s) { 21 Some(interned) => *interned, 22 None => { 23 let interned = Box::leak(Box::from(s)); 24 set.insert(interned); 25 interned 26 } 27 }) 28 } 29