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 = "none" ) ]
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,12 @@ 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+ match self . cmp ( & other) {
806+ Ordering :: Less | Ordering :: Equal => other,
807+ Ordering :: Greater => self ,
808+ }
801809 }
802810
803811 /// Compares and returns the minimum of two values.
@@ -816,8 +824,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
816824 fn min ( self , other : Self ) -> Self
817825 where
818826 Self : Sized ,
827+ Self : ~const Destruct ,
819828 {
820- min_by ( self , other, Ord :: cmp)
829+ match self . cmp ( & other) {
830+ Ordering :: Less | Ordering :: Equal => self ,
831+ Ordering :: Greater => other,
832+ }
821833 }
822834
823835 /// Restrict a value to a certain interval.
@@ -841,6 +853,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
841853 fn clamp ( self , min : Self , max : Self ) -> Self
842854 where
843855 Self : Sized ,
856+ Self : ~const Destruct ,
857+ Self : ~const PartialOrd ,
844858 {
845859 assert ! ( min <= max) ;
846860 if self < min {
@@ -862,15 +876,17 @@ pub macro Ord($item:item) {
862876}
863877
864878#[ stable( feature = "rust1" , since = "1.0.0" ) ]
865- impl Ord for Ordering {
879+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
880+ impl const Ord for Ordering {
866881 #[ inline]
867882 fn cmp ( & self , other : & Ordering ) -> Ordering {
868883 ( * self as i32 ) . cmp ( & ( * other as i32 ) )
869884 }
870885}
871886
872887#[ stable( feature = "rust1" , since = "1.0.0" ) ]
873- impl PartialOrd for Ordering {
888+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
889+ impl const PartialOrd for Ordering {
874890 #[ inline]
875891 fn partial_cmp ( & self , other : & Ordering ) -> Option < Ordering > {
876892 ( * self as i32 ) . partial_cmp ( & ( * other as i32 ) )
@@ -1187,8 +1203,9 @@ pub macro PartialOrd($item:item) {
11871203#[ inline]
11881204#[ must_use]
11891205#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1206+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
11901207#[ cfg_attr( not( test) , rustc_diagnostic_item = "cmp_min" ) ]
1191- pub fn min < T : Ord > ( v1 : T , v2 : T ) -> T {
1208+ pub const fn min < T : ~ const Ord + ~ const Destruct > ( v1 : T , v2 : T ) -> T {
11921209 v1. min ( v2)
11931210}
11941211
@@ -1250,8 +1267,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
12501267#[ inline]
12511268#[ must_use]
12521269#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1270+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
12531271#[ cfg_attr( not( test) , rustc_diagnostic_item = "cmp_max" ) ]
1254- pub fn max < T : Ord > ( v1 : T , v2 : T ) -> T {
1272+ pub const fn max < T : ~ const Ord + ~ const Destruct > ( v1 : T , v2 : T ) -> T {
12551273 v1. max ( v2)
12561274}
12571275
@@ -1304,7 +1322,8 @@ mod impls {
13041322 macro_rules! partial_eq_impl {
13051323 ( $( $t: ty) * ) => ( $(
13061324 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1307- impl PartialEq for $t {
1325+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1326+ impl const PartialEq for $t {
13081327 #[ inline]
13091328 fn eq( & self , other: & $t) -> bool { ( * self ) == ( * other) }
13101329 #[ inline]
@@ -1314,7 +1333,8 @@ mod impls {
13141333 }
13151334
13161335 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1317- impl PartialEq for ( ) {
1336+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1337+ impl const PartialEq for ( ) {
13181338 #[ inline]
13191339 fn eq ( & self , _other : & ( ) ) -> bool {
13201340 true
@@ -1341,7 +1361,8 @@ mod impls {
13411361 macro_rules! partial_ord_impl {
13421362 ( $( $t: ty) * ) => ( $(
13431363 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1344- impl PartialOrd for $t {
1364+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1365+ impl const PartialOrd for $t {
13451366 #[ inline]
13461367 fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
13471368 match ( * self <= * other, * self >= * other) {
@@ -1364,15 +1385,17 @@ mod impls {
13641385 }
13651386
13661387 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1367- impl PartialOrd for ( ) {
1388+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1389+ impl const PartialOrd for ( ) {
13681390 #[ inline]
13691391 fn partial_cmp ( & self , _: & ( ) ) -> Option < Ordering > {
13701392 Some ( Equal )
13711393 }
13721394 }
13731395
13741396 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1375- impl PartialOrd for bool {
1397+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1398+ impl const PartialOrd for bool {
13761399 #[ inline]
13771400 fn partial_cmp ( & self , other : & bool ) -> Option < Ordering > {
13781401 Some ( self . cmp ( other) )
@@ -1384,7 +1407,8 @@ mod impls {
13841407 macro_rules! ord_impl {
13851408 ( $( $t: ty) * ) => ( $(
13861409 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1387- impl PartialOrd for $t {
1410+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1411+ impl const PartialOrd for $t {
13881412 #[ inline]
13891413 fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
13901414 Some ( self . cmp( other) )
@@ -1400,7 +1424,8 @@ mod impls {
14001424 }
14011425
14021426 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1403- impl Ord for $t {
1427+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1428+ impl const Ord for $t {
14041429 #[ inline]
14051430 fn cmp( & self , other: & $t) -> Ordering {
14061431 // The order here is important to generate more optimal assembly.
@@ -1414,15 +1439,17 @@ mod impls {
14141439 }
14151440
14161441 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1417- impl Ord for ( ) {
1442+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1443+ impl const Ord for ( ) {
14181444 #[ inline]
14191445 fn cmp ( & self , _other : & ( ) ) -> Ordering {
14201446 Equal
14211447 }
14221448 }
14231449
14241450 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
1425- impl Ord for bool {
1451+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1452+ impl const Ord for bool {
14261453 #[ inline]
14271454 fn cmp ( & self , other : & bool ) -> Ordering {
14281455 // Casting to i8's and converting the difference to an Ordering generates
@@ -1441,7 +1468,8 @@ mod impls {
14411468 ord_impl ! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
14421469
14431470 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1444- impl PartialEq for ! {
1471+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1472+ impl const PartialEq for ! {
14451473 fn eq ( & self , _: & !) -> bool {
14461474 * self
14471475 }
@@ -1451,14 +1479,16 @@ mod impls {
14511479 impl Eq for ! { }
14521480
14531481 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1454- impl PartialOrd for ! {
1482+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1483+ impl const PartialOrd for ! {
14551484 fn partial_cmp ( & self , _: & !) -> Option < Ordering > {
14561485 * self
14571486 }
14581487 }
14591488
14601489 #[ unstable( feature = "never_type" , issue = "35121" ) ]
1461- impl Ord for ! {
1490+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1491+ impl const Ord for ! {
14621492 fn cmp ( & self , _: & !) -> Ordering {
14631493 * self
14641494 }
0 commit comments