-
Notifications
You must be signed in to change notification settings - Fork 13.6k
document assumptions about Clone
and Eq
traits
#144330
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
base: master
Are you sure you want to change the base?
Conversation
rustbot has assigned @Mark-Simulacrum. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
library/core/src/clone.rs
Outdated
/// Violating this property is a logic error. The behavior resulting from a logic error is not | ||
/// specified, but users of the trait must ensure that such logic errors do *not* result in | ||
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these | ||
/// methods. |
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.
It's a bit confusing to say "these methods" here IMO - maybe "this property"?
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.
I agree, I could change it to "... rely on this property being satisfied."
The reason behind the "correctness of these methods" wording is that it is phrased like this in the documentation on Hash
and Eq
, as well as the Eq
and PartialEq
trait documentation. I found it confusing there, too, but I thought it was just because I lacked experience with the terminology.
So, would you say the wording in these other places is confusing, too? And if so, should I make it part of this PR to change it there, too? (my first time doing a PR like this, so I'm still learning best practices)
Moving into libs-api queue for FCP, since this is a new property (even if likely already required implicitly) so merits some team discussion. |
Most standard library collections break if
Clone
has a non-standard implementation which violatesx.clone() == x
. Here the resulting broken behaviour of different collections is shown. I originally created an issue at rust-lang/hashbrown#629, but the conclusion there was thatx.clone()
resulting in an object that compares equal to the original one is probably a very universal assumption. However, this assumption is (to my knowledge) not documented anywhere.I propose to make this assumption explicit in the
Clone
trait documentation. The property that seems the most reasonable to me is the following: When implementing bothClone
andPartialEq
, thenis expected to hold. This way, when also implementing
Eq
, it automatically follows thatx.clone() == x
has to hold, which should be enough for the collections to not break. At the same time, the property also works for the "normal" elements of a type withPartialEq
. For the wording, I tried to follow theHash
andEq
documentation.As I am fairly new to Rust, it might well be that this property cannot be generally expected – it seems reasonable to me, but any counter-examples or critique, both content- and wording-wise, would be very welcome. If the property turns out to be too general, I would suggest to at least document the assumption of
x.clone() == x
for the collections somehow.An additional thought of mine:
If it is indeed generally expected that
x == x -> x.clone() == x
, then, for the sake of completeness, one could also define thatx != x -> y != y for y = x.clone()
should hold, i.e., that an object that did not compare equal to itself before cloning, should also not compare equal to itself afterwards.