Add HashStable_NoContext to simplify HashStable implementations in rustc_type_ir
This commit is contained in:
parent
baf4abff31
commit
426bc70ad6
10 changed files with 69 additions and 291 deletions
|
@ -64,6 +64,50 @@ pub(crate) fn hash_stable_generic_derive(
|
|||
)
|
||||
}
|
||||
|
||||
pub(crate) fn hash_stable_no_context_derive(
|
||||
mut s: synstructure::Structure<'_>,
|
||||
) -> proc_macro2::TokenStream {
|
||||
let generic: syn::GenericParam = parse_quote!(__CTX);
|
||||
s.add_bounds(synstructure::AddBounds::Fields);
|
||||
s.add_impl_generic(generic);
|
||||
let body = s.each(|bi| {
|
||||
let attrs = parse_attributes(bi.ast());
|
||||
if attrs.ignore {
|
||||
quote! {}
|
||||
} else if let Some(project) = attrs.project {
|
||||
quote! {
|
||||
(&#bi.#project).hash_stable(__hcx, __hasher);
|
||||
}
|
||||
} else {
|
||||
quote! {
|
||||
#bi.hash_stable(__hcx, __hasher);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let discriminant = match s.ast().data {
|
||||
syn::Data::Enum(_) => quote! {
|
||||
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
|
||||
},
|
||||
syn::Data::Struct(_) => quote! {},
|
||||
syn::Data::Union(_) => panic!("cannot derive on union"),
|
||||
};
|
||||
|
||||
s.bound_impl(
|
||||
quote!(::rustc_data_structures::stable_hasher::HashStable<__CTX>),
|
||||
quote! {
|
||||
#[inline]
|
||||
fn hash_stable(
|
||||
&self,
|
||||
__hcx: &mut __CTX,
|
||||
__hasher: &mut ::rustc_data_structures::stable_hasher::StableHasher) {
|
||||
#discriminant
|
||||
match *self { #body }
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn hash_stable_derive(mut s: synstructure::Structure<'_>) -> proc_macro2::TokenStream {
|
||||
let generic: syn::GenericParam = parse_quote!('__ctx);
|
||||
s.add_bounds(synstructure::AddBounds::Generics);
|
||||
|
|
|
@ -48,6 +48,13 @@ decl_derive!(
|
|||
[HashStable_Generic, attributes(stable_hasher)] =>
|
||||
hash_stable::hash_stable_generic_derive
|
||||
);
|
||||
decl_derive!(
|
||||
[HashStable_NoContext] =>
|
||||
/// `HashStable` implementation that has no `HashStableContext` bound and
|
||||
/// which adds `where` bounds for `HashStable` based off of fields and not
|
||||
/// generics. This is suitable for use in crates like `rustc_type_ir`.
|
||||
hash_stable::hash_stable_no_context_derive
|
||||
);
|
||||
|
||||
decl_derive!([Decodable] => serialize::decodable_derive);
|
||||
decl_derive!([Encodable] => serialize::encodable_derive);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue