As others have mentioned, this is a part of the way #[derive(Clone)]
was designed. The playground actually has an "expand macros" feature which will show you the code generated by our derive macro.
In this case, I got the following:
struct A<'a, T>(&'a T);
#[automatically_derived]
#[allow(unused_qualifications)]
impl <'a, T: Clone> Clone for A<'a, T> {
#[inline]
fn clone(&self) -> A<'a, T> {
match *self {
A(ref __self_0_0) =>
A(Clone::clone(&(*__self_0_0))),
}
}
}
impl <'a, T> Copy for A<'a, T> { }
(playground)
What the compiler "really" wants to generate is impl<'a, T> Clone for A<'a, T> where &'a T: Clone { ... }
, but that would mean we expose the type of A
's private fields as part of its public API. This is particularly not great when the field is more interesting than a simple reference.
The current design works for 99% of cases and doesn't need to break privacy, so you are forced to manually implement Clone
in that other 1% of cases.