diff options
| author | Deadbeef <ent3rm4n@gmail.com> | 2021-12-29 14:07:54 +0800 |
|---|---|---|
| committer | Deadbeef <ent3rm4n@gmail.com> | 2022-07-24 12:01:22 +0000 |
| commit | 9fc5463c18af25b880b1ca476ae05bcba87140e6 (patch) | |
| tree | 3a2af256cd3b36116cf0b3b453de82eb69bdc100 | |
| parent | 35a061724802377a21fc6dac1ebcbb9b8d1f558a (diff) | |
| download | rust-9fc5463c18af25b880b1ca476ae05bcba87140e6.tar.gz rust-9fc5463c18af25b880b1ca476ae05bcba87140e6.zip | |
Constify a few const `(Partial)Ord` impls
| -rw-r--r-- | library/core/src/cmp.rs | 68 | ||||
| -rw-r--r-- | library/core/src/lib.rs | 1 |
2 files changed, 50 insertions, 19 deletions
diff --git a/library/core/src/cmp.rs b/library/core/src/cmp.rs index 1d3466696ed..f3034159f04 100644 --- a/library/core/src/cmp.rs +++ b/library/core/src/cmp.rs @@ -22,6 +22,8 @@ #![stable(feature = "rust1", since = "1.0.0")] +use crate::marker::Destruct; + use self::Ordering::*; /// Trait for equality comparisons which are [partial equivalence @@ -603,7 +605,8 @@ impl Ordering { pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T); #[stable(feature = "reverse_cmp_key", since = "1.19.0")] -impl<T: PartialOrd> PartialOrd for Reverse<T> { +#[rustc_const_unstable(feature = "const_cmp", issue = "none")] +impl<T: ~const PartialOrd> const PartialOrd for Reverse<T> { #[inline] fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> { other.0.partial_cmp(&self.0) @@ -761,6 +764,7 @@ impl<T: Clone> Clone for Reverse<T> { #[doc(alias = ">=")] #[stable(feature = "rust1", since = "1.0.0")] #[rustc_diagnostic_item = "Ord"] +#[const_trait] pub trait Ord: Eq + PartialOrd<Self> { /// This method returns an [`Ordering`] between `self` and `other`. /// @@ -796,8 +800,12 @@ pub trait Ord: Eq + PartialOrd<Self> { fn max(self, other: Self) -> Self where Self: Sized, + Self: ~const Destruct, { - max_by(self, other, Ord::cmp) + match self.cmp(&other) { + Ordering::Less | Ordering::Equal => other, + Ordering::Greater => self, + } } /// Compares and returns the minimum of two values. @@ -816,8 +824,12 @@ pub trait Ord: Eq + PartialOrd<Self> { fn min(self, other: Self) -> Self where Self: Sized, + Self: ~const Destruct, { - min_by(self, other, Ord::cmp) + match self.cmp(&other) { + Ordering::Less | Ordering::Equal => self, + Ordering::Greater => other, + } } /// Restrict a value to a certain interval. @@ -841,6 +853,8 @@ pub trait Ord: Eq + PartialOrd<Self> { fn clamp(self, min: Self, max: Self) -> Self where Self: Sized, + Self: ~const Destruct, + Self: ~const PartialOrd, { assert!(min <= max); if self < min { @@ -862,7 +876,8 @@ pub macro Ord($item:item) { } #[stable(feature = "rust1", since = "1.0.0")] -impl Ord for Ordering { +#[rustc_const_unstable(feature = "const_cmp", issue = "none")] +impl const Ord for Ordering { #[inline] fn cmp(&self, other: &Ordering) -> Ordering { (*self as i32).cmp(&(*other as i32)) @@ -870,7 +885,8 @@ impl Ord for Ordering { } #[stable(feature = "rust1", since = "1.0.0")] -impl PartialOrd for Ordering { +#[rustc_const_unstable(feature = "const_cmp", issue = "none")] +impl const PartialOrd for Ordering { #[inline] fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> { (*self as i32).partial_cmp(&(*other as i32)) @@ -1187,8 +1203,9 @@ pub macro PartialOrd($item:item) { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_cmp", issue = "none")] #[cfg_attr(not(test), rustc_diagnostic_item = "cmp_min")] -pub fn min<T: Ord>(v1: T, v2: T) -> T { +pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T { v1.min(v2) } @@ -1250,8 +1267,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T { #[inline] #[must_use] #[stable(feature = "rust1", since = "1.0.0")] +#[rustc_const_unstable(feature = "const_cmp", issue = "none")] #[cfg_attr(not(test), rustc_diagnostic_item = "cmp_max")] -pub fn max<T: Ord>(v1: T, v2: T) -> T { +pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T { v1.max(v2) } @@ -1304,7 +1322,8 @@ mod impls { macro_rules! partial_eq_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl PartialEq for $t { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialEq for $t { #[inline] fn eq(&self, other: &$t) -> bool { (*self) == (*other) } #[inline] @@ -1314,7 +1333,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl PartialEq for () { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialEq for () { #[inline] fn eq(&self, _other: &()) -> bool { true @@ -1341,7 +1361,8 @@ mod impls { macro_rules! partial_ord_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd for $t { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialOrd for $t { #[inline] fn partial_cmp(&self, other: &$t) -> Option<Ordering> { match (*self <= *other, *self >= *other) { @@ -1364,7 +1385,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd for () { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialOrd for () { #[inline] fn partial_cmp(&self, _: &()) -> Option<Ordering> { Some(Equal) @@ -1372,7 +1394,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd for bool { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialOrd for bool { #[inline] fn partial_cmp(&self, other: &bool) -> Option<Ordering> { Some(self.cmp(other)) @@ -1384,7 +1407,8 @@ mod impls { macro_rules! ord_impl { ($($t:ty)*) => ($( #[stable(feature = "rust1", since = "1.0.0")] - impl PartialOrd for $t { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialOrd for $t { #[inline] fn partial_cmp(&self, other: &$t) -> Option<Ordering> { Some(self.cmp(other)) @@ -1400,7 +1424,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl Ord for $t { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const Ord for $t { #[inline] fn cmp(&self, other: &$t) -> Ordering { // The order here is important to generate more optimal assembly. @@ -1414,7 +1439,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl Ord for () { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const Ord for () { #[inline] fn cmp(&self, _other: &()) -> Ordering { Equal @@ -1422,7 +1448,8 @@ mod impls { } #[stable(feature = "rust1", since = "1.0.0")] - impl Ord for bool { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const Ord for bool { #[inline] fn cmp(&self, other: &bool) -> Ordering { // Casting to i8's and converting the difference to an Ordering generates @@ -1441,7 +1468,8 @@ mod impls { ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 } #[unstable(feature = "never_type", issue = "35121")] - impl PartialEq for ! { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialEq for ! { fn eq(&self, _: &!) -> bool { *self } @@ -1451,14 +1479,16 @@ mod impls { impl Eq for ! {} #[unstable(feature = "never_type", issue = "35121")] - impl PartialOrd for ! { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const PartialOrd for ! { fn partial_cmp(&self, _: &!) -> Option<Ordering> { *self } } #[unstable(feature = "never_type", issue = "35121")] - impl Ord for ! { + #[rustc_const_unstable(feature = "const_cmp", issue = "none")] + impl const Ord for ! { fn cmp(&self, _: &!) -> Ordering { *self } diff --git a/library/core/src/lib.rs b/library/core/src/lib.rs index f24a7ab61ae..30f2f0ee05c 100644 --- a/library/core/src/lib.rs +++ b/library/core/src/lib.rs @@ -105,6 +105,7 @@ #![feature(const_cell_into_inner)] #![feature(const_char_convert)] #![feature(const_clone)] +#![feature(const_cmp)] #![feature(const_discriminant)] #![feature(const_eval_select)] #![feature(const_float_bits_conv)] |
