Skip to content

Commit

Permalink
Specialize PartialOrd for totally ordered primitive types
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
ranma42 committed Sep 16, 2015
1 parent 0f1f5fc commit 1614173
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/libcore/cmp.rs
Expand Up @@ -463,17 +463,35 @@ mod impls {
}
}

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

macro_rules! ord_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl PartialOrd for $t {
#[inline]
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
if *self == *other { Some(Equal) }
else if *self < *other { Some(Less) }
else { Some(Greater) }
}
#[inline]
fn lt(&self, other: &$t) -> bool { (*self) < (*other) }
#[inline]
fn le(&self, other: &$t) -> bool { (*self) <= (*other) }
#[inline]
fn ge(&self, other: &$t) -> bool { (*self) >= (*other) }
#[inline]
fn gt(&self, other: &$t) -> bool { (*self) > (*other) }
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for $t {
#[inline]
fn cmp(&self, other: &$t) -> Ordering {
if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
if *self == *other { Equal }
else if *self < *other { Less }
else { Greater }
}
}
)*)
Expand Down

0 comments on commit 1614173

Please sign in to comment.