diff options
| author | CDirkx <christiaan@dirkx.com> | 2020-09-01 15:44:00 +0200 |
|---|---|---|
| committer | CDirkx <christiaan@dirkx.com> | 2020-09-01 15:44:00 +0200 |
| commit | ea5dc0909ea1ed4135f1bdecbaa3423051be578d (patch) | |
| tree | 059783a04d988497d84311ef72eaa213b033085e | |
| parent | e88e908e66cd1e6e30d789b37bcd774951d01856 (diff) | |
| download | rust-ea5dc0909ea1ed4135f1bdecbaa3423051be578d.tar.gz rust-ea5dc0909ea1ed4135f1bdecbaa3423051be578d.zip | |
Make some Ordering methods const
Constify the following methods of `core::cmp::Ordering`: - `reverse` - `then` Stabilizes these methods as const under the `const_ordering` feature. Also adds a test for these methods in a const context. Possible because of #49146 (Allow `if` and `match` in constants).
| -rw-r--r-- | library/core/src/cmp.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/consts/const-ordering.rs | 15 |
2 files changed, 19 insertions, 2 deletions
diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 3953c73319f..dde442aa7b5 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -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, @@ -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, diff --git a/src/test/ui/consts/const-ordering.rs b/src/test/ui/consts/const-ordering.rs new file mode 100644 index 00000000000..454f2da00df --- /dev/null +++ b/src/test/ui/consts/const-ordering.rs @@ -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); +} |
