Skip to content

Commit f542136

Browse files
author
Stjepan Glavina
committed
Docs: impls of PartialEq/PartialOrd/Ord must agree
1 parent 0aeb9c1 commit f542136

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
@@ -42,8 +42,11 @@
4242
//! // instead of a max-heap.
4343
//! impl Ord for State {
4444
//! fn cmp(&self, other: &State) -> Ordering {
45-
//! // Notice that the we flip the ordering here
45+
//! // Notice that the we flip the ordering on costs.
46+
//! // In case of a tie we compare positions - this step is necessary
47+
//! // to make implementations of `PartialEq` and `Ord` consistent.
4648
//! other.cost.cmp(&self.cost)
49+
//! .then_with(|| self.position.cmp(&other.position))
4750
//! }
4851
//! }
4952
//!

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
///
@@ -343,6 +347,10 @@ impl Ordering {
343347
/// Then you must define an implementation for `cmp()`. You may find it useful to use
344348
/// `cmp()` on your type's fields.
345349
///
350+
/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
351+
/// easy to accidentally make them disagree by deriving some of the traits and manually
352+
/// implementing others.
353+
///
346354
/// Here's an example where you want to sort people by height only, disregarding `id`
347355
/// and `name`:
348356
///
@@ -431,15 +439,19 @@ impl PartialOrd for Ordering {
431439
///
432440
/// ## How can I implement `PartialOrd`?
433441
///
434-
/// PartialOrd only requires implementation of the `partial_cmp` method, with the others generated
435-
/// from default implementations.
442+
/// `PartialOrd` only requires implementation of the `partial_cmp` method, with the others
443+
/// generated from default implementations.
436444
///
437445
/// However it remains possible to implement the others separately for types which do not have a
438446
/// total order. For example, for floating point numbers, `NaN < 0 == false` and `NaN >= 0 ==
439447
/// false` (cf. IEEE 754-2008 section 5.11).
440448
///
441449
/// `PartialOrd` requires your type to be `PartialEq`.
442450
///
451+
/// Implementations of `PartialEq`, `PartialOrd`, and `Ord` *must* agree with each other. It's
452+
/// easy to accidentally make them disagree by deriving some of the traits and manually
453+
/// implementing others.
454+
///
443455
/// If your type is `Ord`, you can implement `partial_cmp()` by using `cmp()`:
444456
///
445457
/// ```

0 commit comments

Comments
 (0)