about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2013-04-11 08:33:41 -0400
committerDaniel Micay <danielmicay@gmail.com>2013-04-11 19:01:03 -0400
commit61b29993ddd531c6f53f7d21ddf28d26d4d63a21 (patch)
tree578b5658e1d5bd1d398c38468dda28813fdb5266
parente0defb8466e624ded66ef00f092a86b638e01152 (diff)
downloadrust-61b29993ddd531c6f53f7d21ddf28d26d4d63a21.tar.gz
rust-61b29993ddd531c6f53f7d21ddf28d26d4d63a21.zip
bool: implement Ord and TotalOrd
-rw-r--r--src/libcore/bool.rs99
1 files changed, 72 insertions, 27 deletions
diff --git a/src/libcore/bool.rs b/src/libcore/bool.rs
index a30cb926937..6c60cec2595 100644
--- a/src/libcore/bool.rs
+++ b/src/libcore/bool.rs
@@ -8,14 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-
 //! Boolean logic
 
+#[cfg(notest)]
+use cmp::{Eq, Ord, TotalOrd, Ordering};
 use option::{None, Option, Some};
 use from_str::FromStr;
 
-#[cfg(notest)] use cmp;
-
 /// Negation / inverse
 pub fn not(v: bool) -> bool { !v }
 
@@ -73,40 +72,86 @@ pub fn all_values(blk: &fn(v: bool)) {
 }
 
 /// converts truth value to an 8 bit byte
+#[inline(always)]
 pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
 
 #[cfg(notest)]
-impl cmp::Eq for bool {
+impl Ord for bool {
+    #[inline(always)]
+    fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
+    #[inline(always)]
+    fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
+    #[inline(always)]
+    fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
+    #[inline(always)]
+    fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
+}
+
+#[cfg(notest)]
+impl TotalOrd for bool {
+    #[inline(always)]
+    fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
+}
+
+#[cfg(notest)]
+impl Eq for bool {
+    #[inline(always)]
     fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
+    #[inline(always)]
     fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
 }
 
-#[test]
-pub fn test_bool_from_str() {
-    use from_str::FromStr;
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use prelude::*;
 
-    do all_values |v| {
-        assert!(Some(v) == FromStr::from_str(to_str(v)))
+    #[test]
+    fn test_bool_from_str() {
+        use from_str::FromStr;
+
+        do all_values |v| {
+            assert!(Some(v) == FromStr::from_str(to_str(v)))
+        }
     }
-}
 
-#[test]
-pub fn test_bool_to_str() {
-    assert!(to_str(false) == ~"false");
-    assert!(to_str(true) == ~"true");
-}
+    #[test]
+    fn test_bool_to_str() {
+        assert!(to_str(false) == ~"false");
+        assert!(to_str(true) == ~"true");
+    }
 
-#[test]
-pub fn test_bool_to_bit() {
-    do all_values |v| {
-        assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
+    #[test]
+    fn test_bool_to_bit() {
+        do all_values |v| {
+            assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
+        }
     }
-}
 
-// Local Variables:
-// mode: rust;
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
+    #[test]
+    fn test_bool_ord() {
+        assert!(true > false);
+        assert!(!(false > true));
+
+        assert!(false < true);
+        assert!(!(true < false));
+
+        assert!(false <= false);
+        assert!(false >= false);
+        assert!(true <= true);
+        assert!(true >= true);
+
+        assert!(false <= true);
+        assert!(!(false >= true));
+        assert!(true >= false);
+        assert!(!(true <= false));
+    }
+
+    #[test]
+    fn test_bool_totalord() {
+        assert_eq!(true.cmp(&true), Equal);
+        assert_eq!(false.cmp(&false), Equal);
+        assert_eq!(true.cmp(&false), Greater);
+        assert_eq!(false.cmp(&true), Less);
+    }
+}