Skip to content

Commit 0ffaf19

Browse files
authored
Rollup merge of rust-lang#95017 - zachs18:cmp_ordering_derive_eq, r=Dylan-DPC
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`.
2 parents fc234d3 + b13b495 commit 0ffaf19

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

library/core/src/cmp.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub struct AssertParamIsEq<T: Eq + ?Sized> {
333333
/// let result = 2.cmp(&1);
334334
/// assert_eq!(Ordering::Greater, result);
335335
/// ```
336-
#[derive(Clone, Copy, PartialEq, Debug, Hash)]
336+
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
337337
#[stable(feature = "rust1", since = "1.0.0")]
338338
#[repr(i8)]
339339
pub enum Ordering {
@@ -861,9 +861,6 @@ pub macro Ord($item:item) {
861861
/* compiler built-in */
862862
}
863863

864-
#[stable(feature = "rust1", since = "1.0.0")]
865-
impl Eq for Ordering {}
866-
867864
#[stable(feature = "rust1", since = "1.0.0")]
868865
impl Ord for Ordering {
869866
#[inline]

library/core/tests/cmp.rs

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,19 @@ fn ordering_const() {
133133
assert_eq!(THEN, Greater);
134134
}
135135

136+
#[test]
137+
fn ordering_structural_eq() {
138+
// test that consts of type `Ordering` are usable in patterns
139+
140+
const ORDERING: Ordering = Greater;
141+
142+
const REVERSE: Ordering = ORDERING.reverse();
143+
match Ordering::Less {
144+
REVERSE => {}
145+
_ => unreachable!(),
146+
};
147+
}
148+
136149
#[test]
137150
fn cmp_default() {
138151
// Test default methods in PartialOrd and PartialEq

0 commit comments

Comments
 (0)