Reputation: 13079
How can I compare generic structs if the type parameter T
implements PartialEq
? I tried the following, but it doesn't compile. I understand I can just make Foobar<T>
derive Eq
and so on, but is it possible to have Foobar
"inherit" or take the PartialEq
from T
somehow? I'm guessing something like that is already made for Vec
, e.g. you can compare Vec
s if and only if T
implements PartialEq
.
struct Foobar<T> {
foobar: T
}
fn x() -> bool {
let a = Foobar{foobar: 1};
let b = Foobar{foobar: 2};
a == b
}
Upvotes: 1
Views: 805
Reputation: 42698
You can implement the trait by constraining the implementation when T
is of PartialEq
too:
impl<T> PartialEq for Foobar<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
self.foobar == other.foobar
}
}
Although the simplest solution would be to derive directly:
#[derive(PartialEq)]
struct Foobar<T> {
foobar: T
}
It will make your inner types constrained by that trait also, it means that you would not be able to instantiate a Foobar
struct with something that is not PartialEq
:
#[derive(PartialEq)]
struct Foobar<T> {
foobar: T
}
struct NonPartialEq {}
fn main() {
let a = Foobar{foobar: NonPartialEq {}};
let b = Foobar{foobar: NonPartialEq {}};
assert!(a != b)
}
Something like this would not compile with:
error[E0369]: binary operation `!=` cannot be applied to type `Foobar<NonPartialEq>`
--> src/main.rs:11:15
|
11 | assert!(a != b)
| - ^^ - Foobar<NonPartialEq>
| |
| Foobar<NonPartialEq>
|
= note: an implementation of `std::cmp::PartialEq` might be missing for `Foobar<NonPartialEq>`
So implementing it by hanb with the constrain allow you to use your struct for any type, but you will still be able to use PartialEq
when the inner type is so.
Upvotes: 1
Reputation: 7927
Regarding Vec<_>
it's useful to scroll its documentation to see, that there is a trait implementation that propagates Eq
just as you want to.
impl<T, A> Eq for Vec<T, A> where
T: Eq,
A: Allocator,
So you need to do the same:
struct Foobar<T> {
foobar: T
}
impl<T> PartialEq for Foobar<T> where T: PartialEq {
fn eq(&self, other: &Self) -> bool {
self.foobar == other.foobar
}
}
impl<T> Eq for Foobar<T> where T: Eq {}
fn x() -> bool {
let a = Foobar{foobar: 1};
let b = Foobar{foobar: 2};
a == b
}
fn main() {
println!("{}", x()); // false
}
Upvotes: 1