Skip to content

Commit a7abd13

Browse files
committed
make Ord doc style consistent with the other two; explain which properties are ensured by default impls
1 parent 1cfc187 commit a7abd13

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

library/core/src/cmp.rs

+26-17
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ use self::Ordering::*;
3535
/// so floating point types implement `PartialEq` but not [`trait@Eq`].
3636
///
3737
/// Implementations must ensure that `eq` and `ne` are consistent with each other:
38-
/// `a != b` if and only if `!(a == b)`.
39-
/// This is ensured by the default implementation of `ne`.
38+
///
39+
/// - `a != b` if and only if `!(a == b)`
40+
/// (ensured by the default implementation).
41+
///
4042
/// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
4143
/// be consistent with `PartialEq` (see the documentation of those traits for the exact
4244
/// requirememts). It's easy to accidentally make them disagree by deriving some of the traits and
@@ -633,22 +635,25 @@ impl<T: Clone> Clone for Reverse<T> {
633635

634636
/// Trait for types that form a [total order](https://en.wikipedia.org/wiki/Total_order).
635637
///
636-
/// An order is a total order if it is (for all `a`, `b` and `c`):
638+
/// Implementations must ensure to be consistent with the [`PartialOrd`] implementation, and that
639+
/// `max`, `min`, and `clamp` are consistent with `cmp`:
637640
///
638-
/// - total and asymmetric: exactly one of `a < b`, `a == b` or `a > b` is true; and
639-
/// - transitive: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
641+
/// - `partial_cmp(a, b) == Some(cmp(a, b))`.
642+
/// - `max(a, b) == max_by(a, b, cmp)` (ensured by the default implementation).
643+
/// - `min(a, b) == min_by(a, b, cmp)` (ensured by the default implementation).
644+
/// - For `a.clamp(min, max)`, see the [method docs](#method.clamp)
645+
/// (ensured by the default implementation).
640646
///
641-
/// Implementations of [`PartialEq`], [`PartialOrd`], and `Ord` *must* agree with each other (for
642-
/// all `a` and `b`):
647+
/// It's easy to accidentally make `cmp` and `partial_cmp` disagree by
648+
/// deriving some of the traits and manually implementing others.
643649
///
644-
/// - `a.partial_cmp(b) == Some(a.cmp(b))`
645-
/// - `a.cmp(b) == Ordering::Equal` if and only if `a == b`
646-
/// (already follows from the above and the requirements of `PartialOrd`)
650+
/// ## Corollaries
647651
///
648-
/// It's easy to accidentally make them disagree by
649-
/// deriving some of the traits and manually implementing others.
652+
/// From the above and the requirements of `PartialOrd`, it follows that `<` defines a strict total order.
653+
/// This means that for all `a`, `b` and `c`:
650654
///
651-
/// Furthermore, the `max`, `min`, and `clamp` methods of this trait must be consistent with `cmp`.
655+
/// - exactly one of `a < b`, `a == b` or `a > b` is true; and
656+
/// - `<` is transitive: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
652657
///
653658
/// ## Derivable
654659
///
@@ -839,10 +844,14 @@ impl PartialOrd for Ordering {
839844
/// the following sense:
840845
///
841846
/// - `a == b` if and only if `partial_cmp(a, b) == Some(Equal)`.
842-
/// - `a < b` if and only if `partial_cmp(a, b) == Some(Less)`.
843-
/// - `a > b` if and only if `partial_cmp(a, b) == Some(Greater)`.
844-
/// - `a <= b` if and only if `a < b || a == b`.
845-
/// - `a >= b` if and only if `a > b || a == b`.
847+
/// - `a < b` if and only if `partial_cmp(a, b) == Some(Less)`
848+
/// (ensured by the default implementation).
849+
/// - `a > b` if and only if `partial_cmp(a, b) == Some(Greater)`
850+
/// (ensured by the default implementation).
851+
/// - `a <= b` if and only if `a < b || a == b`
852+
/// (ensured by the default implementation).
853+
/// - `a >= b` if and only if `a > b || a == b`
854+
/// (ensured by the default implementation).
846855
/// - `a != b` if and only if `!(a == b)` (already part of `PartialEq`).
847856
///
848857
/// If [`Ord`] is also implemented for `Self` and `Rhs`, it must also be consistent with

0 commit comments

Comments
 (0)