@@ -397,6 +397,12 @@ pub enum Ordering {
397
397
}
398
398
399
399
impl Ordering {
400
+ #[ inline]
401
+ const fn as_raw ( self ) -> i8 {
402
+ // FIXME(const-hack): just use `PartialOrd` against `Equal` once that's const
403
+ crate :: intrinsics:: discriminant_value ( & self )
404
+ }
405
+
400
406
/// Returns `true` if the ordering is the `Equal` variant.
401
407
///
402
408
/// # Examples
@@ -413,7 +419,11 @@ impl Ordering {
413
419
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
414
420
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
415
421
pub const fn is_eq ( self ) -> bool {
416
- matches ! ( self , Equal )
422
+ // All the `is_*` methods are implemented as comparisons against zero
423
+ // to follow how clang's libcxx implements their equivalents in
424
+ // <https://github.com/llvm/llvm-project/blob/60486292b79885b7800b082754153202bef5b1f0/libcxx/include/__compare/is_eq.h#L23-L28>
425
+
426
+ self . as_raw ( ) == 0
417
427
}
418
428
419
429
/// Returns `true` if the ordering is not the `Equal` variant.
@@ -432,7 +442,7 @@ impl Ordering {
432
442
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
433
443
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
434
444
pub const fn is_ne ( self ) -> bool {
435
- ! matches ! ( self , Equal )
445
+ self . as_raw ( ) != 0
436
446
}
437
447
438
448
/// Returns `true` if the ordering is the `Less` variant.
@@ -451,7 +461,7 @@ impl Ordering {
451
461
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
452
462
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
453
463
pub const fn is_lt ( self ) -> bool {
454
- matches ! ( self , Less )
464
+ self . as_raw ( ) < 0
455
465
}
456
466
457
467
/// Returns `true` if the ordering is the `Greater` variant.
@@ -470,7 +480,7 @@ impl Ordering {
470
480
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
471
481
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
472
482
pub const fn is_gt ( self ) -> bool {
473
- matches ! ( self , Greater )
483
+ self . as_raw ( ) > 0
474
484
}
475
485
476
486
/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
@@ -489,7 +499,7 @@ impl Ordering {
489
499
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
490
500
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
491
501
pub const fn is_le ( self ) -> bool {
492
- ! matches ! ( self , Greater )
502
+ self . as_raw ( ) <= 0
493
503
}
494
504
495
505
/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
@@ -508,7 +518,7 @@ impl Ordering {
508
518
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
509
519
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
510
520
pub const fn is_ge ( self ) -> bool {
511
- ! matches ! ( self , Less )
521
+ self . as_raw ( ) >= 0
512
522
}
513
523
514
524
/// Reverses the `Ordering`.
@@ -1369,7 +1379,9 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1369
1379
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1370
1380
#[ rustc_diagnostic_item = "cmp_partialord_lt" ]
1371
1381
fn lt ( & self , other : & Rhs ) -> bool {
1372
- self . partial_cmp ( other) . is_some_and ( Ordering :: is_lt)
1382
+ // FIXME(#137901): weirdly, passing these as `Ordering::is_lt` doesn't
1383
+ // MIR-inline, thus the "unnecessary" closure form here.
1384
+ self . partial_cmp ( other) . is_some_and ( |c| c. is_lt ( ) )
1373
1385
}
1374
1386
1375
1387
/// Tests less than or equal to (for `self` and `other`) and is used by the
@@ -1387,7 +1399,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1387
1399
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1388
1400
#[ rustc_diagnostic_item = "cmp_partialord_le" ]
1389
1401
fn le ( & self , other : & Rhs ) -> bool {
1390
- self . partial_cmp ( other) . is_some_and ( Ordering :: is_le)
1402
+ self . partial_cmp ( other) . is_some_and ( |c| c . is_le ( ) )
1391
1403
}
1392
1404
1393
1405
/// Tests greater than (for `self` and `other`) and is used by the `>`
@@ -1405,7 +1417,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1405
1417
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1406
1418
#[ rustc_diagnostic_item = "cmp_partialord_gt" ]
1407
1419
fn gt ( & self , other : & Rhs ) -> bool {
1408
- self . partial_cmp ( other) . is_some_and ( Ordering :: is_gt)
1420
+ self . partial_cmp ( other) . is_some_and ( |c| c . is_gt ( ) )
1409
1421
}
1410
1422
1411
1423
/// Tests greater than or equal to (for `self` and `other`) and is used by
@@ -1423,7 +1435,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1423
1435
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1424
1436
#[ rustc_diagnostic_item = "cmp_partialord_ge" ]
1425
1437
fn ge ( & self , other : & Rhs ) -> bool {
1426
- self . partial_cmp ( other) . is_some_and ( Ordering :: is_ge)
1438
+ self . partial_cmp ( other) . is_some_and ( |c| c . is_ge ( ) )
1427
1439
}
1428
1440
}
1429
1441
0 commit comments