about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-03-31 01:34:37 +1100
committerHuon Wilson <dbau.pp+github@gmail.com>2013-04-12 17:10:26 +1000
commit85b82c763bfbfd5de59f4c6b026dca58f3ba4687 (patch)
treedb7757ea51bc09e0dcefa9e1af77b97cf26c306a
parent3698ea7e54c46cfde377f95782710ee5e19876d3 (diff)
downloadrust-85b82c763bfbfd5de59f4c6b026dca58f3ba4687.tar.gz
rust-85b82c763bfbfd5de59f4c6b026dca58f3ba4687.zip
libcore: combine cmp::Ordering instances in lexical order.
-rw-r--r--src/libcore/cmp.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs
index f96575aaf41..2c2b7f40f31 100644
--- a/src/libcore/cmp.rs
+++ b/src/libcore/cmp.rs
@@ -117,6 +117,19 @@ totalord_impl!(int)
 totalord_impl!(uint)
 
 /**
+Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
+lexical ordering on a type `(int, int)`.
+*/
+// used in deriving code in libsyntax
+#[inline(always)]
+pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
+    match o1 {
+        Equal => o2,
+        _ => o1
+    }
+}
+
+/**
 * Trait for values that can be compared for a sort-order.
 *
 * Eventually this may be simplified to only require
@@ -184,6 +197,8 @@ pub fn max<T:Ord>(v1: T, v2: T) -> T {
 
 #[cfg(test)]
 mod test {
+    use super::lexical_ordering;
+
     #[test]
     fn test_int_totalord() {
         assert_eq!(5.cmp(&10), Less);
@@ -204,4 +219,16 @@ mod test {
         assert!(Less < Equal);
         assert_eq!(Greater.cmp(&Less), Greater);
     }
+
+    #[test]
+    fn test_lexical_ordering() {
+        fn t(o1: Ordering, o2: Ordering, e: Ordering) {
+            assert_eq!(lexical_ordering(o1, o2), e);
+        }
+        for [Less, Equal, Greater].each |&o| {
+            t(Less, o, Less);
+            t(Equal, o, o);
+            t(Greater, o, Greater);
+         }
+    }
 }