I get a compilation error saying I didn't implement PartialEq, but it's implemented


#1

Hi there,

I am having unusual trouble when trying to implement the PartialEq trait. I have some structs in my project, most of them with PartialEq implemented. The issue is that for wone of them it seems that it does not work, it says: ```error: the trait core::cmp::PartialEq<ntru::encparams::NtruEncParams> is not implemented for the type `&ntru::encparams::NtruEncParams````

The trait is implemented and can be seen here: https://github.com/FractalGlobal/ntru-rs/blob/develop/src/encparams.rs#L67

How can I fix it?


#2

What’s the example where you’re using it? When you use the operator, like a == b, those should be direct values, and then eq() will be called by reference. Or both sides can be references, and they will be forwarded by one of these depending on mutability:

impl<'a, 'b, A, B> PartialEq<&'b B> for &'a A where B: ?Sized, A: PartialEq<B> + ?Sized
impl<'a, 'b, A, B> PartialEq<&'b mut B> for &'a mut A where A: PartialEq<B> + ?Sized, B: ?Sized
impl<'a, 'b, A, B> PartialEq<&'b mut B> for &'a A where A: PartialEq<B> + ?Sized, B: ?Sized
impl<'a, 'b, A, B> PartialEq<&'b B> for &'a mut A where A: PartialEq<B> + ?Sized, B: ?Sized

Your error seems to indicate you have a reference on the left and a value on the right, and I don’t think there’s a forwarding rule for that.

fn main() {
  println!("{}", 0i32 == 1i32); // ok
  
  println!("{}", &0i32 == &1i32); // ok
  
  println!("{}", &0i32 == 1i32);
  // error: the trait `core::cmp::PartialEq<i32>` is not implemented for the type `&i32`
  
  println!("{}", 0i32 == &1i32);
  // error: mismatched types:
  //  expected `i32`,
  //     found `&i32`
  // (expected i32,
  //     found &-ptr) [E0308]
}

play


#3

Oh, true. I was comparing a reference with an actual value :confused: Thanks!