Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make some Ordering methods const #76198

Merged
merged 2 commits into from
Sep 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 17 additions & 1 deletion library/core/tests/cmp.rs
Original file line number Diff line number Diff line change
@@ -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);
}