]> git.lizzy.rs Git - rust.git/commitdiff
Docs: impls of PartialEq/PartialOrd/Ord must agree
authorStjepan Glavina <stjepang@gmail.com>
Sat, 27 May 2017 15:00:11 +0000 (17:00 +0200)
committerStjepan Glavina <stjepang@gmail.com>
Sat, 27 May 2017 15:15:32 +0000 (17:15 +0200)
src/libcollections/binary_heap.rs
src/libcore/cmp.rs

index a5a2f70492dc97daaa05e5ef2ee1a770f32173dc..0094804631b08e468498af41df0e148447c25d4b 100644 (file)
 //! // instead of a max-heap.
 //! impl Ord for State {
 //!     fn cmp(&self, other: &State) -> Ordering {
-//!         // Notice that the we flip the ordering here
+//!         // Notice that the we flip the ordering on costs.
+//!         // In case of a tie we compare positions - this step is necessary
+//!         // to make implementations of `PartialEq` and `Ord` consistent.
 //!         other.cost.cmp(&self.cost)
+//!             .then_with(|| self.position.cmp(&other.position))
 //!     }
 //! }
 //!
index cc066099cf8b010167489451110c7fcb7feb6ab0..d523e40b49ed53301f3e38c0932cd1cb73d7673f 100644 (file)
 /// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and
 /// only if `a != b`.
 ///
+/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with
+/// each other. It's easy to accidentally make them disagree by deriving some
+/// of the traits and manually implementing others.
+///
 /// An example implementation for a domain in which two books are considered
 /// the same book if their ISBN matches, even if the formats differ:
 ///
@@ -343,6 +347,10 @@ pub fn then_with<F: FnOnce() -> Ordering>(self, f: F) -> Ordering {
 /// Then you must define an implementation for `cmp()`. You may find it useful to use
 /// `cmp()` on your type's fields.
 ///
+/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
+/// easy to accidentally make them disagree by deriving some of the traits and manually
+/// implementing others.
+///
 /// Here's an example where you want to sort people by height only, disregarding `id`
 /// and `name`:
 ///
@@ -431,8 +439,8 @@ fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
 ///
 /// ## How can I implement `PartialOrd`?
 ///
-/// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated
-/// from default implementations.
+/// `PartialOrd` only requires implementation of the `partial_cmp` method, with the others
+/// generated from default implementations.
 ///
 /// However it remains possible to implement the others separately for types which do not have a
 /// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
@@ -440,6 +448,10 @@ fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
 ///
 /// `PartialOrd` requires your type to be `PartialEq`.
 ///
+/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
+/// easy to accidentally make them disagree by deriving some of the traits and manually
+/// implementing others.
+///
 /// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`:
 ///
 /// ```