From 6efa3c63d0be329b466223843bafa2a299f7d485 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Sun, 23 Feb 2014 06:41:38 +1100 Subject: [PATCH] Remove std::bool::{Bool, all_values} These were never used outside of the tests --- src/libstd/bool.rs | 204 +++++++++--------------------------------- src/libstd/prelude.rs | 1 - 2 files changed, 43 insertions(+), 162 deletions(-) diff --git a/src/libstd/bool.rs b/src/libstd/bool.rs index 2376603fcc8..f0835fe1102 100644 --- a/src/libstd/bool.rs +++ b/src/libstd/bool.rs @@ -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::(true), 1u8); +/// assert_eq!(bool::to_bit::(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::(), 1u8); - /// assert_eq!(false.to_bit::(), 0u8); - /// ``` - fn to_bit(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(self) -> N { - if self { FromPrimitive::from_u8(1).unwrap() } - else { FromPrimitive::from_u8(0).unwrap() } - } +pub fn to_bit(p: bool) -> N { + if p { one() } else { zero() } } ///////////////////////////////////////////////////////////////////////////// @@ -259,13 +159,17 @@ impl BitXor for bool { #[cfg(not(test))] impl Ord for bool { #[inline] - fn lt(&self, other: &bool) -> bool { self.to_bit::() < other.to_bit() } + fn lt(&self, other: &bool) -> bool { + to_bit::(*self) < to_bit(*other) + } } #[cfg(not(test))] impl TotalOrd for bool { #[inline] - fn cmp(&self, other: &bool) -> Ordering { self.to_bit::().cmp(&other.to_bit()) } + fn cmp(&self, other: &bool) -> Ordering { + to_bit::(*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::(true), 1u8); + assert_eq!(to_bit::(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::("true"), Some(true)); assert_eq!(from_str::("false"), Some(false)); assert_eq!(from_str::("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::(), 1u8); - assert_eq!(false.to_bit::(), 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::(), if v { 1u8 } else { 0u8 }); - assert_eq!(v.to_bit::(), if v { 1u } else { 0u }); - assert_eq!(v.to_bit::(), 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); diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index cc1a14a4f82..5ea00c2f67d 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -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};