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 = "92391" ) ]
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,15 @@ 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
+ // 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
+ }
801
812
}
802
813
803
814
/// Compares and returns the minimum of two values.
@@ -816,8 +827,15 @@ pub trait Ord: Eq + PartialOrd<Self> {
816
827
fn min ( self , other : Self ) -> Self
817
828
where
818
829
Self : Sized ,
830
+ Self : ~const Destruct ,
819
831
{
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
+ }
821
839
}
822
840
823
841
/// Restrict a value to a certain interval.
@@ -841,6 +859,8 @@ pub trait Ord: Eq + PartialOrd<Self> {
841
859
fn clamp ( self , min : Self , max : Self ) -> Self
842
860
where
843
861
Self : Sized ,
862
+ Self : ~const Destruct ,
863
+ Self : ~const PartialOrd ,
844
864
{
845
865
assert ! ( min <= max) ;
846
866
if self < min {
@@ -862,15 +882,17 @@ pub macro Ord($item:item) {
862
882
}
863
883
864
884
#[ 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 {
866
887
#[ inline]
867
888
fn cmp ( & self , other : & Ordering ) -> Ordering {
868
889
( * self as i32 ) . cmp ( & ( * other as i32 ) )
869
890
}
870
891
}
871
892
872
893
#[ 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 {
874
896
#[ inline]
875
897
fn partial_cmp ( & self , other : & Ordering ) -> Option < Ordering > {
876
898
( * self as i32 ) . partial_cmp ( & ( * other as i32 ) )
@@ -1187,8 +1209,9 @@ pub macro PartialOrd($item:item) {
1187
1209
#[ inline]
1188
1210
#[ must_use]
1189
1211
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1212
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1190
1213
#[ 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 {
1192
1215
v1. min ( v2)
1193
1216
}
1194
1217
@@ -1250,8 +1273,9 @@ pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
1250
1273
#[ inline]
1251
1274
#[ must_use]
1252
1275
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1276
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1253
1277
#[ 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 {
1255
1279
v1. max ( v2)
1256
1280
}
1257
1281
@@ -1304,7 +1328,8 @@ mod impls {
1304
1328
macro_rules! partial_eq_impl {
1305
1329
( $( $t: ty) * ) => ( $(
1306
1330
#[ 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 {
1308
1333
#[ inline]
1309
1334
fn eq( & self , other: & $t) -> bool { ( * self ) == ( * other) }
1310
1335
#[ inline]
@@ -1314,7 +1339,8 @@ mod impls {
1314
1339
}
1315
1340
1316
1341
#[ 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 ( ) {
1318
1344
#[ inline]
1319
1345
fn eq ( & self , _other : & ( ) ) -> bool {
1320
1346
true
@@ -1341,7 +1367,8 @@ mod impls {
1341
1367
macro_rules! partial_ord_impl {
1342
1368
( $( $t: ty) * ) => ( $(
1343
1369
#[ 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 {
1345
1372
#[ inline]
1346
1373
fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
1347
1374
match ( * self <= * other, * self >= * other) {
@@ -1364,15 +1391,17 @@ mod impls {
1364
1391
}
1365
1392
1366
1393
#[ 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 ( ) {
1368
1396
#[ inline]
1369
1397
fn partial_cmp ( & self , _: & ( ) ) -> Option < Ordering > {
1370
1398
Some ( Equal )
1371
1399
}
1372
1400
}
1373
1401
1374
1402
#[ 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 {
1376
1405
#[ inline]
1377
1406
fn partial_cmp ( & self , other : & bool ) -> Option < Ordering > {
1378
1407
Some ( self . cmp ( other) )
@@ -1384,7 +1413,8 @@ mod impls {
1384
1413
macro_rules! ord_impl {
1385
1414
( $( $t: ty) * ) => ( $(
1386
1415
#[ 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 {
1388
1418
#[ inline]
1389
1419
fn partial_cmp( & self , other: & $t) -> Option <Ordering > {
1390
1420
Some ( self . cmp( other) )
@@ -1400,7 +1430,8 @@ mod impls {
1400
1430
}
1401
1431
1402
1432
#[ 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 {
1404
1435
#[ inline]
1405
1436
fn cmp( & self , other: & $t) -> Ordering {
1406
1437
// The order here is important to generate more optimal assembly.
@@ -1414,15 +1445,17 @@ mod impls {
1414
1445
}
1415
1446
1416
1447
#[ 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 ( ) {
1418
1450
#[ inline]
1419
1451
fn cmp ( & self , _other : & ( ) ) -> Ordering {
1420
1452
Equal
1421
1453
}
1422
1454
}
1423
1455
1424
1456
#[ 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 {
1426
1459
#[ inline]
1427
1460
fn cmp ( & self , other : & bool ) -> Ordering {
1428
1461
// Casting to i8's and converting the difference to an Ordering generates
@@ -1441,7 +1474,8 @@ mod impls {
1441
1474
ord_impl ! { char usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
1442
1475
1443
1476
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1444
- impl PartialEq for ! {
1477
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1478
+ impl const PartialEq for ! {
1445
1479
fn eq ( & self , _: & !) -> bool {
1446
1480
* self
1447
1481
}
@@ -1451,14 +1485,16 @@ mod impls {
1451
1485
impl Eq for ! { }
1452
1486
1453
1487
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1454
- impl PartialOrd for ! {
1488
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1489
+ impl const PartialOrd for ! {
1455
1490
fn partial_cmp ( & self , _: & !) -> Option < Ordering > {
1456
1491
* self
1457
1492
}
1458
1493
}
1459
1494
1460
1495
#[ unstable( feature = "never_type" , issue = "35121" ) ]
1461
- impl Ord for ! {
1496
+ #[ rustc_const_unstable( feature = "const_cmp" , issue = "92391" ) ]
1497
+ impl const Ord for ! {
1462
1498
fn cmp ( & self , _: & !) -> Ordering {
1463
1499
* self
1464
1500
}
0 commit comments