2222
2323#![ stable( feature = "rust1" , since = "1.0.0" ) ]
2424
25+ use crate :: marker:: Destruct ;
26+
2527use self :: Ordering :: * ;
2628
2729/// Trait for equality comparisons which are [partial equivalence
@@ -603,7 +605,8 @@ impl Ordering {
603605pub struct Reverse < T > ( #[ stable( feature = "reverse_cmp_key" , since = "1.19.0" ) ] pub T ) ;
604606
605607#[ stable( feature = "reverse_cmp_key" , since = "1.19.0" ) ]
606- impl < T : PartialOrd > PartialOrd for Reverse < T > {
608+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
609+ impl < T : ~const PartialOrd > const PartialOrd for Reverse < T > {
607610 #[ inline]
608611 fn partial_cmp ( & self , other : & Reverse < T > ) -> Option < Ordering > {
609612 other. 0 . partial_cmp ( & self . 0 )
@@ -761,6 +764,7 @@ impl<T: Clone> Clone for Reverse<T> {
761764#[ doc( alias = ">=" ) ]
762765#[ stable( feature = "rust1" , since = "1.0.0" ) ]
763766#[ rustc_diagnostic_item = "Ord" ]
767+ #[ const_trait]
764768pub trait Ord : Eq + PartialOrd < Self > {
765769 /// This method returns an [`Ordering`] between `self` and `other`.
766770 ///
@@ -796,8 +800,15 @@ pub trait Ord: Eq + PartialOrd<Self> {
796800 fn max ( self , other : Self ) -> Self
797801 where
798802 Self : Sized ,
803+ Self : ~const Destruct ,
799804 {
800- max_by ( self , other, Ord :: cmp)
805+ // HACK(fee1-dead): go back to using `self.max_by(other, Ord::cmp)`
806+ // when trait methods are allowed to be used when a const closure is
807+ // expected.
808+ match self . cmp ( & other) {
809+ Ordering :: Less | Ordering :: Equal => other,
810+ Ordering :: Greater => self ,
811+ }
801812 }
802813
803814 /// Compares and returns the minimum of two values.
@@ -816,8 +827,15 @@ pub trait Ord: Eq + PartialOrd<Self> {
816827 fn min ( self , other : Self ) -> Self
817828 where
818829 Self : Sized ,
830+ Self : ~const Destruct ,
819831 {
820- min_by ( self , other, Ord :: cmp)
832+ // HACK(fee1-dead): go back to using `self.min_by(other, Ord::cmp)`
833+ // when trait methods are allowed to be used when a const closure is
834+ // expected.
835+ match self . cmp ( & other) {
836+ Ordering :: Less | Ordering :: Equal => self ,
837+ Ordering :: Greater => other,
838+ }
821839 }
822840
823841 /// Restrict a value to a certain interval.
@@ -841,6 +859,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
841859 fn clamp ( self , min : Self , max : Self ) -> Self
842860 where
843861 Self : Sized ,
862+ Self : ~const Destruct ,
863+ Self : ~const PartialOrd ,
844864 {
845865 assert ! ( min <= max) ;
846866 if self < min {
@@ -862,15 +882,17 @@ pub macro Ord($item:item) {
862882}
863883
864884#[ stable( feature = "rust1" , since = "1.0.0" ) ]
865- impl Ord for Ordering {
885+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
886+ impl const Ord for Ordering {
866887 #[ inline]
867888 fn cmp ( & self , other : & Ordering ) -> Ordering {
868889 ( * self as i32 ) . cmp ( & ( * other as i32 ) )
869890 }
870891}
871892
872893#[ stable( feature = "rust1" , since = "1.0.0" ) ]
873- impl PartialOrd for Ordering {
894+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
895+ impl const PartialOrd for Ordering {
874896 #[ inline]
875897 fn partial_cmp ( & self , other : & Ordering ) -> Option < Ordering > {
876898 ( * self as i32 ) . partial_cmp ( & ( * other as i32 ) )
@@ -1187,8 +1209,9 @@ pub macro PartialOrd($item:item) {
11871209#[ inline]
11881210#[ must_use]
11891211#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1212+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
11901213#[ cfg_attr( not( test) , rustc_diagnostic_item = "cmp_min" ) ]
1191- pub fn min < T : Ord > ( v1 : T , v2 : T ) -> T {
1214+ pub const fn min < T : ~ const Ord + ~ const Destruct > ( v1 : T , v2 : T ) -> T {
11921215 v1. min ( v2)
11931216}
11941217
@@ -1250,8 +1273,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
12501273#[ inline]
12511274#[ must_use]
12521275#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1276+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
12531277#[ cfg_attr( not( test) , rustc_diagnostic_item = "cmp_max" ) ]
1254- pub fn max < T : Ord > ( v1 : T , v2 : T ) -> T {
1278+ pub const fn max < T : ~ const Ord + ~ const Destruct > ( v1 : T , v2 : T ) -> T {
12551279 v1. max ( v2)
12561280}
12571281
@@ -1304,7 +1328,8 @@ mod impls {
13041328 macro_rules! partial_eq_impl {
13051329 ( $( $t: ty) * ) => ( $(
13061330 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1307- impl PartialEq for $t {
1331+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1332+ impl const PartialEq for $t {
13081333 #[ inline]
13091334 fn eq( & self , other: & $t) -> bool { ( * self ) == ( * other) }
13101335 #[ inline]
@@ -1314,7 +1339,8 @@ mod impls {
13141339 }
13151340
13161341 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1317- impl PartialEq for ( ) {
1342+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1343+ impl const PartialEq for ( ) {
13181344 #[ inline]
13191345 fn eq ( & self , _other : & ( ) ) -> bool {
13201346 true
@@ -1341,7 +1367,8 @@ mod impls {
13411367 macro_rules! partial_ord_impl {
13421368 ( $( $t: ty) * ) => ( $(
13431369 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1344- impl PartialOrd for $t {
1370+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1371+ impl const PartialOrd for $t {
13451372 #[ inline]
13461373 fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
13471374 match ( * self <= * other, * self >= * other) {
@@ -1364,15 +1391,17 @@ mod impls {
13641391 }
13651392
13661393 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1367- impl PartialOrd for ( ) {
1394+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1395+ impl const PartialOrd for ( ) {
13681396 #[ inline]
13691397 fn partial_cmp ( & self , _: & ( ) ) -> Option < Ordering > {
13701398 Some ( Equal )
13711399 }
13721400 }
13731401
13741402 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1375- impl PartialOrd for bool {
1403+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1404+ impl const PartialOrd for bool {
13761405 #[ inline]
13771406 fn partial_cmp ( & self , other : & bool ) -> Option < Ordering > {
13781407 Some ( self . cmp ( other) )
@@ -1384,7 +1413,8 @@ mod impls {
13841413 macro_rules! ord_impl {
13851414 ( $( $t: ty) * ) => ( $(
13861415 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1387- impl PartialOrd for $t {
1416+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1417+ impl const PartialOrd for $t {
13881418 #[ inline]
13891419 fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
13901420 Some ( self . cmp( other) )
@@ -1400,7 +1430,8 @@ mod impls {
14001430 }
14011431
14021432 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1403- impl Ord for $t {
1433+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1434+ impl const Ord for $t {
14041435 #[ inline]
14051436 fn cmp( & self , other: & $t) -> Ordering {
14061437 // The order here is important to generate more optimal assembly.
@@ -1414,15 +1445,17 @@ mod impls {
14141445 }
14151446
14161447 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1417- impl Ord for ( ) {
1448+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1449+ impl const Ord for ( ) {
14181450 #[ inline]
14191451 fn cmp ( & self , _other : & ( ) ) -> Ordering {
14201452 Equal
14211453 }
14221454 }
14231455
14241456 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1425- impl Ord for bool {
1457+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1458+ impl const Ord for bool {
14261459 #[ inline]
14271460 fn cmp ( & self , other : & bool ) -> Ordering {
14281461 // Casting to i8's and converting the difference to an Ordering generates
@@ -1441,7 +1474,8 @@ mod impls {
14411474 ord_impl ! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
14421475
14431476 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1444- impl PartialEq for ! {
1477+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1478+ impl const PartialEq for ! {
14451479 fn eq ( & self , _: & !) -> bool {
14461480 * self
14471481 }
@@ -1451,14 +1485,16 @@ mod impls {
14511485 impl Eq for ! { }
14521486
14531487 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1454- impl PartialOrd for ! {
1488+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1489+ impl const PartialOrd for ! {
14551490 fn partial_cmp ( & self , _: & !) -> Option < Ordering > {
14561491 * self
14571492 }
14581493 }
14591494
14601495 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1461- impl Ord for ! {
1496+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1497+ impl const Ord for ! {
14621498 fn cmp ( & self , _: & !) -> Ordering {
14631499 * self
14641500 }
0 commit comments