mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-26 20:09:19 +00:00
Move interner methods to Symbol, return SmolStr directly since it's ref-counted
This commit is contained in:
parent
246947b779
commit
48bcc229bf
2 changed files with 19 additions and 27 deletions
|
@ -83,10 +83,9 @@ impl server::FreeFunctions for RustAnalyzer {
|
||||||
s: &str,
|
s: &str,
|
||||||
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
|
) -> Result<bridge::Literal<Self::Span, Self::Symbol>, ()> {
|
||||||
// FIXME: keep track of LitKind and Suffix
|
// FIXME: keep track of LitKind and Suffix
|
||||||
let symbol = ThreadLocalSymbolInterner::intern(s);
|
|
||||||
Ok(bridge::Literal {
|
Ok(bridge::Literal {
|
||||||
kind: bridge::LitKind::Err,
|
kind: bridge::LitKind::Err,
|
||||||
symbol,
|
symbol: Symbol::intern(s),
|
||||||
suffix: None,
|
suffix: None,
|
||||||
span: tt::TokenId::unspecified(),
|
span: tt::TokenId::unspecified(),
|
||||||
})
|
})
|
||||||
|
@ -124,7 +123,7 @@ impl server::TokenStream for RustAnalyzer {
|
||||||
|
|
||||||
bridge::TokenTree::Ident(ident) => {
|
bridge::TokenTree::Ident(ident) => {
|
||||||
// FIXME: handle raw idents
|
// 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 ident: tt::Ident = tt::Ident { text, id: ident.span };
|
||||||
let leaf = tt::Leaf::from(ident);
|
let leaf = tt::Leaf::from(ident);
|
||||||
let tree = TokenTree::from(leaf);
|
let tree = TokenTree::from(leaf);
|
||||||
|
@ -198,7 +197,7 @@ impl server::TokenStream for RustAnalyzer {
|
||||||
.map(|tree| match tree {
|
.map(|tree| match tree {
|
||||||
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
|
tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => {
|
||||||
bridge::TokenTree::Ident(bridge::Ident {
|
bridge::TokenTree::Ident(bridge::Ident {
|
||||||
sym: ThreadLocalSymbolInterner::intern(&ident.text),
|
sym: Symbol::intern(&ident.text),
|
||||||
// FIXME: handle raw idents
|
// FIXME: handle raw idents
|
||||||
is_raw: false,
|
is_raw: false,
|
||||||
span: ident.id,
|
span: ident.id,
|
||||||
|
@ -208,7 +207,7 @@ impl server::TokenStream for RustAnalyzer {
|
||||||
bridge::TokenTree::Literal(bridge::Literal {
|
bridge::TokenTree::Literal(bridge::Literal {
|
||||||
// FIXME: handle literal kinds
|
// FIXME: handle literal kinds
|
||||||
kind: bridge::LitKind::Err,
|
kind: bridge::LitKind::Err,
|
||||||
symbol: ThreadLocalSymbolInterner::intern(&lit.text),
|
symbol: Symbol::intern(&lit.text),
|
||||||
// FIXME: handle suffixes
|
// FIXME: handle suffixes
|
||||||
suffix: None,
|
suffix: None,
|
||||||
span: lit.id,
|
span: lit.id,
|
||||||
|
@ -402,11 +401,11 @@ impl server::Server for RustAnalyzer {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn intern_symbol(ident: &str) -> Self::Symbol {
|
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)) {
|
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 {
|
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() {
|
let symbol = self.0.symbol.text();
|
||||||
Some(suffix) => ThreadLocalSymbolInterner::with(suffix, |suffix| f(symbol, suffix)),
|
let suffix = self.0.suffix.map(|s| s.text()).unwrap_or_default();
|
||||||
None => f(symbol, ""),
|
f(symbol.as_str(), suffix.as_str())
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,16 @@ thread_local! {
|
||||||
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
|
#[derive(Hash, Eq, PartialEq, Copy, Clone)]
|
||||||
pub struct Symbol(u32);
|
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)]
|
#[derive(Default)]
|
||||||
struct SymbolInterner {
|
struct SymbolInterner {
|
||||||
idents: HashMap<SmolStr, u32>,
|
idents: HashMap<SmolStr, u32>,
|
||||||
|
@ -34,19 +44,3 @@ impl SymbolInterner {
|
||||||
&self.ident_data[sym.0 as usize]
|
&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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue