]> git.lizzy.rs Git - rust.git/blob - src/libcore/bool.rs
libcore: Export core::from_str::FromStr from core::prelude
[rust.git] / src / libcore / bool.rs
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.
4 //
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.
10
11 //! Boolean logic
12
13 #[cfg(notest)]
14 use cmp::{Eq, Ord, TotalOrd, Ordering};
15 use option::{None, Option, Some};
16 use from_str::FromStr;
17
18 /// Negation / inverse
19 pub fn not(v: bool) -> bool { !v }
20
21 /// Conjunction
22 pub fn and(a: bool, b: bool) -> bool { a && b }
23
24 /// Disjunction
25 pub fn or(a: bool, b: bool) -> bool { a || b }
26
27 /**
28  * Exclusive or
29  *
30  * Identical to `or(and(a, not(b)), and(not(a), b))`
31  */
32 pub fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
33
34 /// Implication in the logic, i.e. from `a` follows `b`
35 pub fn implies(a: bool, b: bool) -> bool { !a || b }
36
37 /// true if truth values `a` and `b` are indistinguishable in the logic
38 pub fn eq(a: bool, b: bool) -> bool { a == b }
39
40 /// true if truth values `a` and `b` are distinguishable in the logic
41 pub fn ne(a: bool, b: bool) -> bool { a != b }
42
43 /// true if `v` represents truth in the logic
44 pub fn is_true(v: bool) -> bool { v }
45
46 /// true if `v` represents falsehood in the logic
47 pub fn is_false(v: bool) -> bool { !v }
48
49 /// Parse logic value from `s`
50 impl FromStr for bool {
51     fn from_str(s: &str) -> Option<bool> {
52         if s == "true" {
53             Some(true)
54         } else if s == "false" {
55             Some(false)
56         } else {
57             None
58         }
59     }
60 }
61
62 /// Convert `v` into a string
63 pub fn to_str(v: bool) -> ~str { if v { ~"true" } else { ~"false" } }
64
65 /**
66  * Iterates over all truth values by passing them to `blk` in an unspecified
67  * order
68  */
69 pub fn all_values(blk: &fn(v: bool)) {
70     blk(true);
71     blk(false);
72 }
73
74 /// converts truth value to an 8 bit byte
75 #[inline(always)]
76 pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
77
78 #[cfg(notest)]
79 impl Ord for bool {
80     #[inline(always)]
81     fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
82     #[inline(always)]
83     fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
84     #[inline(always)]
85     fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
86     #[inline(always)]
87     fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
88 }
89
90 #[cfg(notest)]
91 impl TotalOrd for bool {
92     #[inline(always)]
93     fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
94 }
95
96 #[cfg(notest)]
97 impl Eq for bool {
98     #[inline(always)]
99     fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
100     #[inline(always)]
101     fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
102 }
103
104 #[cfg(test)]
105 mod tests {
106     use super::*;
107     use prelude::*;
108
109     #[test]
110     fn test_bool_from_str() {
111         #[cfg(stage0)]
112         use from_str::FromStr;
113
114         do all_values |v| {
115             assert!(Some(v) == FromStr::from_str(to_str(v)))
116         }
117     }
118
119     #[test]
120     fn test_bool_to_str() {
121         assert!(to_str(false) == ~"false");
122         assert!(to_str(true) == ~"true");
123     }
124
125     #[test]
126     fn test_bool_to_bit() {
127         do all_values |v| {
128             assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
129         }
130     }
131
132     #[test]
133     fn test_bool_ord() {
134         assert!(true > false);
135         assert!(!(false > true));
136
137         assert!(false < true);
138         assert!(!(true < false));
139
140         assert!(false <= false);
141         assert!(false >= false);
142         assert!(true <= true);
143         assert!(true >= true);
144
145         assert!(false <= true);
146         assert!(!(false >= true));
147         assert!(true >= false);
148         assert!(!(true <= false));
149     }
150
151     #[test]
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);
157     }
158 }