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