Constify a few const (Partial)Ord impls
This commit is contained in:
@@ -22,6 +22,8 @@
|
|||||||
|
|
||||||
#![stable(feature = "rust1", since = "1.0.0")]
|
#![stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
|
||||||
|
use crate::marker::Destruct;
|
||||||
|
|
||||||
use self::Ordering::*;
|
use self::Ordering::*;
|
||||||
|
|
||||||
/// Trait for equality comparisons which are [partial equivalence
|
/// 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);
|
pub struct Reverse<T>(#[stable(feature = "reverse_cmp_key", since = "1.19.0")] pub T);
|
||||||
|
|
||||||
#[stable(feature = "reverse_cmp_key", since = "1.19.0")]
|
#[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]
|
#[inline]
|
||||||
fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Reverse<T>) -> Option<Ordering> {
|
||||||
other.0.partial_cmp(&self.0)
|
other.0.partial_cmp(&self.0)
|
||||||
@@ -761,6 +764,7 @@ impl<T: Clone> Clone for Reverse<T> {
|
|||||||
#[doc(alias = ">=")]
|
#[doc(alias = ">=")]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[rustc_diagnostic_item = "Ord"]
|
#[rustc_diagnostic_item = "Ord"]
|
||||||
|
#[const_trait]
|
||||||
pub trait Ord: Eq + PartialOrd<Self> {
|
pub trait Ord: Eq + PartialOrd<Self> {
|
||||||
/// This method returns an [`Ordering`] between `self` and `other`.
|
/// 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
|
fn max(self, other: Self) -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
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.
|
/// Compares and returns the minimum of two values.
|
||||||
@@ -816,8 +824,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
|
|||||||
fn min(self, other: Self) -> Self
|
fn min(self, other: Self) -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
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.
|
/// 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
|
fn clamp(self, min: Self, max: Self) -> Self
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
|
Self: ~const Destruct,
|
||||||
|
Self: ~const PartialOrd,
|
||||||
{
|
{
|
||||||
assert!(min <= max);
|
assert!(min <= max);
|
||||||
if self < min {
|
if self < min {
|
||||||
@@ -862,7 +876,8 @@ pub macro Ord($item:item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn cmp(&self, other: &Ordering) -> Ordering {
|
fn cmp(&self, other: &Ordering) -> Ordering {
|
||||||
(*self as i32).cmp(&(*other as i32))
|
(*self as i32).cmp(&(*other as i32))
|
||||||
@@ -870,7 +885,8 @@ impl Ord for Ordering {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Ordering) -> Option<Ordering> {
|
||||||
(*self as i32).partial_cmp(&(*other as i32))
|
(*self as i32).partial_cmp(&(*other as i32))
|
||||||
@@ -1187,8 +1203,9 @@ pub macro PartialOrd($item:item) {
|
|||||||
#[inline]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_cmp", issue = "none")]
|
||||||
#[cfg_attr(not(test), rustc_diagnostic_item = "cmp_min")]
|
#[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)
|
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]
|
#[inline]
|
||||||
#[must_use]
|
#[must_use]
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
|
#[rustc_const_unstable(feature = "const_cmp", issue = "none")]
|
||||||
#[cfg_attr(not(test), rustc_diagnostic_item = "cmp_max")]
|
#[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)
|
v1.max(v2)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1304,7 +1322,8 @@ mod impls {
|
|||||||
macro_rules! partial_eq_impl {
|
macro_rules! partial_eq_impl {
|
||||||
($($t:ty)*) => ($(
|
($($t:ty)*) => ($(
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
|
fn eq(&self, other: &$t) -> bool { (*self) == (*other) }
|
||||||
#[inline]
|
#[inline]
|
||||||
@@ -1314,7 +1333,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl PartialEq for () {
|
#[rustc_const_unstable(feature = "const_cmp", issue = "none")]
|
||||||
|
impl const PartialEq for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn eq(&self, _other: &()) -> bool {
|
fn eq(&self, _other: &()) -> bool {
|
||||||
true
|
true
|
||||||
@@ -1341,7 +1361,8 @@ mod impls {
|
|||||||
macro_rules! partial_ord_impl {
|
macro_rules! partial_ord_impl {
|
||||||
($($t:ty)*) => ($(
|
($($t:ty)*) => ($(
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
|
||||||
match (*self <= *other, *self >= *other) {
|
match (*self <= *other, *self >= *other) {
|
||||||
@@ -1364,7 +1385,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl PartialOrd for () {
|
#[rustc_const_unstable(feature = "const_cmp", issue = "none")]
|
||||||
|
impl const PartialOrd for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn partial_cmp(&self, _: &()) -> Option<Ordering> {
|
fn partial_cmp(&self, _: &()) -> Option<Ordering> {
|
||||||
Some(Equal)
|
Some(Equal)
|
||||||
@@ -1372,7 +1394,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &bool) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
@@ -1384,7 +1407,8 @@ mod impls {
|
|||||||
macro_rules! ord_impl {
|
macro_rules! ord_impl {
|
||||||
($($t:ty)*) => ($(
|
($($t:ty)*) => ($(
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
|
||||||
Some(self.cmp(other))
|
Some(self.cmp(other))
|
||||||
@@ -1400,7 +1424,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn cmp(&self, other: &$t) -> Ordering {
|
fn cmp(&self, other: &$t) -> Ordering {
|
||||||
// The order here is important to generate more optimal assembly.
|
// The order here is important to generate more optimal assembly.
|
||||||
@@ -1414,7 +1439,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
impl Ord for () {
|
#[rustc_const_unstable(feature = "const_cmp", issue = "none")]
|
||||||
|
impl const Ord for () {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn cmp(&self, _other: &()) -> Ordering {
|
fn cmp(&self, _other: &()) -> Ordering {
|
||||||
Equal
|
Equal
|
||||||
@@ -1422,7 +1448,8 @@ mod impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[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]
|
#[inline]
|
||||||
fn cmp(&self, other: &bool) -> Ordering {
|
fn cmp(&self, other: &bool) -> Ordering {
|
||||||
// Casting to i8's and converting the difference to an Ordering generates
|
// 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 }
|
ord_impl! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
|
||||||
|
|
||||||
#[unstable(feature = "never_type", issue = "35121")]
|
#[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 {
|
fn eq(&self, _: &!) -> bool {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
@@ -1451,14 +1479,16 @@ mod impls {
|
|||||||
impl Eq for ! {}
|
impl Eq for ! {}
|
||||||
|
|
||||||
#[unstable(feature = "never_type", issue = "35121")]
|
#[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> {
|
fn partial_cmp(&self, _: &!) -> Option<Ordering> {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[unstable(feature = "never_type", issue = "35121")]
|
#[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 {
|
fn cmp(&self, _: &!) -> Ordering {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,7 @@
|
|||||||
#![feature(const_cell_into_inner)]
|
#![feature(const_cell_into_inner)]
|
||||||
#![feature(const_char_convert)]
|
#![feature(const_char_convert)]
|
||||||
#![feature(const_clone)]
|
#![feature(const_clone)]
|
||||||
|
#![feature(const_cmp)]
|
||||||
#![feature(const_discriminant)]
|
#![feature(const_discriminant)]
|
||||||
#![feature(const_eval_select)]
|
#![feature(const_eval_select)]
|
||||||
#![feature(const_float_bits_conv)]
|
#![feature(const_float_bits_conv)]
|
||||||
|
|||||||
Reference in New Issue
Block a user