If by "check" you mean "I should get a compile error if I try to create a Complex<T>
where Eq
couldn't be derived", you would need to add the bounds you wanted to act this way to the type parameter. For example the bound below prevents the creation of Complex<f32>
.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Complex<T: Eq> { re: T, im: T }
// ^^^^^
But this generally isn't recommended as you will have to repeat that bound whenever you do something generic with the struct.
impl<T: Eq> Complex<T> { ... }
More typically you'd just assert the bound (or Complex<T>: Eq
) where it matters.
If you meant there should only be PartialEq
if there's Eq
, bulit-in derive can't help, you'll have to write it yourself.
impl<T: Eq> Eq for Complex<T> {}
impl<T: Eq> PartialEq for Complex<T> {
fn eq(&self, rhs: &Complex<T>) -> bool {
self.re.eq(&rhs.re) && self.im.eq(&rhs.im)
}
}
If by "check" you meant "check that the PartialEq
matches the intentions of Eq
", there is no way to do that in the general case. Hence this part of the docs:
This property cannot be checked by the compiler, and therefore Eq
implies PartialEq
, and has no extra methods.
Violating this property is a logic error. The behavior resulting from a logic error is not specified, but users of the trait must ensure that such logic errors do not result in undefined behavior. This means that unsafe
code must not rely on the correctness of these methods.
(The last paragraph is necessary as Eq
is not an unsafe
trait.)
If you meant something else, I'm not sure what you meant.
Not unconditionally -- read @sfackler's reply again and note the where T: Eq
part. That's how the build-in derive works.