radiate_core/domain/
intern.rs

1#[macro_export]
2macro_rules! intern {
3    ($name:expr) => {{
4        use std::cell::RefCell;
5        use std::collections::HashSet;
6
7        thread_local! {
8            static INTERNED: RefCell<HashSet<&'static str>> = RefCell::new(HashSet::new());
9        }
10
11        let name = String::from($name);
12        INTERNED.with(|interned| {
13            let mut interned = interned.borrow_mut();
14            if let Some(&existing) = interned.get(&*name) {
15                existing
16            } else {
17                let static_name: &'static str = Box::leak(name.into_boxed_str());
18                interned.insert(static_name);
19                static_name
20            }
21        })
22    }};
23}