Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign up#[derive(Clone)] should rely on Copy when possible #31085
Comments
This was referenced Jan 21, 2016
This comment has been minimized.
This comment has been minimized.
|
A more specific example https://play.rust-lang.org/?gist=ef17b9c31e01be5f65c2&version=nightly where |
This comment has been minimized.
This comment has been minimized.
|
We currently just desugar |
This comment has been minimized.
This comment has been minimized.
|
I would be quite ok specializing |
This comment has been minimized.
This comment has been minimized.
|
It’s a start, and it may even be the common case. |
This comment has been minimized.
This comment has been minimized.
|
I've had to use a macro to define all my structs in winapi just so I can get that efficient Copy-based Clone impl to improve compile times. |
steveklabnik
added
the
A-libs
label
Feb 2, 2016
This comment has been minimized.
This comment has been minimized.
|
Okay, so thinking aloud about this idea of special-casing From the code #[derive(Copy, Clone)]
struct S<T>(T);
fn main() { }you get (cleaned up) struct S<T>(T);
impl <T: Clone> Clone for S<T> {
fn clone(&self) -> S<T> {
match *self {
S(ref x) => S(Clone::clone(&*x)),
}
}
}
impl <T: Copy> Copy for S<T> { }
fn main() { }Now instead of the above impl<T: Clone + Copy> Clone for S<T> {
fn clone(&self) -> S<T> {
*self
}
}but now |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
I think you would need specialization to supply both the |
This comment has been minimized.
This comment has been minimized.
|
So we'll revisit this after specialization lands? |
This comment has been minimized.
This comment has been minimized.
|
You can do it well already for the case without type parameters |
This comment has been minimized.
This comment has been minimized.
|
Agreed. |
durka
referenced this issue
Feb 4, 2016
Merged
special-case #[derive(Copy, Clone)] with a shallow clone #31414
This comment has been minimized.
This comment has been minimized.
|
See #31414. |
SimonSapin commentedJan 21, 2016
When a type that is also
Copyhas#[derive(Clone)], the derived implementation should be:From #27750 (comment)
Maybe being
Copyis hard to determine exactly by the time#[derive(Clone)]needs to do its thing, but at least this could happen when both traits are in the same derive attribute?#[derive(Copy, Clone)]