Super trait of PartialEq or Eq?

Is it possible to say a trait is a super trait of PartialEq or Eq?

What are you trying to do?

I have a unique id for a struct that is the true thing to compare with for equality. But, for testing purposes, I want to be able to compare every field of the struct to verify intermediate processes match the expected value. Originally, I had this trait just be a super trait of PartialEq but reading the documentation, I’ve realized that’s only supposed to be used when equality is not exact.

So, you have something like this?

struct Foo {
    id: u32,
    field: String,
    bar: Vec<i32>

impl PartialEq for Foo {
    fn eq(&self, other: &Self) -> bool { ==

Where does the other trait come into play?

In addition I have this.

trait AllFieldsEq: PartialEq {
    fn eq(&self, other: &Self) -> bool;

impl AllFieldsEq for Foo {
    fn eq(&self, other: &Self) -> bool { == && self.field == other.field && ==

but would prefer to be able to support both Eq and PartialEq (if that is possible)

Really, no. You can only have one version of == for a type, and that’s done with PartialEq. The main reason Eq exists is to allow you to assert that it’s a full equality relation, and it’s assumed that Eq and PartialEq will always be equivalent.

If you want a different version for tests, then just give it a name foo.deep_eq(bar) or something. Or use a newtype wrapper.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.