I'm working on a custom pointer type, and I'm running into a weird result. I'd like my pointers to be Copy no matter what they point to, but it seems impossible to do with a plain #[derive(Clone, Copy)].
Here's a minimal example:
#[derive(Clone, Copy)]
struct Foo<T>(*const T);
fn bar<T>(foo: &Foo<T>) -> Foo<T> {
*foo // compile error - move out of non-Copy type
}
I suspect that the cause of this issue is that the derive macro ignores the fact that *const T is always copiable, and so rejects making Foo<T> copiable. Is there a way to make it so that Foo<T> can be Copy?
It is magic, but it's a bit different one. You can implement Copy manually for types which only include Copy types while #[derive(Clone, Copy)] adds additional restrictions.
This is important because for Copy type Clonehave to do a bitwise copy, nothing else is allowed.
That's not true. It's bad practice if they don't do exactly the same thing, but the compiler can't enforce this. The documentation you linked to doesn't say that, either ā it only says that a manual Clone impl for a Copy type can simply return *self for ease of implementation.
However, an abomination like this still compiles and runs.
Given the common usage of specialization for optimizing .clone() into bitwise copy, in the standard library, Iām a bit surprised to find no stronger wording against such practices in the docs.