12
12
//!
13
13
//! A quick summary:
14
14
//!
15
- //! ## Trait implementations for `bool`
16
- //!
17
15
//! Implementations of the following traits:
18
16
//!
19
17
//! * `FromStr`
24
22
//! * `Default`
25
23
//! * `Zero`
26
24
//!
27
- //! ## Various functions to compare `bool`s
28
- //!
29
- //! All of the standard comparison functions one would expect: `and`, `eq`, `or`,
30
- //! and more.
31
- //!
32
- //! Also, a few conversion functions: `to_bit` and `to_str`.
25
+ //! A `to_bit` conversion function.
33
26
34
- use option:: { None , Option , Some } ;
35
27
use from_str:: FromStr ;
36
- use num:: FromPrimitive ;
28
+ use num:: { Int , one, zero} ;
29
+ use option:: { None , Option , Some } ;
37
30
38
31
#[ cfg( not( test) ) ] use cmp:: { Eq , Ord , TotalOrd , Ordering } ;
39
32
#[ cfg( not( test) ) ] use ops:: { Not , BitAnd , BitOr , BitXor } ;
@@ -43,112 +36,19 @@ use num::FromPrimitive;
43
36
// Freestanding functions
44
37
/////////////////////////////////////////////////////////////////////////////
45
38
46
- /// Iterates over all truth values, passing them to the given block.
47
- ///
48
- /// There are no guarantees about the order values will be given.
39
+ /// Convert a `bool` to an integer.
49
40
///
50
41
/// # Examples
51
42
///
52
- /// ```
53
- /// std::bool::all_values(|x: bool| {
54
- /// println!("{}", x);
55
- /// })
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);
56
48
/// ```
57
49
#[ inline]
58
- pub fn all_values ( blk: |v: bool |) {
59
- blk ( true ) ;
60
- blk ( false ) ;
61
- }
62
-
63
- /////////////////////////////////////////////////////////////////////////////
64
- // Methods on `bool`
65
- /////////////////////////////////////////////////////////////////////////////
66
-
67
- /// Extension methods on a `bool`
68
- pub trait Bool {
69
- /// Conjunction of two boolean values.
70
- ///
71
- /// # Examples
72
- ///
73
- /// ```rust
74
- /// assert_eq!(true.and(true), true);
75
- /// assert_eq!(true.and(false), false);
76
- /// assert_eq!(false.and(true), false);
77
- /// assert_eq!(false.and(false), false);
78
- /// ```
79
- fn and ( self , b : bool ) -> bool ;
80
-
81
- /// Disjunction of two boolean values.
82
- ///
83
- /// # Examples
84
- ///
85
- /// ```rust
86
- /// assert_eq!(true.or(true), true);
87
- /// assert_eq!(true.or(false), true);
88
- /// assert_eq!(false.or(true), true);
89
- /// assert_eq!(false.or(false), false);
90
- /// ```
91
- fn or ( self , b : bool ) -> bool ;
92
-
93
- /// An 'exclusive or' of two boolean values.
94
- ///
95
- /// 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
96
- ///
97
- /// # Examples
98
- ///
99
- /// ```rust
100
- /// assert_eq!(true.xor(true), false);
101
- /// assert_eq!(true.xor(false), true);
102
- /// assert_eq!(false.xor(true), true);
103
- /// assert_eq!(false.xor(false), false);
104
- /// ```
105
- fn xor ( self , b : bool ) -> bool ;
106
-
107
- /// Implication between two boolean values.
108
- ///
109
- /// Implication is often phrased as 'if a then b.'
110
- ///
111
- /// 'if a then b' is equivalent to `!a || b`.
112
- ///
113
- /// # Examples
114
- ///
115
- /// ```rust
116
- /// assert_eq!(true.implies(true), true);
117
- /// assert_eq!(true.implies(false), false);
118
- /// assert_eq!(false.implies(true), true);
119
- /// assert_eq!(false.implies(false), true);
120
- /// ```
121
- fn implies ( self , b : bool ) -> bool ;
122
-
123
- /// Convert a `bool` to a `u8`.
124
- ///
125
- /// # Examples
126
- ///
127
- /// ```rust
128
- /// assert_eq!(true.to_bit::<u8>(), 1u8);
129
- /// assert_eq!(false.to_bit::<u8>(), 0u8);
130
- /// ```
131
- fn to_bit < N : FromPrimitive > ( self ) -> N ;
132
- }
133
-
134
- impl Bool for bool {
135
- #[ inline]
136
- fn and ( self , b : bool ) -> bool { self && b }
137
-
138
- #[ inline]
139
- fn or ( self , b : bool ) -> bool { self || b }
140
-
141
- #[ inline]
142
- fn xor ( self , b : bool ) -> bool { self ^ b }
143
-
144
- #[ inline]
145
- fn implies ( self , b : bool ) -> bool { !self || b }
146
-
147
- #[ inline]
148
- fn to_bit < N : FromPrimitive > ( self ) -> N {
149
- if self { FromPrimitive :: from_u8 ( 1 ) . unwrap ( ) }
150
- else { FromPrimitive :: from_u8 ( 0 ) . unwrap ( ) }
151
- }
50
+ pub fn to_bit < N : Int > ( p : bool ) -> N {
51
+ if p { one ( ) } else { zero ( ) }
152
52
}
153
53
154
54
/////////////////////////////////////////////////////////////////////////////
@@ -259,13 +159,17 @@ impl BitXor<bool, bool> for bool {
259
159
#[ cfg( not( test) ) ]
260
160
impl Ord for bool {
261
161
#[ inline]
262
- fn lt ( & self , other : & bool ) -> bool { self . to_bit :: < u8 > ( ) < other. to_bit ( ) }
162
+ fn lt ( & self , other : & bool ) -> bool {
163
+ to_bit :: < u8 > ( * self ) < to_bit ( * other)
164
+ }
263
165
}
264
166
265
167
#[ cfg( not( test) ) ]
266
168
impl TotalOrd for bool {
267
169
#[ inline]
268
- fn cmp ( & self , other : & bool ) -> Ordering { self . to_bit :: < u8 > ( ) . cmp ( & other. to_bit ( ) ) }
170
+ fn cmp ( & self , other : & bool ) -> Ordering {
171
+ to_bit :: < u8 > ( * self ) . cmp ( & to_bit ( * other) )
172
+ }
269
173
}
270
174
271
175
/// Equality between two boolean values.
@@ -294,16 +198,24 @@ impl Default for bool {
294
198
#[ cfg( test) ]
295
199
mod tests {
296
200
use prelude:: * ;
297
- use super :: all_values;
298
- use from_str:: FromStr ;
201
+ use super :: to_bit;
299
202
300
203
#[ test]
301
- fn test_bool ( ) {
204
+ fn test_to_bit ( ) {
205
+ assert_eq ! ( to_bit:: <u8 >( true ) , 1u8 ) ;
206
+ assert_eq ! ( to_bit:: <u8 >( false ) , 0u8 ) ;
207
+ }
208
+
209
+ #[ test]
210
+ fn test_eq ( ) {
302
211
assert_eq ! ( false . eq( & true ) , false ) ;
303
212
assert_eq ! ( false == false , true ) ;
304
213
assert_eq ! ( false != true , true ) ;
305
214
assert_eq ! ( false . ne( & false ) , false ) ;
215
+ }
306
216
217
+ #[ test]
218
+ fn test_bitand ( ) {
307
219
assert_eq ! ( false . bitand( & false ) , false ) ;
308
220
assert_eq ! ( true . bitand( & false ) , false ) ;
309
221
assert_eq ! ( false . bitand( & true ) , false ) ;
@@ -313,7 +225,10 @@ mod tests {
313
225
assert_eq ! ( true & false , false ) ;
314
226
assert_eq ! ( false & true , false ) ;
315
227
assert_eq ! ( true & true , true ) ;
228
+ }
316
229
230
+ #[ test]
231
+ fn test_bitor ( ) {
317
232
assert_eq ! ( false . bitor( & false ) , false ) ;
318
233
assert_eq ! ( true . bitor( & false ) , true ) ;
319
234
assert_eq ! ( false . bitor( & true ) , true ) ;
@@ -323,7 +238,10 @@ mod tests {
323
238
assert_eq ! ( true | false , true ) ;
324
239
assert_eq ! ( false | true , true ) ;
325
240
assert_eq ! ( true | true , true ) ;
241
+ }
326
242
243
+ #[ test]
244
+ fn test_bitxor ( ) {
327
245
assert_eq ! ( false . bitxor( & false ) , false ) ;
328
246
assert_eq ! ( true . bitxor( & false ) , true ) ;
329
247
assert_eq ! ( false . bitxor( & true ) , true ) ;
@@ -333,65 +251,29 @@ mod tests {
333
251
assert_eq ! ( true ^ false , true ) ;
334
252
assert_eq ! ( false ^ true , true ) ;
335
253
assert_eq ! ( true ^ true , false ) ;
254
+ }
336
255
256
+ #[ test]
257
+ fn test_not ( ) {
337
258
assert_eq ! ( !true , false ) ;
338
259
assert_eq ! ( !false , true ) ;
260
+ }
339
261
340
- assert_eq ! ( true . to_str( ) , ~"true ");
341
- assert_eq!(false.to_str(), ~" false ");
342
-
262
+ #[ test]
263
+ fn test_from_str ( ) {
343
264
assert_eq ! ( from_str:: <bool >( "true" ) , Some ( true ) ) ;
344
265
assert_eq ! ( from_str:: <bool >( "false" ) , Some ( false ) ) ;
345
266
assert_eq ! ( from_str:: <bool >( "not even a boolean" ) , None ) ;
346
-
347
- assert_eq!(true.and(true), true);
348
- assert_eq!(true.and(false), false);
349
- assert_eq!(false.and(true), false);
350
- assert_eq!(false.and(false), false);
351
-
352
- assert_eq!(true.or(true), true);
353
- assert_eq!(true.or(false), true);
354
- assert_eq!(false.or(true), true);
355
- assert_eq!(false.or(false), false);
356
-
357
- assert_eq!(true.xor(true), false);
358
- assert_eq!(true.xor(false), true);
359
- assert_eq!(false.xor(true), true);
360
- assert_eq!(false.xor(false), false);
361
-
362
- assert_eq!(true.implies(true), true);
363
- assert_eq!(true.implies(false), false);
364
- assert_eq!(false.implies(true), true);
365
- assert_eq!(false.implies(false), true);
366
-
367
- assert_eq!(true.to_bit::<u8>(), 1u8);
368
- assert_eq!(false.to_bit::<u8>(), 0u8);
369
267
}
370
268
371
269
#[ test]
372
- fn test_bool_from_str() {
373
- all_values(|v| {
374
- assert!(Some(v) == FromStr::from_str(v.to_str()))
375
- });
376
- }
377
-
378
- #[test]
379
- fn test_bool_to_str() {
270
+ fn test_to_str ( ) {
380
271
assert_eq ! ( false . to_str( ) , ~"false ");
381
272
assert_eq!(true.to_str(), ~" true " ) ;
382
273
}
383
274
384
275
#[ test]
385
- fn test_bool_to_bit ( ) {
386
- all_values ( |v| {
387
- assert_eq ! ( v. to_bit:: <u8 >( ) , if v { 1u8 } else { 0u8 } ) ;
388
- assert_eq ! ( v. to_bit:: <uint>( ) , if v { 1 u } else { 0 u } ) ;
389
- assert_eq ! ( v. to_bit:: <int>( ) , if v { 1 i } else { 0 i } ) ;
390
- } ) ;
391
- }
392
-
393
- #[ test]
394
- fn test_bool_ord ( ) {
276
+ fn test_ord ( ) {
395
277
assert ! ( true > false ) ;
396
278
assert ! ( !( false > true ) ) ;
397
279
@@ -410,7 +292,7 @@ mod tests {
410
292
}
411
293
412
294
#[ test]
413
- fn test_bool_totalord ( ) {
295
+ fn test_totalord ( ) {
414
296
assert_eq ! ( true . cmp( & true ) , Equal ) ;
415
297
assert_eq ! ( false . cmp( & false ) , Equal ) ;
416
298
assert_eq ! ( true . cmp( & false ) , Greater ) ;
0 commit comments