-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#[derive(Clone)] should rely on Copy when possible #31085
Comments
A more specific example https://play.rust-lang.org/?gist=ef17b9c31e01be5f65c2&version=nightly where |
We currently just desugar |
I would be quite ok specializing |
It’s a start, and it may even be the common case. |
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. |
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 |
I think you would need specialization to supply both the |
So we'll revisit this after specialization lands? |
You can do it well already for the case without type parameters |
Agreed. |
See #31414. |
special-case #[derive(Copy, Clone)] with a shallow clone If a type is Copy then its Clone implementation can be a no-op. Currently `#[derive(Clone)]` generates a deep clone anyway. This can lead to lots of code bloat. This PR detects the case where Copy and Clone are both being derived (the general case of "is this type Copy" can't be determined by a syntax extension) and generates the shallow Clone impl. Right now this can only be done if there are no type parameters (see #31085 (comment)), but this restriction can be removed after specialization. Fixes #31085.
When a type that is also
Copy
has#[derive(Clone)]
, the derived implementation should be:From #27750 (comment)
Maybe being
Copy
is 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)]
The text was updated successfully, but these errors were encountered: