22
22
23
23
#![ stable( feature = "rust1" , since = "1.0.0" ) ]
24
24
25
+ use crate :: marker:: Destruct ;
26
+
25
27
use self :: Ordering :: * ;
26
28
27
29
/// Trait for equality comparisons which are [partial equivalence
@@ -603,7 +605,8 @@ impl Ordering {
603
605
pub struct Reverse < T > ( #[ stable( feature = "reverse_cmp_key" , since = "1.19.0" ) ] pub T ) ;
604
606
605
607
#[ 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 > {
607
610
#[ inline]
608
611
fn partial_cmp ( & self , other : & Reverse < T > ) -> Option < Ordering > {
609
612
other. 0 . partial_cmp ( & self . 0 )
@@ -761,6 +764,7 @@ impl<T: Clone> Clone for Reverse<T> {
761
764
#[ doc( alias = ">=" ) ]
762
765
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
763
766
#[ rustc_diagnostic_item = "Ord" ]
767
+ #[ const_trait]
764
768
pub trait Ord : Eq + PartialOrd < Self > {
765
769
/// This method returns an [`Ordering`] between `self` and `other`.
766
770
///
@@ -796,8 +800,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
796
800
fn max ( self , other : Self ) -> Self
797
801
where
798
802
Self : Sized ,
803
+ Self : ~const Destruct ,
799
804
{
800
- max_by ( self , other, Ord :: cmp)
805
+ match self . cmp ( & other) {
806
+ Ordering :: Less | Ordering :: Equal => other,
807
+ Ordering :: Greater => self ,
808
+ }
801
809
}
802
810
803
811
/// Compares and returns the minimum of two values.
@@ -816,8 +824,12 @@ pub trait Ord: Eq + PartialOrd<Self> {
816
824
fn min ( self , other : Self ) -> Self
817
825
where
818
826
Self : Sized ,
827
+ Self : ~const Destruct ,
819
828
{
820
- min_by ( self , other, Ord :: cmp)
829
+ match self . cmp ( & other) {
830
+ Ordering :: Less | Ordering :: Equal => self ,
831
+ Ordering :: Greater => other,
832
+ }
821
833
}
822
834
823
835
/// Restrict a value to a certain interval.
@@ -841,6 +853,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
841
853
fn clamp ( self , min : Self , max : Self ) -> Self
842
854
where
843
855
Self : Sized ,
856
+ Self : ~const Destruct ,
857
+ Self : ~const PartialOrd ,
844
858
{
845
859
assert ! ( min <= max) ;
846
860
if self < min {
@@ -862,15 +876,17 @@ pub macro Ord($item:item) {
862
876
}
863
877
864
878
#[ 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 {
866
881
#[ inline]
867
882
fn cmp ( & self , other : & Ordering ) -> Ordering {
868
883
( * self as i32 ) . cmp ( & ( * other as i32 ) )
869
884
}
870
885
}
871
886
872
887
#[ 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 {
874
890
#[ inline]
875
891
fn partial_cmp ( & self , other : & Ordering ) -> Option < Ordering > {
876
892
( * self as i32 ) . partial_cmp ( & ( * other as i32 ) )
@@ -1187,8 +1203,9 @@ pub macro PartialOrd($item:item) {
1187
1203
#[ inline]
1188
1204
#[ must_use]
1189
1205
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1206
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1190
1207
#[ 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 {
1192
1209
v1. min ( v2)
1193
1210
}
1194
1211
@@ -1250,8 +1267,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1250
1267
#[ inline]
1251
1268
#[ must_use]
1252
1269
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1270
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1253
1271
#[ 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 {
1255
1273
v1. max ( v2)
1256
1274
}
1257
1275
@@ -1304,7 +1322,8 @@ mod impls {
1304
1322
macro_rules! partial_eq_impl {
1305
1323
( $( $t: ty) * ) => ( $(
1306
1324
#[ 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 {
1308
1327
#[ inline]
1309
1328
fn eq( & self , other: & $t) -> bool { ( * self ) == ( * other) }
1310
1329
#[ inline]
@@ -1314,7 +1333,8 @@ mod impls {
1314
1333
}
1315
1334
1316
1335
#[ 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 ( ) {
1318
1338
#[ inline]
1319
1339
fn eq ( & self , _other : & ( ) ) -> bool {
1320
1340
true
@@ -1341,7 +1361,8 @@ mod impls {
1341
1361
macro_rules! partial_ord_impl {
1342
1362
( $( $t: ty) * ) => ( $(
1343
1363
#[ 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 {
1345
1366
#[ inline]
1346
1367
fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
1347
1368
match ( * self <= * other, * self >= * other) {
@@ -1364,15 +1385,17 @@ mod impls {
1364
1385
}
1365
1386
1366
1387
#[ 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 ( ) {
1368
1390
#[ inline]
1369
1391
fn partial_cmp ( & self , _: & ( ) ) -> Option < Ordering > {
1370
1392
Some ( Equal )
1371
1393
}
1372
1394
}
1373
1395
1374
1396
#[ 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 {
1376
1399
#[ inline]
1377
1400
fn partial_cmp ( & self , other : & bool ) -> Option < Ordering > {
1378
1401
Some ( self . cmp ( other) )
@@ -1384,7 +1407,8 @@ mod impls {
1384
1407
macro_rules! ord_impl {
1385
1408
( $( $t: ty) * ) => ( $(
1386
1409
#[ 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 {
1388
1412
#[ inline]
1389
1413
fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
1390
1414
Some ( self . cmp( other) )
@@ -1400,7 +1424,8 @@ mod impls {
1400
1424
}
1401
1425
1402
1426
#[ 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 {
1404
1429
#[ inline]
1405
1430
fn cmp( & self , other: & $t) -> Ordering {
1406
1431
// The order here is important to generate more optimal assembly.
@@ -1414,15 +1439,17 @@ mod impls {
1414
1439
}
1415
1440
1416
1441
#[ 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 ( ) {
1418
1444
#[ inline]
1419
1445
fn cmp ( & self , _other : & ( ) ) -> Ordering {
1420
1446
Equal
1421
1447
}
1422
1448
}
1423
1449
1424
1450
#[ 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 {
1426
1453
#[ inline]
1427
1454
fn cmp ( & self , other : & bool ) -> Ordering {
1428
1455
// Casting to i8's and converting the difference to an Ordering generates
@@ -1441,7 +1468,8 @@ mod impls {
1441
1468
ord_impl ! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1442
1469
1443
1470
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1444
- impl PartialEq for ! {
1471
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1472
+ impl const PartialEq for ! {
1445
1473
fn eq ( & self , _: & !) -> bool {
1446
1474
* self
1447
1475
}
@@ -1451,14 +1479,16 @@ mod impls {
1451
1479
impl Eq for ! { }
1452
1480
1453
1481
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1454
- impl PartialOrd for ! {
1482
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1483
+ impl const PartialOrd for ! {
1455
1484
fn partial_cmp ( & self , _: & !) -> Option < Ordering > {
1456
1485
* self
1457
1486
}
1458
1487
}
1459
1488
1460
1489
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1461
- impl Ord for ! {
1490
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "none" ) ]
1491
+ impl const Ord for ! {
1462
1492
fn cmp ( & self , _: & !) -> Ordering {
1463
1493
* self
1464
1494
}
0 commit comments