about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-04-09 16:09:59 -0700
committerbors <bors@rust-lang.org>2013-04-09 16:09:59 -0700
commit5e570ce4b087ad7f8623772b64a3d618df9b7756 (patch)
tree7c5060cf2892014354fc659a29f2cce6e82766df
parent81ba65da472c6794372130353a25891a36b1fe74 (diff)
parenta3f40184bdc0d094ac76b15f66bfbf87a699a705 (diff)
downloadrust-5e570ce4b087ad7f8623772b64a3d618df9b7756.tar.gz
rust-5e570ce4b087ad7f8623772b64a3d618df9b7756.zip
auto merge of #5766 : thestinger/rust/cmp, r=brson
It was simpler to just give the variants a value instead of listing out all the cases for (*self, *other) in a match statement or writing spaghetti code. This makes the `cmp` method easier to use with FFI too, since you're a cast away from an idiomatic C comparator function. It would be fine implemented another way though.
-rw-r--r--src/libcore/cmp.rs28
1 files changed, 26 insertions, 2 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index a928d3bb2af..f96575aaf41 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -64,14 +64,32 @@ totaleq_impl!(i64)
 totaleq_impl!(int)
 totaleq_impl!(uint)
 
-#[deriving(Eq)]
-pub enum Ordering { Less, Equal, Greater }
+#[deriving(Clone, Eq)]
+pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
 
 /// Trait for types that form a total order
 pub trait TotalOrd: TotalEq {
     fn cmp(&self, other: &Self) -> Ordering;
 }
 
+impl TotalOrd for Ordering {
+    #[inline(always)]
+    fn cmp(&self, other: &Ordering) -> Ordering {
+        (*self as int).cmp(&(*other as int))
+    }
+}
+
+impl Ord for Ordering {
+    #[inline(always)]
+    fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
+    #[inline(always)]
+    fn le(&self, other: &Ordering) -> bool { (*self as int) <= (*other as int) }
+    #[inline(always)]
+    fn gt(&self, other: &Ordering) -> bool { (*self as int) > (*other as int) }
+    #[inline(always)]
+    fn ge(&self, other: &Ordering) -> bool { (*self as int) >= (*other as int) }
+}
+
 macro_rules! totalord_impl(
     ($t:ty) => {
         impl TotalOrd for $t {
@@ -180,4 +198,10 @@ mod test {
         assert!(5.equals(&5));
         assert!(!2.equals(&17));
     }
+
+    #[test]
+    fn test_ordering_order() {
+        assert!(Less < Equal);
+        assert_eq!(Greater.cmp(&Less), Greater);
+    }
 }