Swap Subtree::token_trees from Vec to boxed slice

This commit is contained in:
GnomedDev 2024-02-03 22:47:13 +00:00
parent 8f6a72871e
commit 8011b56827
No known key found for this signature in database
GPG key ID: 9BF10F8372B254D1
22 changed files with 194 additions and 158 deletions

View file

@ -23,10 +23,10 @@ pub enum TokenTree<S> {
}
impl_from!(Leaf<S>, Subtree<S> for TokenTree);
impl<S: Span> TokenTree<S> {
pub const fn empty(span: S) -> Self {
pub fn empty(span: S) -> Self {
Self::Subtree(Subtree {
delimiter: Delimiter::invisible_spanned(span),
token_trees: vec![],
token_trees: Box::new([]),
})
}
@ -34,7 +34,7 @@ impl<S: Span> TokenTree<S> {
match self {
TokenTree::Leaf(_) => Subtree {
delimiter: Delimiter::invisible_delim_spanned(span),
token_trees: vec![self],
token_trees: Box::new([self]),
},
TokenTree::Subtree(s) => s,
}
@ -69,12 +69,12 @@ impl_from!(Literal<S>, Punct<S>, Ident<S> for Leaf);
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Subtree<S> {
pub delimiter: Delimiter<S>,
pub token_trees: Vec<TokenTree<S>>,
pub token_trees: Box<[TokenTree<S>]>,
}
impl<S: Span> Subtree<S> {
pub const fn empty(span: DelimSpan<S>) -> Self {
Subtree { delimiter: Delimiter::invisible_delim_spanned(span), token_trees: vec![] }
pub fn empty(span: DelimSpan<S>) -> Self {
Subtree { delimiter: Delimiter::invisible_delim_spanned(span), token_trees: Box::new([]) }
}
pub fn visit_ids(&mut self, f: &mut impl FnMut(S) -> S) {
@ -91,6 +91,18 @@ impl<S: Span> Subtree<S> {
}
}
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct SubtreeBuilder<S> {
pub delimiter: Delimiter<S>,
pub token_trees: Vec<TokenTree<S>>,
}
impl<S> SubtreeBuilder<S> {
pub fn build(self) -> Subtree<S> {
Subtree { delimiter: self.delimiter, token_trees: self.token_trees.into_boxed_slice() }
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct DelimSpan<S> {
pub open: S,
@ -241,7 +253,7 @@ impl<S> fmt::Display for Subtree<S> {
};
f.write_str(l)?;
let mut needs_space = false;
for tt in &self.token_trees {
for tt in self.token_trees.iter() {
if needs_space {
f.write_str(" ")?;
}
@ -316,7 +328,7 @@ impl<S> Subtree<S> {
let mut res = String::new();
res.push_str(delim.0);
let mut last = None;
for child in &self.token_trees {
for child in self.token_trees.iter() {
let s = match child {
TokenTree::Leaf(it) => {
let s = match it {