/// If [`PartialOrd`] or [`Ord`] are also implemented for `Self` and `Rhs`, their methods must also
/// requirements). It's easy to accidentally make them disagree by deriving some of the traits and
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// Upholding the requirements stated above can become tricky when one crate implements `PartialEq`
/// for a type of another crate (i.e., to allow comparing one of its own types with a type from the
/// standard library). The recommendation is to never implement this trait for a foreign type. In
/// other words, such a crate should do `impl PartialEq<ForeignType> for LocalType`, but it should
/// This avoids the problem of transitive chains that criss-cross crate boundaries: for all local
/// types `T`, you may assume that no other crate will add `impl`s that allow comparing `T == U`. In
/// other words, if other crates add `impl`s that allow building longer transitive chains `U1 == ...
/// == T == V1 == ...`, then all the types that appear to the right of `T` must be types that the
/// crate defining `T` already knows about. This rules out transitive chains where downstream crates
/// Not having such foreign `impl`s also avoids forward compatibility issues where one crate adding
/// The primary difference to [`PartialEq`] is the additional requirement for reflexivity. A type
/// This property cannot be checked by the compiler, and therefore `Eq` is a trait without methods.
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// Floating point types such as [`f32`] and [`f64`] implement only [`PartialEq`] but *not* `Eq`
/// This trait can be used with `#[derive]`. When `derive`d, because `Eq` has no extra methods, it
/// equivalence relation. Note that the `derive` strategy requires all fields are `Eq`, which isn't
/// If you cannot use the `derive` strategy, specify that your type implements `Eq`, which has no
#[unstable(feature = "derive_eq", reason = "deriving hack, should not be public", issue = "none")]
// <https://github.com/llvm/llvm-project/blob/60486292b79885b7800b082754153202bef5b1f0/libcxx/include/__compare/is_eq.h#L23-L28>
/// Implementations must be consistent with the [`PartialOrd`] implementation, and ensure `max`,
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// From the above and the requirements of `PartialOrd`, it follows that for all `a`, `b` and `c`:
/// - `<` is transitive: `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and
/// Mathematically speaking, the `<` operator defines a strict [weak order]. In cases where `==`
/// When `derive`d on enums, variants are ordered primarily by their discriminants. Secondarily,
/// they are ordered by their fields. By default, the discriminant is smallest for variants at the
/// - If one sequence is a prefix of another, the shorter sequence is lexicographically less than
/// - If two sequences have equivalent elements and are of the same length, then the sequences are
/// Because `Ord` implies a stronger ordering relationship than [`PartialOrd`], and both `Ord` and
/// [`PartialOrd`] must agree, you must choose how to implement `Ord` **first**. You can choose to
/// derive it, or implement it manually. If you derive it, you should derive all four traits. If you
/// // Mistake: floating-point values do not form a total order and using the built-in comparison
/// // Antisymmetry requirement of `Ord` is not given. Only one of a < c and c < a is allowed to be
/// // For performance reasons implementing `PartialEq` this way is not the idiomatic way, but it
/// // Transitivity requirement of `Ord` is not given. If a is smaller than b and b is smaller than
/// // Antisymmetry requirement of `Ord` is not given. Only one of a < c and c < a is allowed to be
/// The `lt`, `le`, `gt`, and `ge` methods of this trait can be called using the `<`, `<=`, `>`, and
/// implementing `PartialOrd` but not [`Ord`]**. Otherwise the comparison logic should be in [`Ord`]
/// The methods of this trait must be consistent with each other and with those of [`PartialEq`].
/// Conditions 2–5 above are ensured by the default implementation. Condition 6 is already ensured
/// `partial_cmp` (see the documentation of that trait for the exact requirements). It's easy to
/// accidentally make them disagree by deriving some of the traits and manually implementing others.
/// The comparison relations must satisfy the following conditions (for all `a`, `b`, `c` of type
/// - **Transitivity**: if `A: PartialOrd<B>` and `B: PartialOrd<C>` and `A: PartialOrd<C>`, then `a
/// - **Duality**: if `A: PartialOrd<B>` and `B: PartialOrd<A>`, then `a < b` if and only if `b >
/// Note that the `B: PartialOrd<A>` (dual) and `A: PartialOrd<C>` (transitive) impls are not forced
/// Violating these requirements is a logic error. The behavior resulting from a logic error is not
/// undefined behavior. This means that `unsafe` code **must not** rely on the correctness of these
/// Upholding the requirements stated above can become tricky when one crate implements `PartialOrd`
/// for a type of another crate (i.e., to allow comparing one of its own types with a type from the
/// standard library). The recommendation is to never implement this trait for a foreign type. In
/// other words, such a crate should do `impl PartialOrd<ForeignType> for LocalType`, but it should
/// This avoids the problem of transitive chains that criss-cross crate boundaries: for all local
/// types `T`, you may assume that no other crate will add `impl`s that allow comparing `T < U`. In
/// other words, if other crates add `impl`s that allow building longer transitive chains `U1 < ...
/// < T < V1 < ...`, then all the types that appear to the right of `T` must be types that the crate
/// defining `T` already knows about. This rules out transitive chains where downstream crates can
/// Not having such foreign `impl`s also avoids forward compatibility issues where one crate adding
/// - duality of `partial_cmp`: `partial_cmp(a, b) == partial_cmp(b, a).map(Ordering::reverse)`
/// The `<` and `>` operators behave according to a *strict* partial order. However, `<=` and `>=`
/// do **not** behave according to a *non-strict* partial order. That is because mathematically, a
/// non-strict partial order would require reflexivity, i.e. `a <= a` would need to be true for
/// When `derive`d on enums, variants are primarily ordered by their discriminants. Secondarily,
/// they are ordered by their fields. By default, the discriminant is smallest for variants at the
/// 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 == false`
/// You may also find it useful to use [`partial_cmp`] on your type's fields. Here is an example of
/// `Person` types who have a floating-point `height` field that is the only field to be used for
/// assert_eq!(a.partial_cmp(&b).unwrap(), Ordering::Equal); // a == b according to `PartialOrd`.