@@ -401,12 +401,18 @@ impl Ordering {
401
401
/// assert_eq!(Ordering::Equal.is_eq(), true);
402
402
/// assert_eq!(Ordering::Greater.is_eq(), false);
403
403
/// ```
404
- #[ inline]
404
+ #[ inline( always ) ]
405
405
#[ must_use]
406
406
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
407
407
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
408
408
pub const fn is_eq ( self ) -> bool {
409
- matches ! ( self , Equal )
409
+ // Implementation note: It appears (as of 2022-12) that LLVM has an
410
+ // easier time with a comparison against zero like this, as opposed
411
+ // to looking for the `Less` value (-1) specifically, maybe because
412
+ // it's not always obvious to it that -2 isn't possible.
413
+ // Thus this and its siblings below are written this way, rather
414
+ // than the potentially-more-obvious `matches!` version.
415
+ ( self as i8 ) == 0
410
416
}
411
417
412
418
/// Returns `true` if the ordering is not the `Equal` variant.
@@ -420,12 +426,12 @@ impl Ordering {
420
426
/// assert_eq!(Ordering::Equal.is_ne(), false);
421
427
/// assert_eq!(Ordering::Greater.is_ne(), true);
422
428
/// ```
423
- #[ inline]
429
+ #[ inline( always ) ]
424
430
#[ must_use]
425
431
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
426
432
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
427
433
pub const fn is_ne ( self ) -> bool {
428
- ! matches ! ( self , Equal )
434
+ ( self as i8 ) != 0
429
435
}
430
436
431
437
/// Returns `true` if the ordering is the `Less` variant.
@@ -439,12 +445,12 @@ impl Ordering {
439
445
/// assert_eq!(Ordering::Equal.is_lt(), false);
440
446
/// assert_eq!(Ordering::Greater.is_lt(), false);
441
447
/// ```
442
- #[ inline]
448
+ #[ inline( always ) ]
443
449
#[ must_use]
444
450
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
445
451
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
446
452
pub const fn is_lt ( self ) -> bool {
447
- matches ! ( self , Less )
453
+ ( self as i8 ) < 0
448
454
}
449
455
450
456
/// Returns `true` if the ordering is the `Greater` variant.
@@ -458,12 +464,12 @@ impl Ordering {
458
464
/// assert_eq!(Ordering::Equal.is_gt(), false);
459
465
/// assert_eq!(Ordering::Greater.is_gt(), true);
460
466
/// ```
461
- #[ inline]
467
+ #[ inline( always ) ]
462
468
#[ must_use]
463
469
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
464
470
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
465
471
pub const fn is_gt ( self ) -> bool {
466
- matches ! ( self , Greater )
472
+ ( self as i8 ) > 0
467
473
}
468
474
469
475
/// Returns `true` if the ordering is either the `Less` or `Equal` variant.
@@ -477,12 +483,12 @@ impl Ordering {
477
483
/// assert_eq!(Ordering::Equal.is_le(), true);
478
484
/// assert_eq!(Ordering::Greater.is_le(), false);
479
485
/// ```
480
- #[ inline]
486
+ #[ inline( always ) ]
481
487
#[ must_use]
482
488
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
483
489
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
484
490
pub const fn is_le ( self ) -> bool {
485
- ! matches ! ( self , Greater )
491
+ ( self as i8 ) <= 0
486
492
}
487
493
488
494
/// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
@@ -496,12 +502,12 @@ impl Ordering {
496
502
/// assert_eq!(Ordering::Equal.is_ge(), true);
497
503
/// assert_eq!(Ordering::Greater.is_ge(), true);
498
504
/// ```
499
- #[ inline]
505
+ #[ inline( always ) ]
500
506
#[ must_use]
501
507
#[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
502
508
#[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
503
509
pub const fn is_ge ( self ) -> bool {
504
- ! matches ! ( self , Less )
510
+ ( self as i8 ) >= 0
505
511
}
506
512
507
513
/// Reverses the `Ordering`.
@@ -1169,7 +1175,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1169
1175
#[ must_use]
1170
1176
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1171
1177
fn lt ( & self , other : & Rhs ) -> bool {
1172
- matches ! ( self . partial_cmp( other) , Some ( Less ) )
1178
+ if let Some ( ordering ) = self . partial_cmp ( other) { ordering . is_lt ( ) } else { false }
1173
1179
}
1174
1180
1175
1181
/// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
@@ -1186,7 +1192,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1186
1192
#[ must_use]
1187
1193
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1188
1194
fn le ( & self , other : & Rhs ) -> bool {
1189
- matches ! ( self . partial_cmp( other) , Some ( Less | Equal ) )
1195
+ if let Some ( ordering ) = self . partial_cmp ( other) { ordering . is_le ( ) } else { false }
1190
1196
}
1191
1197
1192
1198
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
@@ -1202,7 +1208,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1202
1208
#[ must_use]
1203
1209
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1204
1210
fn gt ( & self , other : & Rhs ) -> bool {
1205
- matches ! ( self . partial_cmp( other) , Some ( Greater ) )
1211
+ if let Some ( ordering ) = self . partial_cmp ( other) { ordering . is_gt ( ) } else { false }
1206
1212
}
1207
1213
1208
1214
/// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
@@ -1219,7 +1225,7 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1219
1225
#[ must_use]
1220
1226
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1221
1227
fn ge ( & self , other : & Rhs ) -> bool {
1222
- matches ! ( self . partial_cmp( other) , Some ( Greater | Equal ) )
1228
+ if let Some ( ordering ) = self . partial_cmp ( other) { ordering . is_ge ( ) } else { false }
1223
1229
}
1224
1230
}
1225
1231
@@ -1563,12 +1569,31 @@ mod impls {
1563
1569
impl Ord for $t {
1564
1570
#[ inline]
1565
1571
fn cmp( & self , other: & $t) -> Ordering {
1566
- // The order here is important to generate more optimal assembly.
1567
- // See <https://github.com/rust-lang/rust/issues/63758> for more info.
1568
- if * self < * other { Less }
1569
- else if * self == * other { Equal }
1570
- else { Greater }
1572
+ let mut res = 0i8 ;
1573
+ res -= ( * self < * other ) as i8 ;
1574
+ res += ( * self > * other) as i8 ;
1575
+ // SAFETY: The discriminants of Ord were chosen to permit this
1576
+ unsafe { crate :: mem :: transmute ( res ) }
1571
1577
}
1578
+
1579
+ #[ inline]
1580
+ fn max( self , other: Self ) -> Self {
1581
+ if self > other {
1582
+ self
1583
+ } else {
1584
+ other
1585
+ }
1586
+ }
1587
+
1588
+ #[ inline]
1589
+ fn min( self , other: Self ) -> Self {
1590
+ if self > other {
1591
+ other
1592
+ } else {
1593
+ self
1594
+ }
1595
+ }
1596
+
1572
1597
}
1573
1598
) * )
1574
1599
}
0 commit comments