Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Fold and VisitMut methods for Vec<Attribute> #1762

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading