Skip to content

Add skip_discriminant to skip discriminant comparison in derived PartialEq implementations #116

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ pub struct InputHash {
pub struct InputPartialEq {
/// The `bound` attribute if present and the corresponding bounds.
bounds: Option<Vec<syn::WherePredicate>>,
/// Skip discriminant comparison that happens before equating fields
skip_discriminant: bool,
}

#[derive(Debug, Default)]
Expand Down Expand Up @@ -336,6 +338,9 @@ impl Input {
for value in values;
"bound" => parse_bound(&mut partial_eq.bounds, value, errors),
"feature_allow_slow_enum" => (), // backward compatibility, now unnecessary
"skip_discriminant" => {
partial_eq.skip_discriminant = parse_boolean_meta_item(value, true, "skip_discriminant", errors);
}
}
}
"PartialOrd" => {
Expand Down Expand Up @@ -440,6 +445,12 @@ impl Input {
pub fn ord_on_enum(&self) -> bool {
self.ord.as_ref().map_or(false, |d| d.on_enum)
}

pub(crate) fn skip_discriminant_on_partial_eq(&self) -> bool {
self.partial_eq
.as_ref()
.map_or(false, |d| d.skip_discriminant)
}
}

impl Field {
Expand Down
8 changes: 6 additions & 2 deletions src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ pub fn derive_eq(input: &ast::Input) -> proc_macro2::TokenStream {
/// Derive `PartialEq` for `input`.
pub fn derive_partial_eq(input: &ast::Input) -> proc_macro2::TokenStream {
let discriminant_cmp = if let ast::Body::Enum(_) = input.body {
let discriminant_path = paths::discriminant_path();
if !input.attrs.skip_discriminant_on_partial_eq() {
let discriminant_path = paths::discriminant_path();

quote!((#discriminant_path(&*self) == #discriminant_path(&*other)))
quote!((#discriminant_path(&*self) == #discriminant_path(&*other)))
} else {
quote!(true)
}
} else {
quote!(true)
};
Expand Down
2 changes: 1 addition & 1 deletion src/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ impl<T: Fn (&ast::Field) -> bool> Matcher<T> {
quote! {
match (&#left_matched_expr, &#right_matched_expr) {
#t
_ => unreachable!(),
_ => false,
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions tests/derive-debug-generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,22 @@ fn main() {
assert_eq!(F(NoDebug).to_show(), "F".to_string());
assert_eq!(G(42, NoDebug).to_show(), "G(42)".to_string());
assert_eq!(J(NoDebug).to_show(), "J".to_string());
assert_eq!(&format!("{:?}", PhantomField::<NoDebug> { foo: Default::default() }), "PhantomField { foo: PhantomData }");
assert_eq!(&format!("{:?}", PhantomTuple::<NoDebug> { foo: Default::default() }), "PhantomTuple { foo: PhantomData }");
assert_eq!(
&format!(
"{:?}",
PhantomField::<NoDebug> {
foo: Default::default()
}
),
"PhantomField { foo: PhantomData<derive_debug_generics::NoDebug> }"
);
assert_eq!(
&format!(
"{:?}",
PhantomTuple::<NoDebug> {
foo: Default::default()
}
),
"PhantomTuple { foo: PhantomData<(derive_debug_generics::NoDebug,)> }"
);
}