-
Notifications
You must be signed in to change notification settings - Fork 527
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
feat: derive Copy trait for messages where possible #950
Conversation
2546fc8
to
5952163
Compare
5952163
to
fe5cd31
Compare
Would it be possible to make this configurable? I personally like to avoid automatic Copy derives to avoid implicit cloning. |
What do you mean with implicit cloning? The trait The docs says: When should my type be Copy? Generally speaking, if your type can implement Copy, it should. |
This example is perhaps trivial, but my experience is that these things can become really tricky to pin down in large projects. fn main() {
let copyable = 1;
read(©able);
}
fn read(x: &u32) {
update(*x); // implicit clone
dbg!(x); // 1
}
fn update(mut x: u32) {
x += 1;
dbg!(x); // 2
} |
I would not call that an implicit clone, as you explicitly deref |
Well things get a bit tricky because derefmut and deref both use the same symbol Sure, my example was maybe not the best. I'm also just playing a bit of devil's advocate. There's still the basic case of implicit cloning mentioned in the link you posted above: #[derive(Copy, Clone)]
struct GiantStruct(u32);
fn main() {
let copyable0 = GiantStruct(1);
let copyable1 = copyable0;
} |
Well, that is how the language is supposed to work, right? If you want two variables pointing to the same instance, then you want to take a reference: Sorry, but I don't understand in what situation you don't want an |
You're probably right. I usually want the compiler to tell me that I must copy things if that's what I'm trying to do. The Rust API Guidelines book also recommends implementing |
Rust primitive types can be copied by simply copying the bits. Rust structs can also have this property by deriving the Copy trait. Automatically derive Copy for: - messages that only have fields with primitive types - the Rust enum for one-of fields - messages whose field type are messages that also implement Copy Generated code for Protobuf enums already derives Copy.
449b016
to
93a39d8
Compare
Clippy reports: warning: using `clone` on type `Timestamp` which implements the `Copy` trait
93a39d8
to
4ca89a0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice work! I like this a lot. Do you think we should release this as part of a breaking change or not?
I agree this is a breaking change. |
Rust primitive types can be copied by simply copying the bits. Rust structs can also have this property by deriving the Copy trait.
Automatically derive Copy for:
Generated code for Protobuf enums already derives Copy.