Skip to content

Commit

Permalink
Add Fold and VisitMut methods for Vec<Attribute>
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 20, 2024
1 parent 4c7f82e commit 7681951
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 383 deletions.
22 changes: 18 additions & 4 deletions codegen/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ fn visit(
}
Type::Vec(t) => {
let Type::Syn(t) = &**t else { unimplemented!() };
let method = method_name(t);
Some(quote! {
fold_vec(#name, f, F::#method)
})
if t == "Attribute" {
Some(quote! {
f.fold_attributes(#name)
})
} else {
let method = method_name(t);
Some(quote! {
fold_vec(#name, f, F::#method)
})
}
}
Type::Punctuated(p) => {
let t = &*p.element;
Expand Down Expand Up @@ -214,6 +220,14 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
#fold_impl
}
});

if s.ident == "Attribute" {
traits.extend(quote! {
fn fold_attributes(&mut self, i: Vec<crate::Attribute>) -> Vec<crate::Attribute> {
fold_vec(i, self, Self::fold_attribute)
}
});
}
}

pub fn generate(defs: &Definitions) -> Result<()> {
Expand Down
30 changes: 23 additions & 7 deletions codegen/src/visit_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ fn visit(
visit(t, features, defs, &Owned(quote!(*#name)))
}
Type::Vec(t) => {
let operand = Borrowed(quote!(it));
let val = visit(t, features, defs, &operand)?;
let name = name.ref_mut_tokens();
Some(quote! {
for it in #name {
#val;
}
})
if matches!(&**t, Type::Syn(t) if t == "Attribute") {
Some(quote! {
v.visit_attributes_mut(#name);
})
} else {
let operand = Borrowed(quote!(it));
let val = visit(t, features, defs, &operand)?;
Some(quote! {
for it in #name {
#val;
}
})
}
}
Type::Punctuated(p) => {
let operand = Borrowed(quote!(it));
Expand Down Expand Up @@ -193,6 +199,16 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
#visit_mut_impl
}
});

if s.ident == "Attribute" {
traits.extend(quote! {
fn visit_attributes_mut(&mut self, i: &mut Vec<crate::Attribute>) {
for attr in i {
self.visit_attribute_mut(attr);
}
}
});
}
}

pub fn generate(defs: &Definitions) -> Result<()> {
Expand Down
Loading

0 comments on commit 7681951

Please sign in to comment.