Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with deriving Hash on generic enums #68

Merged
merged 2 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use proc_macro2;
use ast;
use attr;
use matcher;
use paths;
use syn;
use utils;

Expand All @@ -31,7 +32,7 @@ pub fn derive_eq(input: &ast::Input) -> proc_macro2::TokenStream {
/// Derive `PartialEq` for `input`.
pub fn derive_partial_eq(input: &ast::Input) -> Result<proc_macro2::TokenStream, String> {
let discriminant_cmp = if let ast::Body::Enum(_) = input.body {
let discriminant_path = discriminant_path();
let discriminant_path = paths::discriminant_path();

quote!((#discriminant_path(&*self) == #discriminant_path(&*other)))
} else {
Expand Down Expand Up @@ -340,13 +341,4 @@ fn ordering_path() -> syn::Path {
} else {
parse_quote!(::std::cmp::Ordering)
}
}

/// Return the path of the `discriminant` function, that is `::std::mem::discriminant`.
fn discriminant_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::mem::discriminant)
} else {
parse_quote!(::std::mem::discriminant)
}
}
}
22 changes: 12 additions & 10 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@ use proc_macro2;
use ast;
use attr;
use matcher;
use paths;
use syn;
use utils;

pub fn derive(input: &ast::Input) -> proc_macro2::TokenStream {
let hasher_trait_path = hasher_trait_path();
let hash_trait_path = hash_trait_path();

let discriminant = if let ast::Body::Enum(_) = input.body {
let discriminant = paths::discriminant_path();
Some(quote!(
#hash_trait_path::hash(&#discriminant(self), __state);
))
} else {
None
};

let body = matcher::Matcher::new(matcher::BindingStyle::Ref).build_arms(
input,
"__arg",
|arm_path, _, _, _, _, bis| {
|_, _, _, _, _, bis| {
let field_prints = bis.iter().filter_map(|bi| {
if bi.field.attrs.ignore_hash() {
return None;
Expand All @@ -32,16 +42,7 @@ pub fn derive(input: &ast::Input) -> proc_macro2::TokenStream {
}
});

let variant = if let ast::Body::Enum(_) = input.body {
Some(quote!(
#hash_trait_path::hash(&(#arm_path as u64), __state);
))
} else {
None
};

quote! {
#variant
#(#field_prints)*
}
},
Expand All @@ -64,6 +65,7 @@ pub fn derive(input: &ast::Input) -> proc_macro2::TokenStream {
fn hash<#hasher_ty_parameter>(&self, __state: &mut #hasher_ty_parameter)
where #hasher_ty_parameter: #hasher_trait_path
{
#discriminant
match *self {
#body
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod debug;
mod default;
mod hash;
mod matcher;
mod paths;
mod utils;

use proc_macro::TokenStream;
Expand Down
10 changes: 10 additions & 0 deletions src/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//! Contains some standard paths.

/// Return the path of the `discriminant` function, that is `::std::mem::discriminant`.
pub fn discriminant_path() -> syn::Path {
if cfg!(feature = "use_core") {
parse_quote!(::core::mem::discriminant)
} else {
parse_quote!(::std::mem::discriminant)
}
}
11 changes: 11 additions & 0 deletions tests/issue-67.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[cfg(feature = "use_core")]
extern crate core;

#[macro_use]
extern crate derivative;

#[derive(Derivative)]
#[derivative(Hash)]
enum Enumeration<T> {
_Variant(T),
}