Skip to content

Implement Eq derive (#163) #479

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 14 commits into
base: master
Choose a base branch
from
Open

Implement Eq derive (#163) #479

wants to merge 14 commits into from

Conversation

tyranron
Copy link
Collaborator

@tyranron tyranron commented Jun 2, 2025

Part of #163

Synopsis

As our PartialEq derive infers trait bounds correctly for generic cases, we need also an Eq derive for doing the same.

Solution

derive(Eq) in std uses core::cmp::AssertParamIsEq to assert that fields' types are indeed Eq in its Eq::assert_receiver_is_total_eq() method. However, they both are #[doc(hidden)] and not a stable part of std, so we cannot reuse them blindly.

However, instead, we could just put the appropriate trait bounds in the where clause of the generated Eq impl:

impl Eq for Foo where FieldType: Eq {}

The only situation where this fails is when the field type uses the self-type recursively:

struct Foo(Option<Box<Foo>>);

In this case, we cannot generate Option<Box<Foo>>: Eq trait bound, because will hit the compilation error:

error[E0275]: overflow evaluating the requirement

For such fields, we need to do the similar static assertion as std does, that's why introduce our own derive_more::__private::AssertParamIsEq helper type. In contrast with std, we do generate these assertions (if needed) in an inherent method, rather than introducing a trait for it.

Checklist

@tyranron tyranron added this to the 2.1.0 milestone Jun 2, 2025
@tyranron tyranron self-assigned this Jun 2, 2025
@tyranron tyranron linked an issue Jun 2, 2025 that may be closed by this pull request
@tyranron tyranron marked this pull request as ready for review June 3, 2025 19:19
@tyranron tyranron enabled auto-merge (squash) June 3, 2025 19:21
@tyranron tyranron requested a review from JelteF June 3, 2025 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature idea: PartialEq and PartialOrd for the wrapped type
1 participant