Skip to content

Commit

Permalink
Auto merge of #75463 - CDirkx:ordering-const, r=CDirkx
Browse files Browse the repository at this point in the history
Make some Ordering methods const

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

Possible because of #49146 (Allow `if` and `match` in constants).

Tracking issue:  #76113
  • Loading branch information
bors committed Aug 31, 2020
2 parents 022e1fe + 12f4624 commit 92290d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
6 changes: 4 additions & 2 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
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
15 changes: 15 additions & 0 deletions src/test/ui/consts/const-ordering.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass

use std::cmp::Ordering;

// the following methods of core::cmp::Ordering are const:
// - reverse
// - then

fn main() {
const REVERSE : Ordering = Ordering::Greater.reverse();
assert_eq!(REVERSE, Ordering::Less);

const THEN : Ordering = Ordering::Equal.then(REVERSE);
assert_eq!(THEN, Ordering::Less);
}

0 comments on commit 92290d1

Please sign in to comment.