Skip to content

Commit 423b410

Browse files
Rollup merge of rust-lang#42260 - stjepang:document-cmp-traits-agreement, r=alexcrichton
Docs: impls of PartialEq/PartialOrd/Ord must agree Fixes rust-lang#41270. This PR brings two improvements to the docs: 1. Docs for `PartialEq`, `PartialOrd`, and `Ord` clarify that their implementations must agree. 2. Fixes a subtle bug in the Dijkstra example for `BinaryHeap`, where the impls are inconsistent. Thanks @Rufflewind for spotting the bug! r? @alexcrichton cc @frankmcsherry
2 parents 906c9bc + f542136 commit 423b410

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/libcollections/binary_heap.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@
4343
//! // instead of a max-heap.
4444
//! impl Ord for State {
4545
//! fn cmp(&self, other: &State) -> Ordering {
46-
//! // Notice that the we flip the ordering here
46+
//! // Notice that the we flip the ordering on costs.
47+
//! // In case of a tie we compare positions - this step is necessary
48+
//! // to make implementations of `PartialEq` and `Ord` consistent.
4749
//! other.cost.cmp(&self.cost)
50+
//! .then_with(|| self.position.cmp(&other.position))
4851
//! }
4952
//! }
5053
//!

src/libcore/cmp.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ use self::Ordering::*;
6767
/// the rule that `eq` is a strict inverse of `ne`; that is, `!(a == b)` if and
6868
/// only if `a != b`.
6969
///
70+
/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with
71+
/// each other. It's easy to accidentally make them disagree by deriving some
72+
/// of the traits and manually implementing others.
73+
///
7074
/// An example implementation for a domain in which two books are considered
7175
/// the same book if their ISBN matches, even if the formats differ:
7276
///
@@ -386,6 +390,10 @@ impl<T: Ord> Ord for Reverse<T> {
386390
/// Then you must define an implementation for `cmp()`. You may find it useful to use
387391
/// `cmp()` on your type's fields.
388392
///
393+
/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
394+
/// easy to accidentally make them disagree by deriving some of the traits and manually
395+
/// implementing others.
396+
///
389397
/// Here's an example where you want to sort people by height only, disregarding `id`
390398
/// and `name`:
391399
///
@@ -474,15 +482,19 @@ impl PartialOrd for Ordering {
474482
///
475483
/// ## How can I implement `PartialOrd`?
476484
///
477-
/// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated
478-
/// from default implementations.
485+
/// `PartialOrd` only requires implementation of the `partial_cmp` method, with the others
486+
/// generated from default implementations.
479487
///
480488
/// However it remains possible to implement the others separately for types which do not have a
481489
/// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
482490
/// false` (cf. IEEE 754-2008 section 5.11).
483491
///
484492
/// `PartialOrd` requires your type to be `PartialEq`.
485493
///
494+
/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
495+
/// easy to accidentally make them disagree by deriving some of the traits and manually
496+
/// implementing others.
497+
///
486498
/// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`:
487499
///
488500
/// ```

0 commit comments

Comments
 (0)