Minor refactoring

- use `str::parse()` rather than `FromStr::from_str()`
- use `iter::once()` instead of constructing `Vec` for a single element
This commit is contained in:
Ryo Yoshida 2023-07-27 16:30:57 +09:00
parent b64e5b3919
commit 10b5fd1431
No known key found for this signature in database
GPG key ID: E25698A930586171
2 changed files with 12 additions and 11 deletions

View file

@ -17,7 +17,10 @@ use token_stream::TokenStreamBuilder;
mod symbol; mod symbol;
pub use symbol::*; pub use symbol::*;
use std::ops::{Bound, Range}; use std::{
iter,
ops::{Bound, Range},
};
use crate::tt; use crate::tt;
@ -80,9 +83,7 @@ impl server::TokenStream for RustAnalyzer {
stream.is_empty() stream.is_empty()
} }
fn from_str(&mut self, src: &str) -> Self::TokenStream { fn from_str(&mut self, src: &str) -> Self::TokenStream {
use std::str::FromStr; src.parse().expect("cannot parse string")
Self::TokenStream::from_str(src).expect("cannot parse string")
} }
fn to_string(&mut self, stream: &Self::TokenStream) -> String { fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string() stream.to_string()
@ -101,7 +102,7 @@ impl server::TokenStream for RustAnalyzer {
}, },
}; };
let tree = TokenTree::from(group); let tree = TokenTree::from(group);
Self::TokenStream::from_iter(vec![tree]) Self::TokenStream::from_iter(iter::once(tree))
} }
bridge::TokenTree::Ident(ident) => { bridge::TokenTree::Ident(ident) => {
@ -111,7 +112,7 @@ impl server::TokenStream for RustAnalyzer {
let ident: tt::Ident = tt::Ident { text, span: ident.span }; let ident: tt::Ident = tt::Ident { text, span: ident.span };
let leaf = tt::Leaf::from(ident); let leaf = tt::Leaf::from(ident);
let tree = TokenTree::from(leaf); let tree = TokenTree::from(leaf);
Self::TokenStream::from_iter(vec![tree]) Self::TokenStream::from_iter(iter::once(tree))
} }
bridge::TokenTree::Literal(literal) => { bridge::TokenTree::Literal(literal) => {
@ -123,7 +124,7 @@ impl server::TokenStream for RustAnalyzer {
let literal = tt::Literal { text, span: literal.0.span }; let literal = tt::Literal { text, span: literal.0.span };
let leaf = tt::Leaf::from(literal); let leaf = tt::Leaf::from(literal);
let tree = TokenTree::from(leaf); let tree = TokenTree::from(leaf);
Self::TokenStream::from_iter(vec![tree]) Self::TokenStream::from_iter(iter::once(tree))
} }
bridge::TokenTree::Punct(p) => { bridge::TokenTree::Punct(p) => {
@ -134,7 +135,7 @@ impl server::TokenStream for RustAnalyzer {
}; };
let leaf = tt::Leaf::from(punct); let leaf = tt::Leaf::from(punct);
let tree = TokenTree::from(leaf); let tree = TokenTree::from(leaf);
Self::TokenStream::from_iter(vec![tree]) Self::TokenStream::from_iter(iter::once(tree))
} }
} }
} }
@ -355,12 +356,12 @@ impl server::Server for RustAnalyzer {
} }
fn intern_symbol(ident: &str) -> Self::Symbol { fn intern_symbol(ident: &str) -> Self::Symbol {
// FIXME: should be self.interner once the proc-macro api allows is // FIXME: should be `self.interner` once the proc-macro api allows it.
Symbol::intern(&SYMBOL_INTERNER, &::tt::SmolStr::from(ident)) Symbol::intern(&SYMBOL_INTERNER, &::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)) {
// FIXME: should be self.interner once the proc-macro api allows is // FIXME: should be `self.interner` once the proc-macro api allows it.
f(symbol.text(&SYMBOL_INTERNER).as_str()) f(symbol.text(&SYMBOL_INTERNER).as_str())
} }
} }

View file

@ -65,7 +65,7 @@ pub mod token_id {
} }
impl TokenTree { impl TokenTree {
pub const fn empty() -> Self { pub const fn empty() -> Self {
Self::Subtree(Subtree { delimiter: Delimiter::unspecified(), token_trees: vec![] }) Self::Subtree(Subtree::empty())
} }
} }