- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
          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
Changes from 3 commits
30033b4
              cdbcd72
              623317e
              c72bb70
              fadc961
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -139,6 +139,30 @@ mod uninit; | |
| /// // Note: With the manual implementations the above line will compile. | ||
| /// ``` | ||
| /// | ||
| /// ## `Clone` and `PartialEq`/`Eq` | ||
| /// `Clone` is intended for the duplication of objects. Consequently, when implementing | ||
| /// both `Clone` and [`PartialEq`], the following property is expected to hold: | ||
| /// ```text | ||
| /// x == x -> x.clone() == x | ||
| /// ``` | ||
| /// In other words, if an object compares equal to itself, | ||
| /// its clone must also compare equal to the original. | ||
| /// | ||
| /// For types that also implement [`Eq`] – for which `x == x` always holds – | ||
| /// this implies that `x.clone() == x` must always be true. | ||
| /// Standard library collections such as | ||
| /// [`HashMap`], [`HashSet`], [`BTreeMap`], [`BTreeSet`] and [`BinaryHeap`] | ||
| /// rely on their keys respecting this property for correct behavior. | ||
| /// | ||
| /// This property is automatically satisfied when deriving both `Clone` and [`PartialEq`] | ||
| /// using `#[derive(Clone, PartialEq)]` or when additionally deriving [`Eq`] | ||
| /// using `#[derive(Clone, PartialEq, Eq)]`. | ||
|         
                  gewitternacht marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| /// | ||
| /// 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. | ||
|          | ||
| /// | ||
| /// ## Additional implementors | ||
| /// | ||
| /// In addition to the [implementors listed below][impls], | ||
|  | @@ -152,6 +176,11 @@ mod uninit; | |
| /// (even if the referent doesn't), | ||
| /// while variables captured by mutable reference never implement `Clone`. | ||
| /// | ||
| /// [`HashMap`]: ../../std/collections/struct.HashMap.html | ||
| /// [`HashSet`]: ../../std/collections/struct.HashSet.html | ||
| /// [`BTreeMap`]: ../../std/collections/struct.BTreeMap.html | ||
| /// [`BTreeSet`]: ../../std/collections/struct.BTreeSet.html | ||
| /// [`BinaryHeap`]: ../../std/collections/struct.BinaryHeap.html | ||
| /// [impls]: #implementors | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
| #[lang = "clone"] | ||
|  | ||
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 unclear whether this should apply to
PartialEq.However this definitely needs to be extended to include
HashandOrdsinceHashMapandBTreeMaprely on this. It could also have vague wording that this may extend to other traits as well.Uh oh!
There was an error while loading. Please reload this page.
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.
Do you mean this in the sense that the property cannot be required of
PartialEqandCloneas we might have to deliberalty break it in some contexts? Or in the sense that we just don't know of anything that depends on this property forPartialEq?In case of the latter, I would argue that the documentation of
Clone– "creation of a duplicate value" – very strongly suggests that I get the "same" thing by cloning, andPartialEqdefines "sameness" of objects. E.g., iff == 1.0_f64, I expect thatf.clone() == 1.0_f64, too. But without the propertyx == x -> x.clone() == x, this is never explicitly guaranteed anywhere.I only wrote about
PartialEq/Eqbecausex.clone() == xalready ensures thatclonepreserves the hash and ordering of elements, when put together with the guarantees that are required ofHash(see "HashandEq") andOrd(due to the conditions forPartialOrd). But I could make this interaction explicit.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.
Now that I think about it, you are correct and
PartialEqis the right place to introduce this requirement.