From ba611d55f39909f4c4da43fe96a275c4746c38d1 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Wed, 16 Mar 2022 11:36:31 -0500 Subject: [PATCH 1/2] 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 Orderings as const generic parameters. --- library/core/src/cmp.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index ddaeb9eca975c..74328a3607d64 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -333,7 +333,7 @@ pub struct AssertParamIsEq { /// 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 { @@ -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] From b13b495b918026fc5f3bf059bbde19c832ffe3c0 Mon Sep 17 00:00:00 2001 From: Zachary S Date: Wed, 16 Mar 2022 14:01:48 -0500 Subject: [PATCH 2/2] Add test for StructuralEq for std::cmp::Ordering. Added test in library/core/tests/cmp.rs that ensures that `const`s of type `Ordering`s can be used in patterns. --- library/core/tests/cmp.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/library/core/tests/cmp.rs b/library/core/tests/cmp.rs index 2b234de6795eb..8d0e59d5a4972 100644 --- a/library/core/tests/cmp.rs +++ b/library/core/tests/cmp.rs @@ -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