1 use proc_macro2::{self, Ident};
3 use syn::{self, parse_quote, Meta, NestedMeta};
7 project: Option<Ident>,
10 fn parse_attributes(field: &syn::Field) -> Attributes {
11 let mut attrs = Attributes { ignore: false, project: None };
12 for attr in &field.attrs {
13 if let Ok(meta) = attr.parse_meta() {
14 if !meta.path().is_ident("stable_hasher") {
17 let mut any_attr = false;
18 if let Meta::List(list) = meta {
19 for nested in list.nested.iter() {
20 if let NestedMeta::Meta(meta) = nested {
21 if meta.path().is_ident("ignore") {
25 if meta.path().is_ident("project") {
26 if let Meta::List(list) = meta {
27 if let Some(nested) = list.nested.iter().next() {
28 if let NestedMeta::Meta(meta) = nested {
29 attrs.project = meta.path().get_ident().cloned();
39 panic!("error parsing stable_hasher");
46 pub fn hash_stable_generic_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
47 let generic: syn::GenericParam = parse_quote!(__CTX);
48 s.add_bounds(synstructure::AddBounds::Generics);
49 s.add_impl_generic(generic);
50 s.add_where_predicate(parse_quote! { __CTX: crate::HashStableContext });
51 let body = s.each(|bi| {
52 let attrs = parse_attributes(bi.ast());
55 } else if let Some(project) = attrs.project {
57 (&#bi.#project).hash_stable(__hcx, __hasher);
61 #bi.hash_stable(__hcx, __hasher);
66 let discriminant = match s.ast().data {
67 syn::Data::Enum(_) => quote! {
68 ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
70 syn::Data::Struct(_) => quote! {},
71 syn::Data::Union(_) => panic!("cannot derive on union"),
75 quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
81 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
89 pub fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
90 let generic: syn::GenericParam = parse_quote!('__ctx);
91 s.add_bounds(synstructure::AddBounds::Generics);
92 s.add_impl_generic(generic);
93 let body = s.each(|bi| {
94 let attrs = parse_attributes(bi.ast());
97 } else if let Some(project) = attrs.project {
99 (&#bi.#project).hash_stable(__hcx, __hasher);
103 #bi.hash_stable(__hcx, __hasher);
108 let discriminant = match s.ast().data {
109 syn::Data::Enum(_) => quote! {
110 ::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
112 syn::Data::Struct(_) => quote! {},
113 syn::Data::Union(_) => panic!("cannot derive on union"),
118 ::rustc_data_structures::stable_hasher::HashStable<
119 ::rustc_query_system::ich::StableHashingContext<'__ctx>,
126 __hcx: &mut ::rustc_query_system::ich::StableHashingContext<'__ctx>,
127 __hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
129 match *self { #body }