auto merge of #12473 : bjz/rust/remove-bool, r=alexcrichton
These were never used outside of the tests.
This commit is contained in:
commit
27cc309a7f
2 changed files with 43 additions and 162 deletions
|
@ -12,8 +12,6 @@
|
|||
//!
|
||||
//! A quick summary:
|
||||
//!
|
||||
//! ## Trait implementations for `bool`
|
||||
//!
|
||||
//! Implementations of the following traits:
|
||||
//!
|
||||
//! * `FromStr`
|
||||
|
@ -24,16 +22,11 @@
|
|||
//! * `Default`
|
||||
//! * `Zero`
|
||||
//!
|
||||
//! ## Various functions to compare `bool`s
|
||||
//!
|
||||
//! All of the standard comparison functions one would expect: `and`, `eq`, `or`,
|
||||
//! and more.
|
||||
//!
|
||||
//! Also, a few conversion functions: `to_bit` and `to_str`.
|
||||
//! A `to_bit` conversion function.
|
||||
|
||||
use option::{None, Option, Some};
|
||||
use from_str::FromStr;
|
||||
use num::FromPrimitive;
|
||||
use num::{Int, one, zero};
|
||||
use option::{None, Option, Some};
|
||||
|
||||
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
|
||||
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
|
||||
|
@ -43,112 +36,19 @@ use num::FromPrimitive;
|
|||
// Freestanding functions
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Iterates over all truth values, passing them to the given block.
|
||||
///
|
||||
/// There are no guarantees about the order values will be given.
|
||||
/// Convert a `bool` to an integer.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// std::bool::all_values(|x: bool| {
|
||||
/// println!("{}", x);
|
||||
/// })
|
||||
/// ```rust
|
||||
/// use std::bool;
|
||||
///
|
||||
/// assert_eq!(bool::to_bit::<u8>(true), 1u8);
|
||||
/// assert_eq!(bool::to_bit::<u8>(false), 0u8);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn all_values(blk: |v: bool|) {
|
||||
blk(true);
|
||||
blk(false);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Methods on `bool`
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/// Extension methods on a `bool`
|
||||
pub trait Bool {
|
||||
/// Conjunction of two boolean values.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// assert_eq!(true.and(true), true);
|
||||
/// assert_eq!(true.and(false), false);
|
||||
/// assert_eq!(false.and(true), false);
|
||||
/// assert_eq!(false.and(false), false);
|
||||
/// ```
|
||||
fn and(self, b: bool) -> bool;
|
||||
|
||||
/// Disjunction of two boolean values.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// assert_eq!(true.or(true), true);
|
||||
/// assert_eq!(true.or(false), true);
|
||||
/// assert_eq!(false.or(true), true);
|
||||
/// assert_eq!(false.or(false), false);
|
||||
/// ```
|
||||
fn or(self, b: bool) -> bool;
|
||||
|
||||
/// An 'exclusive or' of two boolean values.
|
||||
///
|
||||
/// 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// assert_eq!(true.xor(true), false);
|
||||
/// assert_eq!(true.xor(false), true);
|
||||
/// assert_eq!(false.xor(true), true);
|
||||
/// assert_eq!(false.xor(false), false);
|
||||
/// ```
|
||||
fn xor(self, b: bool) -> bool;
|
||||
|
||||
/// Implication between two boolean values.
|
||||
///
|
||||
/// Implication is often phrased as 'if a then b.'
|
||||
///
|
||||
/// 'if a then b' is equivalent to `!a || b`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// assert_eq!(true.implies(true), true);
|
||||
/// assert_eq!(true.implies(false), false);
|
||||
/// assert_eq!(false.implies(true), true);
|
||||
/// assert_eq!(false.implies(false), true);
|
||||
/// ```
|
||||
fn implies(self, b: bool) -> bool;
|
||||
|
||||
/// Convert a `bool` to a `u8`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// assert_eq!(true.to_bit::<u8>(), 1u8);
|
||||
/// assert_eq!(false.to_bit::<u8>(), 0u8);
|
||||
/// ```
|
||||
fn to_bit<N: FromPrimitive>(self) -> N;
|
||||
}
|
||||
|
||||
impl Bool for bool {
|
||||
#[inline]
|
||||
fn and(self, b: bool) -> bool { self && b }
|
||||
|
||||
#[inline]
|
||||
fn or(self, b: bool) -> bool { self || b }
|
||||
|
||||
#[inline]
|
||||
fn xor(self, b: bool) -> bool { self ^ b }
|
||||
|
||||
#[inline]
|
||||
fn implies(self, b: bool) -> bool { !self || b }
|
||||
|
||||
#[inline]
|
||||
fn to_bit<N: FromPrimitive>(self) -> N {
|
||||
if self { FromPrimitive::from_u8(1).unwrap() }
|
||||
else { FromPrimitive::from_u8(0).unwrap() }
|
||||
}
|
||||
pub fn to_bit<N: Int>(p: bool) -> N {
|
||||
if p { one() } else { zero() }
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -259,13 +159,17 @@ impl BitXor<bool, bool> for bool {
|
|||
#[cfg(not(test))]
|
||||
impl Ord for bool {
|
||||
#[inline]
|
||||
fn lt(&self, other: &bool) -> bool { self.to_bit::<u8>() < other.to_bit() }
|
||||
fn lt(&self, other: &bool) -> bool {
|
||||
to_bit::<u8>(*self) < to_bit(*other)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
impl TotalOrd for bool {
|
||||
#[inline]
|
||||
fn cmp(&self, other: &bool) -> Ordering { self.to_bit::<u8>().cmp(&other.to_bit()) }
|
||||
fn cmp(&self, other: &bool) -> Ordering {
|
||||
to_bit::<u8>(*self).cmp(&to_bit(*other))
|
||||
}
|
||||
}
|
||||
|
||||
/// Equality between two boolean values.
|
||||
|
@ -294,16 +198,24 @@ impl Default for bool {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use prelude::*;
|
||||
use super::all_values;
|
||||
use from_str::FromStr;
|
||||
use super::to_bit;
|
||||
|
||||
#[test]
|
||||
fn test_bool() {
|
||||
fn test_to_bit() {
|
||||
assert_eq!(to_bit::<u8>(true), 1u8);
|
||||
assert_eq!(to_bit::<u8>(false), 0u8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eq() {
|
||||
assert_eq!(false.eq(&true), false);
|
||||
assert_eq!(false == false, true);
|
||||
assert_eq!(false != true, true);
|
||||
assert_eq!(false.ne(&false), false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitand() {
|
||||
assert_eq!(false.bitand(&false), false);
|
||||
assert_eq!(true.bitand(&false), false);
|
||||
assert_eq!(false.bitand(&true), false);
|
||||
|
@ -313,7 +225,10 @@ mod tests {
|
|||
assert_eq!(true & false, false);
|
||||
assert_eq!(false & true, false);
|
||||
assert_eq!(true & true, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitor() {
|
||||
assert_eq!(false.bitor(&false), false);
|
||||
assert_eq!(true.bitor(&false), true);
|
||||
assert_eq!(false.bitor(&true), true);
|
||||
|
@ -323,7 +238,10 @@ mod tests {
|
|||
assert_eq!(true | false, true);
|
||||
assert_eq!(false | true, true);
|
||||
assert_eq!(true | true, true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitxor() {
|
||||
assert_eq!(false.bitxor(&false), false);
|
||||
assert_eq!(true.bitxor(&false), true);
|
||||
assert_eq!(false.bitxor(&true), true);
|
||||
|
@ -333,65 +251,29 @@ mod tests {
|
|||
assert_eq!(true ^ false, true);
|
||||
assert_eq!(false ^ true, true);
|
||||
assert_eq!(true ^ true, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_not() {
|
||||
assert_eq!(!true, false);
|
||||
assert_eq!(!false, true);
|
||||
}
|
||||
|
||||
assert_eq!(true.to_str(), ~"true");
|
||||
assert_eq!(false.to_str(), ~"false");
|
||||
|
||||
#[test]
|
||||
fn test_from_str() {
|
||||
assert_eq!(from_str::<bool>("true"), Some(true));
|
||||
assert_eq!(from_str::<bool>("false"), Some(false));
|
||||
assert_eq!(from_str::<bool>("not even a boolean"), None);
|
||||
|
||||
assert_eq!(true.and(true), true);
|
||||
assert_eq!(true.and(false), false);
|
||||
assert_eq!(false.and(true), false);
|
||||
assert_eq!(false.and(false), false);
|
||||
|
||||
assert_eq!(true.or(true), true);
|
||||
assert_eq!(true.or(false), true);
|
||||
assert_eq!(false.or(true), true);
|
||||
assert_eq!(false.or(false), false);
|
||||
|
||||
assert_eq!(true.xor(true), false);
|
||||
assert_eq!(true.xor(false), true);
|
||||
assert_eq!(false.xor(true), true);
|
||||
assert_eq!(false.xor(false), false);
|
||||
|
||||
assert_eq!(true.implies(true), true);
|
||||
assert_eq!(true.implies(false), false);
|
||||
assert_eq!(false.implies(true), true);
|
||||
assert_eq!(false.implies(false), true);
|
||||
|
||||
assert_eq!(true.to_bit::<u8>(), 1u8);
|
||||
assert_eq!(false.to_bit::<u8>(), 0u8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_from_str() {
|
||||
all_values(|v| {
|
||||
assert!(Some(v) == FromStr::from_str(v.to_str()))
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_to_str() {
|
||||
fn test_to_str() {
|
||||
assert_eq!(false.to_str(), ~"false");
|
||||
assert_eq!(true.to_str(), ~"true");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_to_bit() {
|
||||
all_values(|v| {
|
||||
assert_eq!(v.to_bit::<u8>(), if v { 1u8 } else { 0u8 });
|
||||
assert_eq!(v.to_bit::<uint>(), if v { 1u } else { 0u });
|
||||
assert_eq!(v.to_bit::<int>(), if v { 1i } else { 0i });
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_ord() {
|
||||
fn test_ord() {
|
||||
assert!(true > false);
|
||||
assert!(!(false > true));
|
||||
|
||||
|
@ -410,7 +292,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_bool_totalord() {
|
||||
fn test_totalord() {
|
||||
assert_eq!(true.cmp(&true), Equal);
|
||||
assert_eq!(false.cmp(&false), Equal);
|
||||
assert_eq!(true.cmp(&false), Greater);
|
||||
|
|
|
@ -37,7 +37,6 @@ pub use mem::drop;
|
|||
|
||||
pub use any::{Any, AnyOwnExt, AnyRefExt, AnyMutRefExt};
|
||||
pub use ascii::{Ascii, AsciiCast, OwnedAsciiCast, AsciiStr, IntoBytes};
|
||||
pub use bool::Bool;
|
||||
pub use c_str::ToCStr;
|
||||
pub use char::Char;
|
||||
pub use clone::{Clone, DeepClone};
|
||||
|
|
Loading…
Add table
Reference in a new issue