←back to thread

128 points RGBCube | 1 comments | | HN request time: 0.212s | source
Show context
loa_in_ ◴[] No.44497772[source]
Automatically deriving Clone is a convenience. You can and should write your own implementation for Clone whenever you need it and automatically derived implementation is insufficient.
replies(3): >>44498124 #>>44499705 #>>44500307 #
josephg ◴[] No.44498124[source]
But this issue makes it confusing & surprising when an automatically derived clone is sufficient and when its not. Its a silly extra rule that you have to memorise.

By the way, this issue also affects all of the other derivable traits in std - including PartialEq, Debug and others. Manually deriving all this stuff - especially Debug - is needless pain. Especially as your structs change and you need to (or forget to) maintain all this stuff.

Elegant software is measured in the number of lines of code you didn't need to write.

replies(4): >>44498182 #>>44498258 #>>44498373 #>>44498385 #
0rzech ◴[] No.44498182[source]
It's surprising up to the moment the compilation error tells you that all of the members have to implement derived trait.

Nevertheless, it would be cool to be able to add #[noderive(Trait)] or something to a field not to be included in automatic trait implementation. Especially that sometimes foreign types do not implement some traits and one has to implement lots of boilerplate just to ignore fields of those types.

I know of Derivative crate [1], but it's yet another dependency in an increasingly NPM-like dependency tree of a modern Rust project.

All in all, I resort to manual trait implementations when needed, just as GP.

[1] https://crates.io/crates/derivative

replies(2): >>44498752 #>>44499987 #
1. 0rzech ◴[] No.44498752[source]
Apparently, Derivative is unmaintained [1], but there is Derive_more [2], Educe [3] and Derive-where [4], if anyone is interested.

[1] https://rustsec.org/advisories/RUSTSEC-2024-0388.html

[2] https://crates.io/crates/derive_more

[3] https://crates.io/crates/educe

[4] https://crates.io/crates/derive-where