about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-04-11 16:28:00 -0700
committerbors <bors@rust-lang.org>2013-04-11 16:28:00 -0700
commitb3b8c0502bb7d223b4da50f3eac2c1ad51cf6c72 (patch)
tree0f63dd3e6629dc56cf5f4801139d449718f65c31
parentbfeb6d124dc924e52a218387a388caf87964162f (diff)
parent61b29993ddd531c6f53f7d21ddf28d26d4d63a21 (diff)
downloadrust-b3b8c0502bb7d223b4da50f3eac2c1ad51cf6c72.tar.gz
rust-b3b8c0502bb7d223b4da50f3eac2c1ad51cf6c72.zip
auto merge of #5845 : thestinger/rust/bool, r=catamorphism
This is mostly just to make deriving more convenient, which is probably why Haskell does this too.
-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);
+    }
+}