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 386ae9d
Show file tree
Hide file tree
Showing 4 changed files with 245 additions and 383 deletions.
25 changes: 21 additions & 4 deletions codegen/src/fold.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cfg::{self, DocCfg};
use crate::{file, full, gen};
use anyhow::Result;
use proc_macro2::{Ident, Span, TokenStream};
Expand Down Expand Up @@ -32,10 +33,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 +221,16 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
#fold_impl
}
});

if s.ident == "Attribute" {
let features = cfg::features(&s.features, DocCfg::Ordinary);
traits.extend(quote! {
#features
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
33 changes: 26 additions & 7 deletions codegen/src/visit_mut.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cfg::{self, DocCfg};
use crate::operand::{Borrowed, Operand, Owned};
use crate::{file, full, gen};
use anyhow::Result;
Expand Down Expand Up @@ -36,14 +37,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 +200,18 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
#visit_mut_impl
}
});

if s.ident == "Attribute" {
let features = cfg::features(&s.features, DocCfg::Ordinary);
traits.extend(quote! {
#features
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 386ae9d

Please sign in to comment.