@@ -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,11 @@ 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) {
1179
+ ordering. is_lt ( )
1180
+ } else {
1181
+ false
1182
+ }
1173
1183
}
1174
1184
1175
1185
/// This method tests less than or equal to (for `self` and `other`) and is used by the `<=`
@@ -1186,7 +1196,11 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1186
1196
#[ must_use]
1187
1197
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1188
1198
fn le ( & self , other : & Rhs ) -> bool {
1189
- matches ! ( self . partial_cmp( other) , Some ( Less | Equal ) )
1199
+ if let Some ( ordering) = self . partial_cmp ( other) {
1200
+ ordering. is_le ( )
1201
+ } else {
1202
+ false
1203
+ }
1190
1204
}
1191
1205
1192
1206
/// This method tests greater than (for `self` and `other`) and is used by the `>` operator.
@@ -1202,7 +1216,11 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1202
1216
#[ must_use]
1203
1217
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1204
1218
fn gt ( & self , other : & Rhs ) -> bool {
1205
- matches ! ( self . partial_cmp( other) , Some ( Greater ) )
1219
+ if let Some ( ordering) = self . partial_cmp ( other) {
1220
+ ordering. is_gt ( )
1221
+ } else {
1222
+ false
1223
+ }
1206
1224
}
1207
1225
1208
1226
/// This method tests greater than or equal to (for `self` and `other`) and is used by the `>=`
@@ -1219,7 +1237,11 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
1219
1237
#[ must_use]
1220
1238
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1221
1239
fn ge ( & self , other : & Rhs ) -> bool {
1222
- matches ! ( self . partial_cmp( other) , Some ( Greater | Equal ) )
1240
+ if let Some ( ordering) = self . partial_cmp ( other) {
1241
+ ordering. is_ge ( )
1242
+ } else {
1243
+ false
1244
+ }
1223
1245
}
1224
1246
}
1225
1247
@@ -1563,12 +1585,31 @@ mod impls {
1563
1585
impl Ord for $t {
1564
1586
#[ inline]
1565
1587
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 }
1588
+ let mut res = 0i8 ;
1589
+ res -= ( * self < * other ) as i8 ;
1590
+ res += ( * self > * other) as i8 ;
1591
+ // SAFETY: The discriminants of Ord were chosen to permit this
1592
+ unsafe { crate :: mem :: transmute ( res ) }
1571
1593
}
1594
+
1595
+ #[ inline]
1596
+ fn max( self , other: Self ) -> Self {
1597
+ if self > other {
1598
+ self
1599
+ } else {
1600
+ other
1601
+ }
1602
+ }
1603
+
1604
+ #[ inline]
1605
+ fn min( self , other: Self ) -> Self {
1606
+ if self > other {
1607
+ other
1608
+ } else {
1609
+ self
1610
+ }
1611
+ }
1612
+
1572
1613
}
1573
1614
) * )
1574
1615
}
0 commit comments