Skip to content

Commit

Permalink
Rollup merge of rust-lang#76198 - CDirkx:const-ordering, r=dtolnay
Browse files Browse the repository at this point in the history
Make some Ordering methods const

Resubmission of [PR#75463](rust-lang#75463) as per [PR#76172](rust-lang#76172).

Constify the following methods of `core::cmp::Ordering`:
 - `reverse`
 - `then`

Insta-stabilizes these methods as const under the `const_ordering` feature, as their implementation is a trivial match and the recent stabilization of rust-lang#49146 (Allow `if` and `match` in constants).
Note: the `const_ordering` feature has never actually been used as these methods have not been `#[rustc_const_unstable]`.

Tracking issue:  rust-lang#76113
  • Loading branch information
Dylan-DPC committed Sep 15, 2020
2 parents db22898 + 79d563c commit 69ac076
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions library/core/src/cmp.rs
Expand Up @@ -356,8 +356,9 @@ impl Ordering {
/// ```
#[inline]
#[must_use]
#[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn reverse(self) -> Ordering {
pub const fn reverse(self) -> Ordering {
match self {
Less => Greater,
Equal => Equal,
Expand Down Expand Up @@ -394,8 +395,9 @@ impl Ordering {
/// ```
#[inline]
#[must_use]
#[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
#[stable(feature = "ordering_chaining", since = "1.17.0")]
pub fn then(self, other: Ordering) -> Ordering {
pub const fn then(self, other: Ordering) -> Ordering {
match self {
Equal => other,
_ => self,
Expand Down
18 changes: 17 additions & 1 deletion library/core/tests/cmp.rs
@@ -1,4 +1,7 @@
use core::cmp::{self, Ordering::*};
use core::cmp::{
self,
Ordering::{self, *},
};

#[test]
fn test_int_totalord() {
Expand Down Expand Up @@ -116,3 +119,16 @@ fn test_user_defined_eq() {
assert!(SketchyNum { num: 37 } == SketchyNum { num: 34 });
assert!(SketchyNum { num: 25 } != SketchyNum { num: 57 });
}

#[test]
fn ordering_const() {
// test that the methods of `Ordering` are usable in a const context

const ORDERING: Ordering = Greater;

const REVERSE: Ordering = ORDERING.reverse();
assert_eq!(REVERSE, Less);

const THEN: Ordering = Equal.then(ORDERING);
assert_eq!(THEN, Greater);
}

0 comments on commit 69ac076

Please sign in to comment.