1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use cmp::{Eq, Ord, TotalOrd, Ordering};
15 use option::{None, Option, Some};
16 use from_str::FromStr;
18 /// Negation / inverse
19 pub fn not(v: bool) -> bool { !v }
22 pub fn and(a: bool, b: bool) -> bool { a && b }
25 pub fn or(a: bool, b: bool) -> bool { a || b }
30 * Identical to `or(and(a, not(b)), and(not(a), b))`
32 pub fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
34 /// Implication in the logic, i.e. from `a` follows `b`
35 pub fn implies(a: bool, b: bool) -> bool { !a || b }
37 /// true if truth values `a` and `b` are indistinguishable in the logic
38 pub fn eq(a: bool, b: bool) -> bool { a == b }
40 /// true if truth values `a` and `b` are distinguishable in the logic
41 pub fn ne(a: bool, b: bool) -> bool { a != b }
43 /// true if `v` represents truth in the logic
44 pub fn is_true(v: bool) -> bool { v }
46 /// true if `v` represents falsehood in the logic
47 pub fn is_false(v: bool) -> bool { !v }
49 /// Parse logic value from `s`
50 impl FromStr for bool {
51 fn from_str(s: &str) -> Option<bool> {
54 } else if s == "false" {
62 /// Convert `v` into a string
63 pub fn to_str(v: bool) -> ~str { if v { ~"true" } else { ~"false" } }
66 * Iterates over all truth values by passing them to `blk` in an unspecified
69 pub fn all_values(blk: &fn(v: bool)) {
74 /// converts truth value to an 8 bit byte
76 pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
81 fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
83 fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
85 fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
87 fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
91 impl TotalOrd for bool {
93 fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
99 fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
101 fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
110 fn test_bool_from_str() {
112 use from_str::FromStr;
115 assert!(Some(v) == FromStr::from_str(to_str(v)))
120 fn test_bool_to_str() {
121 assert!(to_str(false) == ~"false");
122 assert!(to_str(true) == ~"true");
126 fn test_bool_to_bit() {
128 assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
134 assert!(true > false);
135 assert!(!(false > true));
137 assert!(false < true);
138 assert!(!(true < false));
140 assert!(false <= false);
141 assert!(false >= false);
142 assert!(true <= true);
143 assert!(true >= true);
145 assert!(false <= true);
146 assert!(!(false >= true));
147 assert!(true >= false);
148 assert!(!(true <= false));
152 fn test_bool_totalord() {
153 assert_eq!(true.cmp(&true), Equal);
154 assert_eq!(false.cmp(&false), Equal);
155 assert_eq!(true.cmp(&false), Greater);
156 assert_eq!(false.cmp(&true), Less);