@@ -401,12 +401,18 @@ impl Ordering {
401401 /// assert_eq!(Ordering::Equal.is_eq(), true);
402402 /// assert_eq!(Ordering::Greater.is_eq(), false);
403403 /// ```
404- #[ inline]
404+ #[ inline( always ) ]
405405 #[ must_use]
406406 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
407407 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
408408 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
410416 }
411417
412418 /// Returns `true` if the ordering is not the `Equal` variant.
@@ -420,12 +426,12 @@ impl Ordering {
420426 /// assert_eq!(Ordering::Equal.is_ne(), false);
421427 /// assert_eq!(Ordering::Greater.is_ne(), true);
422428 /// ```
423- #[ inline]
429+ #[ inline( always ) ]
424430 #[ must_use]
425431 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
426432 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
427433 pub const fn is_ne ( self ) -> bool {
428- ! matches ! ( self , Equal )
434+ ( self as i8 ) != 0
429435 }
430436
431437 /// Returns `true` if the ordering is the `Less` variant.
@@ -439,12 +445,12 @@ impl Ordering {
439445 /// assert_eq!(Ordering::Equal.is_lt(), false);
440446 /// assert_eq!(Ordering::Greater.is_lt(), false);
441447 /// ```
442- #[ inline]
448+ #[ inline( always ) ]
443449 #[ must_use]
444450 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
445451 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
446452 pub const fn is_lt ( self ) -> bool {
447- matches ! ( self , Less )
453+ ( self as i8 ) < 0
448454 }
449455
450456 /// Returns `true` if the ordering is the `Greater` variant.
@@ -458,12 +464,12 @@ impl Ordering {
458464 /// assert_eq!(Ordering::Equal.is_gt(), false);
459465 /// assert_eq!(Ordering::Greater.is_gt(), true);
460466 /// ```
461- #[ inline]
467+ #[ inline( always ) ]
462468 #[ must_use]
463469 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
464470 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
465471 pub const fn is_gt ( self ) -> bool {
466- matches ! ( self , Greater )
472+ ( self as i8 ) > 0
467473 }
468474
469475 /// Returns `true` if the ordering is either the `Less` or `Equal` variant.
@@ -477,12 +483,12 @@ impl Ordering {
477483 /// assert_eq!(Ordering::Equal.is_le(), true);
478484 /// assert_eq!(Ordering::Greater.is_le(), false);
479485 /// ```
480- #[ inline]
486+ #[ inline( always ) ]
481487 #[ must_use]
482488 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
483489 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
484490 pub const fn is_le ( self ) -> bool {
485- ! matches ! ( self , Greater )
491+ ( self as i8 ) <= 0
486492 }
487493
488494 /// Returns `true` if the ordering is either the `Greater` or `Equal` variant.
@@ -496,12 +502,12 @@ impl Ordering {
496502 /// assert_eq!(Ordering::Equal.is_ge(), true);
497503 /// assert_eq!(Ordering::Greater.is_ge(), true);
498504 /// ```
499- #[ inline]
505+ #[ inline( always ) ]
500506 #[ must_use]
501507 #[ rustc_const_stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
502508 #[ stable( feature = "ordering_helpers" , since = "1.53.0" ) ]
503509 pub const fn is_ge ( self ) -> bool {
504- ! matches ! ( self , Less )
510+ ( self as i8 ) >= 0
505511 }
506512
507513 /// Reverses the `Ordering`.
@@ -1169,7 +1175,11 @@ pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
11691175 #[ must_use]
11701176 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11711177 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+ }
11731183 }
11741184
11751185 /// 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> {
11861196 #[ must_use]
11871197 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
11881198 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+ }
11901204 }
11911205
11921206 /// 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> {
12021216 #[ must_use]
12031217 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
12041218 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+ }
12061224 }
12071225
12081226 /// 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> {
12191237 #[ must_use]
12201238 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
12211239 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+ }
12231245 }
12241246}
12251247
@@ -1563,12 +1585,31 @@ mod impls {
15631585 impl Ord for $t {
15641586 #[ inline]
15651587 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 ) }
15711593 }
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+
15721613 }
15731614 ) * )
15741615 }
0 commit comments