Skip to content

Commit 386ae9d

Browse files
committed
Add Fold and VisitMut methods for Vec<Attribute>
1 parent 4c7f82e commit 386ae9d

File tree

4 files changed

+245
-383
lines changed

4 files changed

+245
-383
lines changed

codegen/src/fold.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cfg::{self, DocCfg};
12
use crate::{file, full, gen};
23
use anyhow::Result;
34
use proc_macro2::{Ident, Span, TokenStream};
@@ -32,10 +33,16 @@ fn visit(
3233
}
3334
Type::Vec(t) => {
3435
let Type::Syn(t) = &**t else { unimplemented!() };
35-
let method = method_name(t);
36-
Some(quote! {
37-
fold_vec(#name, f, F::#method)
38-
})
36+
if t == "Attribute" {
37+
Some(quote! {
38+
f.fold_attributes(#name)
39+
})
40+
} else {
41+
let method = method_name(t);
42+
Some(quote! {
43+
fold_vec(#name, f, F::#method)
44+
})
45+
}
3946
}
4047
Type::Punctuated(p) => {
4148
let t = &*p.element;
@@ -214,6 +221,16 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
214221
#fold_impl
215222
}
216223
});
224+
225+
if s.ident == "Attribute" {
226+
let features = cfg::features(&s.features, DocCfg::Ordinary);
227+
traits.extend(quote! {
228+
#features
229+
fn fold_attributes(&mut self, i: Vec<crate::Attribute>) -> Vec<crate::Attribute> {
230+
fold_vec(i, self, Self::fold_attribute)
231+
}
232+
});
233+
}
217234
}
218235

219236
pub fn generate(defs: &Definitions) -> Result<()> {

codegen/src/visit_mut.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::cfg::{self, DocCfg};
12
use crate::operand::{Borrowed, Operand, Owned};
23
use crate::{file, full, gen};
34
use anyhow::Result;
@@ -36,14 +37,20 @@ fn visit(
3637
visit(t, features, defs, &Owned(quote!(*#name)))
3738
}
3839
Type::Vec(t) => {
39-
let operand = Borrowed(quote!(it));
40-
let val = visit(t, features, defs, &operand)?;
4140
let name = name.ref_mut_tokens();
42-
Some(quote! {
43-
for it in #name {
44-
#val;
45-
}
46-
})
41+
if matches!(&**t, Type::Syn(t) if t == "Attribute") {
42+
Some(quote! {
43+
v.visit_attributes_mut(#name);
44+
})
45+
} else {
46+
let operand = Borrowed(quote!(it));
47+
let val = visit(t, features, defs, &operand)?;
48+
Some(quote! {
49+
for it in #name {
50+
#val;
51+
}
52+
})
53+
}
4754
}
4855
Type::Punctuated(p) => {
4956
let operand = Borrowed(quote!(it));
@@ -193,6 +200,18 @@ fn node(traits: &mut TokenStream, impls: &mut TokenStream, s: &Node, defs: &Defi
193200
#visit_mut_impl
194201
}
195202
});
203+
204+
if s.ident == "Attribute" {
205+
let features = cfg::features(&s.features, DocCfg::Ordinary);
206+
traits.extend(quote! {
207+
#features
208+
fn visit_attributes_mut(&mut self, i: &mut Vec<crate::Attribute>) {
209+
for attr in i {
210+
self.visit_attribute_mut(attr);
211+
}
212+
}
213+
});
214+
}
196215
}
197216

198217
pub fn generate(defs: &Definitions) -> Result<()> {

0 commit comments

Comments
 (0)