Rust crate for "ignore this field for Eq/PartialEq/Hash"?

I have a field of a struct/tuple that I want to be ignored when auto deriving Hash/Eq/PartialEq.

Is there a crate that provides this functionality.

It would be used something like:

pub struct Foo {
  x: i32,
  y: Ignore<BlahBlah>,
}

then, for purposes of Hash/Eq/PartialEq, the above Foo should completely ignore the y field.

AFAIK, you can't affect deriving in any way which is not handled by derive macro itself. In this case, you'll likely want to make your own custom derive (or implement the trait manually).

I know close to nothing about how derive macros work. How certain are you of the above? I thought it would be possible to build something like:

pub struct<T> Ignored {
  inner: T,
}

impl <T> Hash/Eq/PartialEq for Ignored<T>  { ... }

Well, you certainly can make a PartialEq implementation which will always return true, the Hash implementation which will always return the same constant, etc. Effectively, this will make the implementations of containing struct ignore this field (since the result will be independent of the field's value). Is this what you want?

Having PartialEq always return true + having Hash do nothing is what I want.

  1. Clearly my question above was not clear. What did you think I want ?

  2. Now that (I think) we have the same problem statement in mind, is there any reason to believe this won't work with derive ?

I first thought that you want the implementation to not even access these fields. But you're right, from the external point of view it would be the same as your real goal.

Derives for most traits work structurally, i.e. they refer to the implementations of the same trait on fields. So, the described way should work.

With derivative crate:

use derivative::Derivative;

#[derive(Derivative)]
#[derivative(PartialEq, Eq, Hash)]
pub struct Foo {
    x: i32,
    #[derivative(PartialEq="ignore")]
    #[derivative(Hash="ignore")]
    y: BlahBlah,
}
2 Likes