Skip to content

Commit 92290d1

Browse files
committed
Auto merge of #75463 - CDirkx:ordering-const, r=CDirkx
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
2 parents 022e1fe + 12f4624 commit 92290d1

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Diff for: library/core/src/cmp.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,9 @@ impl Ordering {
356356
/// ```
357357
#[inline]
358358
#[must_use]
359+
#[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
359360
#[stable(feature = "rust1", since = "1.0.0")]
360-
pub fn reverse(self) -> Ordering {
361+
pub const fn reverse(self) -> Ordering {
361362
match self {
362363
Less => Greater,
363364
Equal => Equal,
@@ -394,8 +395,9 @@ impl Ordering {
394395
/// ```
395396
#[inline]
396397
#[must_use]
398+
#[rustc_const_stable(feature = "const_ordering", since = "1.48.0")]
397399
#[stable(feature = "ordering_chaining", since = "1.17.0")]
398-
pub fn then(self, other: Ordering) -> Ordering {
400+
pub const fn then(self, other: Ordering) -> Ordering {
399401
match self {
400402
Equal => other,
401403
_ => self,

Diff for: src/test/ui/consts/const-ordering.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
3+
use std::cmp::Ordering;
4+
5+
// the following methods of core::cmp::Ordering are const:
6+
// - reverse
7+
// - then
8+
9+
fn main() {
10+
const REVERSE : Ordering = Ordering::Greater.reverse();
11+
assert_eq!(REVERSE, Ordering::Less);
12+
13+
const THEN : Ordering = Ordering::Equal.then(REVERSE);
14+
assert_eq!(THEN, Ordering::Less);
15+
}

0 commit comments

Comments
 (0)