Thread global symbol interner through as if it was a local

This commit is contained in:
Lukas Wirth 2023-03-29 11:42:08 +02:00
parent c4582f6d18
commit 0522503f14
3 changed files with 39 additions and 26 deletions

View file

@ -1,28 +1,30 @@
//! Symbol interner for proc-macro-srv
use std::{cell::RefCell, collections::HashMap};
use std::{cell::RefCell, collections::HashMap, thread::LocalKey};
use tt::SmolStr;
thread_local! {
static SYMBOL_INTERNER: RefCell<SymbolInterner> = Default::default();
pub(crate) static SYMBOL_INTERNER: RefCell<SymbolInterner> = Default::default();
}
// ID for an interned symbol.
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
pub struct Symbol(u32);
pub(crate) type SymbolInternerRef = &'static LocalKey<RefCell<SymbolInterner>>;
impl Symbol {
pub(super) fn intern(data: &str) -> Symbol {
SYMBOL_INTERNER.with(|i| i.borrow_mut().intern(data))
pub(super) fn intern(interner: SymbolInternerRef, data: &str) -> Symbol {
interner.with(|i| i.borrow_mut().intern(data))
}
pub(super) fn text(&self) -> SmolStr {
SYMBOL_INTERNER.with(|i| i.borrow().get(self).clone())
pub(super) fn text(&self, interner: SymbolInternerRef) -> SmolStr {
interner.with(|i| i.borrow().get(self).clone())
}
}
#[derive(Default)]
struct SymbolInterner {
pub(crate) struct SymbolInterner {
idents: HashMap<SmolStr, u32>,
ident_data: Vec<SmolStr>,
}