Skip to content

Commit 27cc309

Browse files
committed
auto merge of #12473 : bjz/rust/remove-bool, r=alexcrichton
These were never used outside of the tests.
2 parents 4243cad + 6efa3c6 commit 27cc309

File tree

2 files changed

+43
-162
lines changed

2 files changed

+43
-162
lines changed

src/libstd/bool.rs

+43-161
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
//!
1313
//! A quick summary:
1414
//!
15-
//! ## Trait implementations for `bool`
16-
//!
1715
//! Implementations of the following traits:
1816
//!
1917
//! * `FromStr`
@@ -24,16 +22,11 @@
2422
//! * `Default`
2523
//! * `Zero`
2624
//!
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.
3326
34-
use option::{None, Option, Some};
3527
use from_str::FromStr;
36-
use num::FromPrimitive;
28+
use num::{Int, one, zero};
29+
use option::{None, Option, Some};
3730

3831
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
3932
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
@@ -43,112 +36,19 @@ use num::FromPrimitive;
4336
// Freestanding functions
4437
/////////////////////////////////////////////////////////////////////////////
4538

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.
4940
///
5041
/// # Examples
5142
///
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);
5648
/// ```
5749
#[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() }
15252
}
15353

15454
/////////////////////////////////////////////////////////////////////////////
@@ -259,13 +159,17 @@ impl BitXor<bool, bool> for bool {
259159
#[cfg(not(test))]
260160
impl Ord for bool {
261161
#[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+
}
263165
}
264166

265167
#[cfg(not(test))]
266168
impl TotalOrd for bool {
267169
#[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+
}
269173
}
270174

271175
/// Equality between two boolean values.
@@ -294,16 +198,24 @@ impl Default for bool {
294198
#[cfg(test)]
295199
mod tests {
296200
use prelude::*;
297-
use super::all_values;
298-
use from_str::FromStr;
201+
use super::to_bit;
299202

300203
#[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() {
302211
assert_eq!(false.eq(&true), false);
303212
assert_eq!(false == false, true);
304213
assert_eq!(false != true, true);
305214
assert_eq!(false.ne(&false), false);
215+
}
306216

217+
#[test]
218+
fn test_bitand() {
307219
assert_eq!(false.bitand(&false), false);
308220
assert_eq!(true.bitand(&false), false);
309221
assert_eq!(false.bitand(&true), false);
@@ -313,7 +225,10 @@ mod tests {
313225
assert_eq!(true & false, false);
314226
assert_eq!(false & true, false);
315227
assert_eq!(true & true, true);
228+
}
316229

230+
#[test]
231+
fn test_bitor() {
317232
assert_eq!(false.bitor(&false), false);
318233
assert_eq!(true.bitor(&false), true);
319234
assert_eq!(false.bitor(&true), true);
@@ -323,7 +238,10 @@ mod tests {
323238
assert_eq!(true | false, true);
324239
assert_eq!(false | true, true);
325240
assert_eq!(true | true, true);
241+
}
326242

243+
#[test]
244+
fn test_bitxor() {
327245
assert_eq!(false.bitxor(&false), false);
328246
assert_eq!(true.bitxor(&false), true);
329247
assert_eq!(false.bitxor(&true), true);
@@ -333,65 +251,29 @@ mod tests {
333251
assert_eq!(true ^ false, true);
334252
assert_eq!(false ^ true, true);
335253
assert_eq!(true ^ true, false);
254+
}
336255

256+
#[test]
257+
fn test_not() {
337258
assert_eq!(!true, false);
338259
assert_eq!(!false, true);
260+
}
339261

340-
assert_eq!(true.to_str(), ~"true");
341-
assert_eq!(false.to_str(), ~"false");
342-
262+
#[test]
263+
fn test_from_str() {
343264
assert_eq!(from_str::<bool>("true"), Some(true));
344265
assert_eq!(from_str::<bool>("false"), Some(false));
345266
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);
369267
}
370268

371269
#[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() {
380271
assert_eq!(false.to_str(), ~"false");
381272
assert_eq!(true.to_str(), ~"true");
382273
}
383274

384275
#[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 { 1u } else { 0u });
389-
assert_eq!(v.to_bit::<int>(), if v { 1i } else { 0i });
390-
});
391-
}
392-
393-
#[test]
394-
fn test_bool_ord() {
276+
fn test_ord() {
395277
assert!(true > false);
396278
assert!(!(false > true));
397279

@@ -410,7 +292,7 @@ mod tests {
410292
}
411293

412294
#[test]
413-
fn test_bool_totalord() {
295+
fn test_totalord() {
414296
assert_eq!(true.cmp(&true), Equal);
415297
assert_eq!(false.cmp(&false), Equal);
416298
assert_eq!(true.cmp(&false), Greater);

src/libstd/prelude.rs

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub use mem::drop;
3737

3838
pub use any::{Any, AnyOwnExt, AnyRefExt, AnyMutRefExt};
3939
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, IntoBytes};
40-
pub use bool::Bool;
4140
pub use c_str::ToCStr;
4241
pub use char::Char;
4342
pub use clone::{Clone, DeepClone};

0 commit comments

Comments
 (0)