Skip to content

Commit

Permalink
Rollup merge of #95017 - zachs18:cmp_ordering_derive_eq, r=Dylan-DPC
Browse files Browse the repository at this point in the history
Derive Eq for std::cmp::Ordering, instead of using manual impl.

This allows consts of type Ordering to be used in patterns, and with feature(adt_const_params) allows using `Ordering` as a const generic parameter.

Currently, `std::cmp::Ordering` implements `Eq` using a manually written `impl Eq for Ordering {}`, instead of `derive(Eq)`. This means that it does not implement `StructuralEq`.

This commit removes the manually written impl, and adds `derive(Eq)` to `Ordering`, so that it will implement `StructuralEq`.
  • Loading branch information
matthiaskrgr authored Mar 18, 2022
2 parents c271920 + b13b495 commit 4ead6d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
5 changes: 1 addition & 4 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
/// let result = 2.cmp(&1);
/// assert_eq!(Ordering::Greater, result);
/// ```
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
#[stable(feature = "rust1", since = "1.0.0")]
#[repr(i8)]
pub enum Ordering {
Expand Down Expand Up @@ -861,9 +861,6 @@ pub macro Ord($item:item) {
/* compiler built-in */
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for Ordering {}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for Ordering {
#[inline]
Expand Down
13 changes: 13 additions & 0 deletions library/core/tests/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,19 @@ fn ordering_const() {
assert_eq!(THEN, Greater);
}

#[test]
fn ordering_structural_eq() {
// test that consts of type `Ordering` are usable in patterns

const ORDERING: Ordering = Greater;

const REVERSE: Ordering = ORDERING.reverse();
match Ordering::Less {
REVERSE => {}
_ => unreachable!(),
};
}

#[test]
fn cmp_default() {
// Test default methods in PartialOrd and PartialEq
Expand Down

0 comments on commit 4ead6d9

Please sign in to comment.