cmp: rm TotalOrd impl code duplication

This commit is contained in:
Daniel Micay 2013-03-27 14:15:24 -04:00
parent b93393e907
commit 91cb6687a8

View file

@ -45,62 +45,31 @@ pub trait TotalOrd {
fn cmp(&self, other: &Self) -> Ordering; fn cmp(&self, other: &Self) -> Ordering;
} }
#[inline(always)] macro_rules! totalord_impl(
fn icmp<T: Ord>(a: &T, b: &T) -> Ordering { ($t:ty) => {
if *a < *b { Less } impl TotalOrd for $t {
else if *a > *b { Greater } #[inline(always)]
else { Equal } fn cmp(&self, other: &$t) -> Ordering {
} if *self < *other { Less }
else if *self > *other { Greater }
else { Equal }
}
}
}
)
impl TotalOrd for u8 { totalord_impl!(u8)
#[inline(always)] totalord_impl!(u16)
fn cmp(&self, other: &u8) -> Ordering { icmp(self, other) } totalord_impl!(u32)
} totalord_impl!(u64)
impl TotalOrd for u16 { totalord_impl!(i8)
#[inline(always)] totalord_impl!(i16)
fn cmp(&self, other: &u16) -> Ordering { icmp(self, other) } totalord_impl!(i32)
} totalord_impl!(i64)
impl TotalOrd for u32 { totalord_impl!(int)
#[inline(always)] totalord_impl!(uint)
fn cmp(&self, other: &u32) -> Ordering { icmp(self, other) }
}
impl TotalOrd for u64 {
#[inline(always)]
fn cmp(&self, other: &u64) -> Ordering { icmp(self, other) }
}
impl TotalOrd for i8 {
#[inline(always)]
fn cmp(&self, other: &i8) -> Ordering { icmp(self, other) }
}
impl TotalOrd for i16 {
#[inline(always)]
fn cmp(&self, other: &i16) -> Ordering { icmp(self, other) }
}
impl TotalOrd for i32 {
#[inline(always)]
fn cmp(&self, other: &i32) -> Ordering { icmp(self, other) }
}
impl TotalOrd for i64 {
#[inline(always)]
fn cmp(&self, other: &i64) -> Ordering { icmp(self, other) }
}
impl TotalOrd for int {
#[inline(always)]
fn cmp(&self, other: &int) -> Ordering { icmp(self, other) }
}
impl TotalOrd for uint {
#[inline(always)]
fn cmp(&self, other: &uint) -> Ordering { icmp(self, other) }
}
/** /**
* Trait for values that can be compared for a sort-order. * Trait for values that can be compared for a sort-order.