]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | use quote::quote; |
2 | ||
3 | pub fn type_foldable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream { | |
4 | if let syn::Data::Union(_) = s.ast().data { | |
5 | panic!("cannot derive on union") | |
6 | } | |
7 | ||
8 | s.add_bounds(synstructure::AddBounds::Generics); | |
9 | let body_fold = s.each_variant(|vi| { | |
10 | let bindings = vi.bindings(); | |
11 | vi.construct(|_, index| { | |
12 | let bind = &bindings[index]; | |
dfeec247 | 13 | quote! { |
ba9703b0 | 14 | ::rustc_middle::ty::fold::TypeFoldable::fold_with(#bind, __folder) |
60c5eb7d XL |
15 | } |
16 | }) | |
17 | }); | |
18 | let body_visit = s.fold(false, |acc, bind| { | |
ba9703b0 | 19 | quote! { #acc || ::rustc_middle::ty::fold::TypeFoldable::visit_with(#bind, __folder) } |
60c5eb7d XL |
20 | }); |
21 | ||
dfeec247 | 22 | s.bound_impl( |
ba9703b0 | 23 | quote!(::rustc_middle::ty::fold::TypeFoldable<'tcx>), |
dfeec247 | 24 | quote! { |
ba9703b0 | 25 | fn super_fold_with<__F: ::rustc_middle::ty::fold::TypeFolder<'tcx>>( |
dfeec247 XL |
26 | &self, |
27 | __folder: &mut __F | |
28 | ) -> Self { | |
29 | match *self { #body_fold } | |
30 | } | |
60c5eb7d | 31 | |
ba9703b0 | 32 | fn super_visit_with<__F: ::rustc_middle::ty::fold::TypeVisitor<'tcx>>( |
dfeec247 XL |
33 | &self, |
34 | __folder: &mut __F | |
35 | ) -> bool { | |
36 | match *self { #body_visit } | |
37 | } | |
38 | }, | |
39 | ) | |
60c5eb7d | 40 | } |