Move interner methods to Symbol, return SmolStr directly since it's ref-counted

This commit is contained in:
Amos Wenger 2022-07-21 21:32:46 +02:00
parent 246947b779
commit 48bcc229bf
2 changed files with 19 additions and 27 deletions

View File

@ -83,10 +83,9 @@ impl server::FreeFunctions for RustAnalyzer {
s: &str,
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
// FIXME: keep track of LitKind and Suffix
let symbol = ThreadLocalSymbolInterner::intern(s);
Ok(bridge::Literal {
kind: bridge::LitKind::Err,
symbol,
symbol: Symbol::intern(s),
suffix: None,
span: tt::TokenId::unspecified(),
})
@ -124,7 +123,7 @@ impl server::TokenStream for RustAnalyzer {
bridge::TokenTree::Ident(ident) => {
// FIXME: handle raw idents
let text = ThreadLocalSymbolInterner::get_cloned(&ident.sym);
let text = ident.sym.text();
let ident: tt::Ident = tt::Ident { text, id: ident.span };
let leaf = tt::Leaf::from(ident);
let tree = TokenTree::from(leaf);
@ -198,7 +197,7 @@ impl server::TokenStream for RustAnalyzer {
.map(|tree| match tree {
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
bridge::TokenTree::Ident(bridge::Ident {
sym: ThreadLocalSymbolInterner::intern(&ident.text),
sym: Symbol::intern(&ident.text),
// FIXME: handle raw idents
is_raw: false,
span: ident.id,
@ -208,7 +207,7 @@ impl server::TokenStream for RustAnalyzer {
bridge::TokenTree::Literal(bridge::Literal {
// FIXME: handle literal kinds
kind: bridge::LitKind::Err,
symbol: ThreadLocalSymbolInterner::intern(&lit.text),
symbol: Symbol::intern(&lit.text),
// FIXME: handle suffixes
suffix: None,
span: lit.id,
@ -402,11 +401,11 @@ impl server::Server for RustAnalyzer {
}
fn intern_symbol(ident: &str) -> Self::Symbol {
ThreadLocalSymbolInterner::intern(&tt::SmolStr::from(ident))
Symbol::intern(&tt::SmolStr::from(ident))
}
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
ThreadLocalSymbolInterner::with(symbol, |s| f(s.as_str()))
f(symbol.text().as_str())
}
}
@ -450,10 +449,9 @@ impl LiteralFormatter {
}
fn with_symbol_and_suffix<R>(&self, f: impl FnOnce(&str, &str) -> R) -> R {
ThreadLocalSymbolInterner::with(&self.0.symbol, |symbol| match self.0.suffix.as_ref() {
Some(suffix) => ThreadLocalSymbolInterner::with(suffix, |suffix| f(symbol, suffix)),
None => f(symbol, ""),
})
let symbol = self.0.symbol.text();
let suffix = self.0.suffix.map(|s| s.text()).unwrap_or_default();
f(symbol.as_str(), suffix.as_str())
}
}

View File

@ -11,6 +11,16 @@ thread_local! {
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
pub struct Symbol(u32);
impl Symbol {
pub fn intern(data: &str) -> Symbol {
SYMBOL_INTERNER.with(|i| i.borrow_mut().intern(data))
}
pub fn text(&self) -> SmolStr {
SYMBOL_INTERNER.with(|i| i.borrow().get(self).clone())
}
}
#[derive(Default)]
struct SymbolInterner {
idents: HashMap<SmolStr, u32>,
@ -34,19 +44,3 @@ impl SymbolInterner {
&self.ident_data[sym.0 as usize]
}
}
pub(super) struct ThreadLocalSymbolInterner;
impl ThreadLocalSymbolInterner {
pub(super) fn intern(data: &str) -> Symbol {
SYMBOL_INTERNER.with(|i| i.borrow_mut().intern(data))
}
pub(super) fn with<T>(sym: &Symbol, f: impl FnOnce(&SmolStr) -> T) -> T {
SYMBOL_INTERNER.with(|i| f(i.borrow().get(sym)))
}
pub(super) fn get_cloned(sym: &Symbol) -> SmolStr {
Self::with(sym, |s| s.clone())
}
}