Skip to content

Commit 1614173

Browse files
committed
Specialize PartialOrd for totally ordered primitive types
Knowing the result of equality comparison can enable additional optimizations in LLVM. Additionally, this makes it obvious that `partial_cmp` on totally ordered types cannot return `None`.
1 parent 0f1f5fc commit 1614173

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

src/libcore/cmp.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,17 +463,35 @@ mod impls {
463463
}
464464
}
465465

466-
partial_ord_impl! { char usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
466+
partial_ord_impl! { f32 f64 }
467467

468468
macro_rules! ord_impl {
469469
($($t:ty)*) => ($(
470+
#[stable(feature = "rust1", since = "1.0.0")]
471+
impl PartialOrd for $t {
472+
#[inline]
473+
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
474+
if *self == *other { Some(Equal) }
475+
else if *self < *other { Some(Less) }
476+
else { Some(Greater) }
477+
}
478+
#[inline]
479+
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
480+
#[inline]
481+
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
482+
#[inline]
483+
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
484+
#[inline]
485+
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
486+
}
487+
470488
#[stable(feature = "rust1", since = "1.0.0")]
471489
impl Ord for $t {
472490
#[inline]
473491
fn cmp(&self, other: &$t) -> Ordering {
474-
if *self < *other { Less }
475-
else if *self > *other { Greater }
476-
else { Equal }
492+
if *self == *other { Equal }
493+
else if *self < *other { Less }
494+
else { Greater }
477495
}
478496
}
479497
)*)

0 commit comments

Comments
 (0)