(index<- ) ./libstd/bool.rs
git branch: * master c7553ea auto merge of #13609 : richo/rust/str-type-vim, r=alexcrichton
modified: Sat Apr 19 11:22:39 2014
1 // Copyright 2013 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 //! Operations on boolean values (`bool` type)
12 //!
13 //! A quick summary:
14 //!
15 //! Implementations of the following traits:
16 //!
17 //! * `FromStr`
18 //! * `Not`
19 //! * `Ord`
20 //! * `TotalOrd`
21 //! * `Eq`
22 //! * `Default`
23 //! * `Zero`
24 //!
25 //! A `to_bit` conversion function.
26
27 use from_str::FromStr;
28 use num::{Int, one, zero};
29 use option::{None, Option, Some};
30
31 #[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
32 #[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
33 #[cfg(not(test))] use default::Default;
34
35 /////////////////////////////////////////////////////////////////////////////
36 // Freestanding functions
37 /////////////////////////////////////////////////////////////////////////////
38
39 /// Convert a `bool` to an integer.
40 ///
41 /// # Examples
42 ///
43 /// ```rust
44 /// use std::bool;
45 ///
46 /// assert_eq!(bool::to_bit::<u8>(true), 1u8);
47 /// assert_eq!(bool::to_bit::<u8>(false), 0u8);
48 /// ```
49 #[inline]
50 pub fn to_bit<N: Int>(p: bool) -> N {
51 if p { one() } else { zero() }
52 }
53
54 /////////////////////////////////////////////////////////////////////////////
55 // Trait impls on `bool`
56 /////////////////////////////////////////////////////////////////////////////
57
58 impl FromStr for bool {
59 /// Parse a `bool` from a string.
60 ///
61 /// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
62 ///
63 /// # Examples
64 ///
65 /// ```rust
66 /// assert_eq!(from_str::<bool>("true"), Some(true));
67 /// assert_eq!(from_str::<bool>("false"), Some(false));
68 /// assert_eq!(from_str::<bool>("not even a boolean"), None);
69 /// ```
70 #[inline]
71 fn from_str(s: &str) -> Option<bool> {
72 match s {
73 "true" => Some(true),
74 "false" => Some(false),
75 _ => None,
76 }
77 }
78 }
79
80 #[cfg(not(test))]
81 impl Not<bool> for bool {
82 /// The logical complement of a boolean value.
83 ///
84 /// # Examples
85 ///
86 /// ```rust
87 /// assert_eq!(!true, false);
88 /// assert_eq!(!false, true);
89 /// ```
90 #[inline]
91 fn not(&self) -> bool { !*self }
92 }
93
94 #[cfg(not(test))]
95 impl BitAnd<bool, bool> for bool {
96 /// Conjunction of two boolean values.
97 ///
98 /// # Examples
99 ///
100 /// ```rust
101 /// assert_eq!(false.bitand(&false), false);
102 /// assert_eq!(true.bitand(&false), false);
103 /// assert_eq!(false.bitand(&true), false);
104 /// assert_eq!(true.bitand(&true), true);
105 ///
106 /// assert_eq!(false & false, false);
107 /// assert_eq!(true & false, false);
108 /// assert_eq!(false & true, false);
109 /// assert_eq!(true & true, true);
110 /// ```
111 #[inline]
112 fn bitand(&self, b: &bool) -> bool { *self & *b }
113 }
114
115 #[cfg(not(test))]
116 impl BitOr<bool, bool> for bool {
117 /// Disjunction of two boolean values.
118 ///
119 /// # Examples
120 ///
121 /// ```rust
122 /// assert_eq!(false.bitor(&false), false);
123 /// assert_eq!(true.bitor(&false), true);
124 /// assert_eq!(false.bitor(&true), true);
125 /// assert_eq!(true.bitor(&true), true);
126 ///
127 /// assert_eq!(false | false, false);
128 /// assert_eq!(true | false, true);
129 /// assert_eq!(false | true, true);
130 /// assert_eq!(true | true, true);
131 /// ```
132 #[inline]
133 fn bitor(&self, b: &bool) -> bool { *self | *b }
134 }
135
136 #[cfg(not(test))]
137 impl BitXor<bool, bool> for bool {
138 /// An 'exclusive or' of two boolean values.
139 ///
140 /// 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
141 ///
142 /// # Examples
143 ///
144 /// ```rust
145 /// assert_eq!(false.bitxor(&false), false);
146 /// assert_eq!(true.bitxor(&false), true);
147 /// assert_eq!(false.bitxor(&true), true);
148 /// assert_eq!(true.bitxor(&true), false);
149 ///
150 /// assert_eq!(false ^ false, false);
151 /// assert_eq!(true ^ false, true);
152 /// assert_eq!(false ^ true, true);
153 /// assert_eq!(true ^ true, false);
154 /// ```
155 #[inline]
156 fn bitxor(&self, b: &bool) -> bool { *self ^ *b }
157 }
158
159 #[cfg(not(test))]
160 impl Ord for bool {
161 #[inline]
162 fn lt(&self, other: &bool) -> bool {
163 to_bit::<u8>(*self) < to_bit(*other)
164 }
165 }
166
167 #[cfg(not(test))]
168 impl TotalOrd for bool {
169 #[inline]
170 fn cmp(&self, other: &bool) -> Ordering {
171 to_bit::<u8>(*self).cmp(&to_bit(*other))
172 }
173 }
174
175 /// Equality between two boolean values.
176 ///
177 /// Two booleans are equal if they have the same value.
178 ///
179 /// # Examples
180 ///
181 /// ```rust
182 /// assert_eq!(false.eq(&true), false);
183 /// assert_eq!(false == false, true);
184 /// assert_eq!(false != true, true);
185 /// assert_eq!(false.ne(&false), false);
186 /// ```
187 #[cfg(not(test))]
188 impl Eq for bool {
189 #[inline]
190 fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
191 }
192
193 #[cfg(not(test))]
194 impl Default for bool {
195 fn default() -> bool { false }
196 }
197
198 #[cfg(test)]
199 mod tests {
200 use prelude::*;
201 use super::to_bit;
202 use str::StrSlice;
203
204 #[test]
205 fn test_to_bit() {
206 assert_eq!(to_bit::<u8>(true), 1u8);
207 assert_eq!(to_bit::<u8>(false), 0u8);
208 }
209
210 #[test]
211 fn test_eq() {
212 assert_eq!(false.eq(&true), false);
213 assert_eq!(false == false, true);
214 assert_eq!(false != true, true);
215 assert_eq!(false.ne(&false), false);
216 }
217
218 #[test]
219 fn test_bitand() {
220 assert_eq!(false.bitand(&false), false);
221 assert_eq!(true.bitand(&false), false);
222 assert_eq!(false.bitand(&true), false);
223 assert_eq!(true.bitand(&true), true);
224
225 assert_eq!(false & false, false);
226 assert_eq!(true & false, false);
227 assert_eq!(false & true, false);
228 assert_eq!(true & true, true);
229 }
230
231 #[test]
232 fn test_bitor() {
233 assert_eq!(false.bitor(&false), false);
234 assert_eq!(true.bitor(&false), true);
235 assert_eq!(false.bitor(&true), true);
236 assert_eq!(true.bitor(&true), true);
237
238 assert_eq!(false | false, false);
239 assert_eq!(true | false, true);
240 assert_eq!(false | true, true);
241 assert_eq!(true | true, true);
242 }
243
244 #[test]
245 fn test_bitxor() {
246 assert_eq!(false.bitxor(&false), false);
247 assert_eq!(true.bitxor(&false), true);
248 assert_eq!(false.bitxor(&true), true);
249 assert_eq!(true.bitxor(&true), false);
250
251 assert_eq!(false ^ false, false);
252 assert_eq!(true ^ false, true);
253 assert_eq!(false ^ true, true);
254 assert_eq!(true ^ true, false);
255 }
256
257 #[test]
258 fn test_not() {
259 assert_eq!(!true, false);
260 assert_eq!(!false, true);
261 }
262
263 #[test]
264 fn test_from_str() {
265 assert_eq!(from_str::<bool>("true"), Some(true));
266 assert_eq!(from_str::<bool>("false"), Some(false));
267 assert_eq!(from_str::<bool>("not even a boolean"), None);
268 }
269
270 #[test]
271 fn test_to_str() {
272 assert_eq!(false.to_str(), "false".to_owned());
273 assert_eq!(true.to_str(), "true".to_owned());
274 }
275
276 #[test]
277 fn test_ord() {
278 assert!(true > false);
279 assert!(!(false > true));
280
281 assert!(false < true);
282 assert!(!(true < false));
283
284 assert!(false <= false);
285 assert!(false >= false);
286 assert!(true <= true);
287 assert!(true >= true);
288
289 assert!(false <= true);
290 assert!(!(false >= true));
291 assert!(true >= false);
292 assert!(!(true <= false));
293 }
294
295 #[test]
296 fn test_totalord() {
297 assert!(true.cmp(&true) == Equal);
298 assert!(false.cmp(&false) == Equal);
299 assert!(true.cmp(&false) == Greater);
300 assert!(false.cmp(&true) == Less);
301 }
302 }