Skip to content

Commit 48bcc22

Browse files
Move interner methods to Symbol, return SmolStr directly since it's ref-counted
1 parent 246947b commit 48bcc22

File tree

2 files changed

+19
-27
lines changed

2 files changed

+19
-27
lines changed

crates/proc-macro-srv/src/abis/abi_sysroot/ra_server.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,9 @@ impl server::FreeFunctions for RustAnalyzer {
8383
s: &str,
8484
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
8585
// FIXME: keep track of LitKind and Suffix
86-
let symbol = ThreadLocalSymbolInterner::intern(s);
8786
Ok(bridge::Literal {
8887
kind: bridge::LitKind::Err,
89-
symbol,
88+
symbol: Symbol::intern(s),
9089
suffix: None,
9190
span: tt::TokenId::unspecified(),
9291
})
@@ -124,7 +123,7 @@ impl server::TokenStream for RustAnalyzer {
124123

125124
bridge::TokenTree::Ident(ident) => {
126125
// FIXME: handle raw idents
127-
let text = ThreadLocalSymbolInterner::get_cloned(&ident.sym);
126+
let text = ident.sym.text();
128127
let ident: tt::Ident = tt::Ident { text, id: ident.span };
129128
let leaf = tt::Leaf::from(ident);
130129
let tree = TokenTree::from(leaf);
@@ -198,7 +197,7 @@ impl server::TokenStream for RustAnalyzer {
198197
.map(|tree| match tree {
199198
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
200199
bridge::TokenTree::Ident(bridge::Ident {
201-
sym: ThreadLocalSymbolInterner::intern(&ident.text),
200+
sym: Symbol::intern(&ident.text),
202201
// FIXME: handle raw idents
203202
is_raw: false,
204203
span: ident.id,
@@ -208,7 +207,7 @@ impl server::TokenStream for RustAnalyzer {
208207
bridge::TokenTree::Literal(bridge::Literal {
209208
// FIXME: handle literal kinds
210209
kind: bridge::LitKind::Err,
211-
symbol: ThreadLocalSymbolInterner::intern(&lit.text),
210+
symbol: Symbol::intern(&lit.text),
212211
// FIXME: handle suffixes
213212
suffix: None,
214213
span: lit.id,
@@ -402,11 +401,11 @@ impl server::Server for RustAnalyzer {
402401
}
403402

404403
fn intern_symbol(ident: &str) -> Self::Symbol {
405-
ThreadLocalSymbolInterner::intern(&tt::SmolStr::from(ident))
404+
Symbol::intern(&tt::SmolStr::from(ident))
406405
}
407406

408407
fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
409-
ThreadLocalSymbolInterner::with(symbol, |s| f(s.as_str()))
408+
f(symbol.text().as_str())
410409
}
411410
}
412411

@@ -450,10 +449,9 @@ impl LiteralFormatter {
450449
}
451450

452451
fn with_symbol_and_suffix<R>(&self, f: impl FnOnce(&str, &str) -> R) -> R {
453-
ThreadLocalSymbolInterner::with(&self.0.symbol, |symbol| match self.0.suffix.as_ref() {
454-
Some(suffix) => ThreadLocalSymbolInterner::with(suffix, |suffix| f(symbol, suffix)),
455-
None => f(symbol, ""),
456-
})
452+
let symbol = self.0.symbol.text();
453+
let suffix = self.0.suffix.map(|s| s.text()).unwrap_or_default();
454+
f(symbol.as_str(), suffix.as_str())
457455
}
458456
}
459457

crates/proc-macro-srv/src/abis/abi_sysroot/ra_server/symbol.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ thread_local! {
1111
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
1212
pub struct Symbol(u32);
1313

14+
impl Symbol {
15+
pub fn intern(data: &str) -> Symbol {
16+
SYMBOL_INTERNER.with(|i| i.borrow_mut().intern(data))
17+
}
18+
19+
pub fn text(&self) -> SmolStr {
20+
SYMBOL_INTERNER.with(|i| i.borrow().get(self).clone())
21+
}
22+
}
23+
1424
#[derive(Default)]
1525
struct SymbolInterner {
1626
idents: HashMap<SmolStr, u32>,
@@ -34,19 +44,3 @@ impl SymbolInterner {
3444
&self.ident_data[sym.0 as usize]
3545
}
3646
}
37-
38-
pub(super) struct ThreadLocalSymbolInterner;
39-
40-
impl ThreadLocalSymbolInterner {
41-
pub(super) fn intern(data: &str) -> Symbol {
42-
SYMBOL_INTERNER.with(|i| i.borrow_mut().intern(data))
43-
}
44-
45-
pub(super) fn with<T>(sym: &Symbol, f: impl FnOnce(&SmolStr) -> T) -> T {
46-
SYMBOL_INTERNER.with(|i| f(i.borrow().get(sym)))
47-
}
48-
49-
pub(super) fn get_cloned(sym: &Symbol) -> SmolStr {
50-
Self::with(sym, |s| s.clone())
51-
}
52-
}

0 commit comments

Comments
 (0)