]> git.lizzy.rs Git - rust.git/commitdiff
cmp: rm TotalOrd impl code duplication
authorDaniel Micay <danielmicay@gmail.com>
Wed, 27 Mar 2013 18:15:24 +0000 (14:15 -0400)
committerDaniel Micay <danielmicay@gmail.com>
Wed, 27 Mar 2013 18:17:16 +0000 (14:17 -0400)
src/libcore/cmp.rs

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