@@ -38,33 +38,16 @@ fn parse_attributes(field: &syn::Field) -> Attributes {
3838 attrs
3939}
4040
41- pub fn hash_stable_generic_derive ( mut s : synstructure:: Structure < ' _ > ) -> proc_macro2:: TokenStream {
41+ pub ( crate ) fn hash_stable_generic_derive (
42+ mut s : synstructure:: Structure < ' _ > ,
43+ ) -> proc_macro2:: TokenStream {
4244 let generic: syn:: GenericParam = parse_quote ! ( __CTX) ;
4345 s. add_bounds ( synstructure:: AddBounds :: Generics ) ;
4446 s. add_impl_generic ( generic) ;
4547 s. add_where_predicate ( parse_quote ! { __CTX: crate :: HashStableContext } ) ;
46- let body = s. each ( |bi| {
47- let attrs = parse_attributes ( bi. ast ( ) ) ;
48- if attrs. ignore {
49- quote ! { }
50- } else if let Some ( project) = attrs. project {
51- quote ! {
52- ( & #bi. #project) . hash_stable( __hcx, __hasher) ;
53- }
54- } else {
55- quote ! {
56- #bi. hash_stable( __hcx, __hasher) ;
57- }
58- }
59- } ) ;
6048
61- let discriminant = match s. ast ( ) . data {
62- syn:: Data :: Enum ( _) => quote ! {
63- :: std:: mem:: discriminant( self ) . hash_stable( __hcx, __hasher) ;
64- } ,
65- syn:: Data :: Struct ( _) => quote ! { } ,
66- syn:: Data :: Union ( _) => panic ! ( "cannot derive on union" ) ,
67- } ;
49+ let discriminant = hash_stable_discriminant ( & mut s) ;
50+ let body = hash_stable_body ( & mut s) ;
6851
6952 s. bound_impl (
7053 quote ! ( :: rustc_data_structures:: stable_hasher:: HashStable <__CTX>) ,
@@ -81,32 +64,13 @@ pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_ma
8164 )
8265}
8366
84- pub fn hash_stable_derive ( mut s : synstructure:: Structure < ' _ > ) -> proc_macro2:: TokenStream {
67+ pub ( crate ) fn hash_stable_derive ( mut s : synstructure:: Structure < ' _ > ) -> proc_macro2:: TokenStream {
8568 let generic: syn:: GenericParam = parse_quote ! ( ' __ctx) ;
8669 s. add_bounds ( synstructure:: AddBounds :: Generics ) ;
8770 s. add_impl_generic ( generic) ;
88- let body = s. each ( |bi| {
89- let attrs = parse_attributes ( bi. ast ( ) ) ;
90- if attrs. ignore {
91- quote ! { }
92- } else if let Some ( project) = attrs. project {
93- quote ! {
94- ( & #bi. #project) . hash_stable( __hcx, __hasher) ;
95- }
96- } else {
97- quote ! {
98- #bi. hash_stable( __hcx, __hasher) ;
99- }
100- }
101- } ) ;
10271
103- let discriminant = match s. ast ( ) . data {
104- syn:: Data :: Enum ( _) => quote ! {
105- :: std:: mem:: discriminant( self ) . hash_stable( __hcx, __hasher) ;
106- } ,
107- syn:: Data :: Struct ( _) => quote ! { } ,
108- syn:: Data :: Union ( _) => panic ! ( "cannot derive on union" ) ,
109- } ;
72+ let discriminant = hash_stable_discriminant ( & mut s) ;
73+ let body = hash_stable_body ( & mut s) ;
11074
11175 s. bound_impl (
11276 quote ! (
@@ -126,3 +90,30 @@ pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::To
12690 } ,
12791 )
12892}
93+
94+ fn hash_stable_discriminant ( s : & mut synstructure:: Structure < ' _ > ) -> proc_macro2:: TokenStream {
95+ match s. ast ( ) . data {
96+ syn:: Data :: Enum ( _) => quote ! {
97+ :: std:: mem:: discriminant( self ) . hash_stable( __hcx, __hasher) ;
98+ } ,
99+ syn:: Data :: Struct ( _) => quote ! { } ,
100+ syn:: Data :: Union ( _) => panic ! ( "cannot derive on union" ) ,
101+ }
102+ }
103+
104+ fn hash_stable_body ( s : & mut synstructure:: Structure < ' _ > ) -> proc_macro2:: TokenStream {
105+ s. each ( |bi| {
106+ let attrs = parse_attributes ( bi. ast ( ) ) ;
107+ if attrs. ignore {
108+ quote ! { }
109+ } else if let Some ( project) = attrs. project {
110+ quote ! {
111+ ( & #bi. #project) . hash_stable( __hcx, __hasher) ;
112+ }
113+ } else {
114+ quote ! {
115+ #bi. hash_stable( __hcx, __hasher) ;
116+ }
117+ }
118+ } )
119+ }
0 commit comments