Derive of PartialEq and Eq

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

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

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> {

Why shouldn't it?


It makes sense

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.

1 Like