Derive of PartialEq and Eq


#1
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Regular<T: Debug + Clone> {
    value: T,
}

It compiles, but it shouldn’t, so why is it happening?


#2

I believe derive generates code that looks like the following:

impl<T: PartialEq> PartialEq<Regular<T>> for Regular<T> {
    ...
}

impl<T: Eq> Eq<Regular<T>> for Regular<T> {
    ....
}

#3

Why shouldn’t it?


#4

It makes sense


#5

Note that sometimes this is overly constrained – for instance:

#[derive(Clone, Copy)]
struct Ref<'a, T> {
    inner: &'a T,
}

The derived impls will require T: Clone and T: Copy respectively, even though &T can be cloned and copied for any T whatsoever. You can’t use #[derive] for the more general case – issue 26925.