Skip to content

Commit 69ac076

Browse files
authored
Rollup merge of #76198 - CDirkx:const-ordering, r=dtolnay
Make some Ordering methods const Resubmission of [PR#75463](#75463) as per [PR#76172](#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 #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: #76113
2 parents db22898 + 79d563c commit 69ac076

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

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,

library/core/tests/cmp.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use core::cmp::{self, Ordering::*};
1+
use core::cmp::{
2+
self,
3+
Ordering::{self, *},
4+
};
25

36
#[test]
47
fn test_int_totalord() {
@@ -116,3 +119,16 @@ fn test_user_defined_eq() {
116119
assert!(SketchyNum { num: 37 } == SketchyNum { num: 34 });
117120
assert!(SketchyNum { num: 25 } != SketchyNum { num: 57 });
118121
}
122+
123+
#[test]
124+
fn ordering_const() {
125+
// test that the methods of `Ordering` are usable in a const context
126+
127+
const ORDERING: Ordering = Greater;
128+
129+
const REVERSE: Ordering = ORDERING.reverse();
130+
assert_eq!(REVERSE, Less);
131+
132+
const THEN: Ordering = Equal.then(ORDERING);
133+
assert_eq!(THEN, Greater);
134+
}

0 commit comments

Comments
 (0)