2013-10-21 21:41:32 +02:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-12-24 17:08:28 +01:00
|
|
|
//! Operations on boolean values (`bool` type)
|
2013-10-21 21:41:32 +02:00
|
|
|
//!
|
|
|
|
//! A quick summary:
|
|
|
|
//!
|
|
|
|
//! Implementations of the following traits:
|
|
|
|
//!
|
|
|
|
//! * `FromStr`
|
|
|
|
//! * `Not`
|
|
|
|
//! * `Ord`
|
|
|
|
//! * `TotalOrd`
|
|
|
|
//! * `Eq`
|
|
|
|
//! * `Default`
|
|
|
|
//! * `Zero`
|
|
|
|
//!
|
2014-02-23 06:41:38 +11:00
|
|
|
//! A `to_bit` conversion function.
|
2013-05-26 09:39:55 -05:00
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
use from_str::FromStr;
|
2014-02-23 06:41:38 +11:00
|
|
|
use num::{Int, one, zero};
|
|
|
|
use option::{None, Option, Some};
|
2013-05-26 09:39:55 -05:00
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
|
|
|
|
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
|
|
|
|
#[cfg(not(test))] use default::Default;
|
2013-05-26 09:39:55 -05:00
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Freestanding functions
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
/// Convert a `bool` to an integer.
|
2013-10-21 21:41:32 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
2014-02-23 06:41:38 +11:00
|
|
|
/// ```rust
|
|
|
|
/// use std::bool;
|
|
|
|
///
|
|
|
|
/// assert_eq!(bool::to_bit::<u8>(true), 1u8);
|
|
|
|
/// assert_eq!(bool::to_bit::<u8>(false), 0u8);
|
2013-10-21 21:41:32 +02:00
|
|
|
/// ```
|
|
|
|
#[inline]
|
2014-02-23 06:41:38 +11:00
|
|
|
pub fn to_bit<N: Int>(p: bool) -> N {
|
|
|
|
if p { one() } else { zero() }
|
2013-10-21 21:41:32 +02:00
|
|
|
}
|
2013-03-06 13:05:59 -05:00
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Trait impls on `bool`
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
2013-07-24 22:37:48 +03:00
|
|
|
|
2013-03-06 13:05:59 -05:00
|
|
|
impl FromStr for bool {
|
2013-10-21 21:41:32 +02:00
|
|
|
/// Parse a `bool` from a string.
|
|
|
|
///
|
|
|
|
/// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// 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);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn from_str(s: &str) -> Option<bool> {
|
2013-05-18 16:27:54 +10:00
|
|
|
match s {
|
|
|
|
"true" => Some(true),
|
|
|
|
"false" => Some(false),
|
|
|
|
_ => None,
|
2013-03-06 13:05:59 -05:00
|
|
|
}
|
2011-12-13 16:25:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 22:32:02 +10:00
|
|
|
#[cfg(not(test))]
|
|
|
|
impl Not<bool> for bool {
|
2013-10-21 21:41:32 +02:00
|
|
|
/// The logical complement of a boolean value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// assert_eq!(!true, false);
|
|
|
|
/// assert_eq!(!false, true);
|
|
|
|
/// ```
|
2013-07-07 22:32:02 +10:00
|
|
|
#[inline]
|
|
|
|
fn not(&self) -> bool { !*self }
|
|
|
|
}
|
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
#[cfg(not(test))]
|
|
|
|
impl BitAnd<bool, bool> for bool {
|
|
|
|
/// Conjunction of two boolean values.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// assert_eq!(false.bitand(&false), false);
|
|
|
|
/// assert_eq!(true.bitand(&false), false);
|
|
|
|
/// assert_eq!(false.bitand(&true), false);
|
|
|
|
/// assert_eq!(true.bitand(&true), true);
|
|
|
|
///
|
|
|
|
/// assert_eq!(false & false, false);
|
|
|
|
/// assert_eq!(true & false, false);
|
|
|
|
/// assert_eq!(false & true, false);
|
|
|
|
/// assert_eq!(true & true, true);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
fn bitand(&self, b: &bool) -> bool { *self & *b }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
impl BitOr<bool, bool> for bool {
|
|
|
|
/// Disjunction of two boolean values.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// assert_eq!(false.bitor(&false), false);
|
|
|
|
/// assert_eq!(true.bitor(&false), true);
|
|
|
|
/// assert_eq!(false.bitor(&true), true);
|
|
|
|
/// assert_eq!(true.bitor(&true), true);
|
|
|
|
///
|
|
|
|
/// assert_eq!(false | false, false);
|
|
|
|
/// assert_eq!(true | false, true);
|
|
|
|
/// assert_eq!(false | true, true);
|
|
|
|
/// assert_eq!(true | true, true);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
fn bitor(&self, b: &bool) -> bool { *self | *b }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(test))]
|
|
|
|
impl BitXor<bool, bool> for 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!(false.bitxor(&false), false);
|
|
|
|
/// assert_eq!(true.bitxor(&false), true);
|
|
|
|
/// assert_eq!(false.bitxor(&true), true);
|
|
|
|
/// assert_eq!(true.bitxor(&true), false);
|
|
|
|
///
|
|
|
|
/// assert_eq!(false ^ false, false);
|
|
|
|
/// assert_eq!(true ^ false, true);
|
|
|
|
/// assert_eq!(false ^ true, true);
|
|
|
|
/// assert_eq!(true ^ true, false);
|
|
|
|
/// ```
|
|
|
|
#[inline]
|
|
|
|
fn bitxor(&self, b: &bool) -> bool { *self ^ *b }
|
|
|
|
}
|
|
|
|
|
2013-05-08 21:11:23 +10:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 08:33:41 -04:00
|
|
|
impl Ord for bool {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn lt(&self, other: &bool) -> bool {
|
|
|
|
to_bit::<u8>(*self) < to_bit(*other)
|
|
|
|
}
|
2013-04-11 08:33:41 -04:00
|
|
|
}
|
|
|
|
|
2013-05-08 21:11:23 +10:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 08:33:41 -04:00
|
|
|
impl TotalOrd for bool {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn cmp(&self, other: &bool) -> Ordering {
|
|
|
|
to_bit::<u8>(*self).cmp(&to_bit(*other))
|
|
|
|
}
|
2013-04-11 08:33:41 -04:00
|
|
|
}
|
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
/// Equality between two boolean values.
|
|
|
|
///
|
|
|
|
/// Two booleans are equal if they have the same value.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// assert_eq!(false.eq(&true), false);
|
|
|
|
/// assert_eq!(false == false, true);
|
|
|
|
/// assert_eq!(false != true, true);
|
|
|
|
/// assert_eq!(false.ne(&false), false);
|
|
|
|
/// ```
|
2013-05-08 21:11:23 +10:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 08:33:41 -04:00
|
|
|
impl Eq for bool {
|
2013-06-18 14:45:18 -07:00
|
|
|
#[inline]
|
2013-03-21 21:20:48 -07:00
|
|
|
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
|
2012-09-19 18:00:26 -07:00
|
|
|
}
|
2012-08-27 16:26:35 -07:00
|
|
|
|
2013-09-11 21:49:25 -07:00
|
|
|
#[cfg(not(test))]
|
|
|
|
impl Default for bool {
|
|
|
|
fn default() -> bool { false }
|
|
|
|
}
|
|
|
|
|
2013-04-11 08:33:41 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2014-01-06 22:33:37 -08:00
|
|
|
use prelude::*;
|
2014-02-23 06:41:38 +11:00
|
|
|
use super::to_bit;
|
2013-03-06 13:05:59 -05:00
|
|
|
|
2013-10-21 21:41:32 +02:00
|
|
|
#[test]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn test_to_bit() {
|
|
|
|
assert_eq!(to_bit::<u8>(true), 1u8);
|
|
|
|
assert_eq!(to_bit::<u8>(false), 0u8);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2013-10-21 21:41:32 +02:00
|
|
|
assert_eq!(false.eq(&true), false);
|
|
|
|
assert_eq!(false == false, true);
|
|
|
|
assert_eq!(false != true, true);
|
|
|
|
assert_eq!(false.ne(&false), false);
|
2014-02-23 06:41:38 +11:00
|
|
|
}
|
2013-10-21 21:41:32 +02:00
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
#[test]
|
|
|
|
fn test_bitand() {
|
2013-10-21 21:41:32 +02:00
|
|
|
assert_eq!(false.bitand(&false), false);
|
|
|
|
assert_eq!(true.bitand(&false), false);
|
|
|
|
assert_eq!(false.bitand(&true), false);
|
|
|
|
assert_eq!(true.bitand(&true), true);
|
|
|
|
|
|
|
|
assert_eq!(false & false, false);
|
|
|
|
assert_eq!(true & false, false);
|
|
|
|
assert_eq!(false & true, false);
|
|
|
|
assert_eq!(true & true, true);
|
2014-02-23 06:41:38 +11:00
|
|
|
}
|
2013-10-21 21:41:32 +02:00
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
#[test]
|
|
|
|
fn test_bitor() {
|
2013-10-21 21:41:32 +02:00
|
|
|
assert_eq!(false.bitor(&false), false);
|
|
|
|
assert_eq!(true.bitor(&false), true);
|
|
|
|
assert_eq!(false.bitor(&true), true);
|
|
|
|
assert_eq!(true.bitor(&true), true);
|
|
|
|
|
|
|
|
assert_eq!(false | false, false);
|
|
|
|
assert_eq!(true | false, true);
|
|
|
|
assert_eq!(false | true, true);
|
|
|
|
assert_eq!(true | true, true);
|
2014-02-23 06:41:38 +11:00
|
|
|
}
|
2013-10-21 21:41:32 +02:00
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
#[test]
|
|
|
|
fn test_bitxor() {
|
2013-10-21 21:41:32 +02:00
|
|
|
assert_eq!(false.bitxor(&false), false);
|
|
|
|
assert_eq!(true.bitxor(&false), true);
|
|
|
|
assert_eq!(false.bitxor(&true), true);
|
|
|
|
assert_eq!(true.bitxor(&true), false);
|
|
|
|
|
|
|
|
assert_eq!(false ^ false, false);
|
|
|
|
assert_eq!(true ^ false, true);
|
|
|
|
assert_eq!(false ^ true, true);
|
|
|
|
assert_eq!(true ^ true, false);
|
2014-02-23 06:41:38 +11:00
|
|
|
}
|
2013-10-21 21:41:32 +02:00
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
#[test]
|
|
|
|
fn test_not() {
|
2013-10-21 21:41:32 +02:00
|
|
|
assert_eq!(!true, false);
|
|
|
|
assert_eq!(!false, true);
|
2014-02-23 06:41:38 +11:00
|
|
|
}
|
2013-10-21 21:41:32 +02:00
|
|
|
|
2014-02-23 06:41:38 +11:00
|
|
|
#[test]
|
|
|
|
fn test_from_str() {
|
2013-10-21 21:41:32 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2013-04-11 08:33:41 -04:00
|
|
|
#[test]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn test_to_str() {
|
2013-05-31 11:54:15 -04:00
|
|
|
assert_eq!(false.to_str(), ~"false");
|
|
|
|
assert_eq!(true.to_str(), ~"true");
|
2013-04-11 08:33:41 -04:00
|
|
|
}
|
2012-01-17 17:28:21 -08:00
|
|
|
|
2013-04-11 08:33:41 -04:00
|
|
|
#[test]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn test_ord() {
|
2013-04-11 08:33:41 -04:00
|
|
|
assert!(true > false);
|
|
|
|
assert!(!(false > true));
|
|
|
|
|
|
|
|
assert!(false < true);
|
|
|
|
assert!(!(true < false));
|
|
|
|
|
|
|
|
assert!(false <= false);
|
|
|
|
assert!(false >= false);
|
|
|
|
assert!(true <= true);
|
|
|
|
assert!(true >= true);
|
|
|
|
|
|
|
|
assert!(false <= true);
|
|
|
|
assert!(!(false >= true));
|
|
|
|
assert!(true >= false);
|
|
|
|
assert!(!(true <= false));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-02-23 06:41:38 +11:00
|
|
|
fn test_totalord() {
|
2014-02-28 01:23:06 -08:00
|
|
|
assert!(true.cmp(&true) == Equal);
|
|
|
|
assert!(false.cmp(&false) == Equal);
|
|
|
|
assert!(true.cmp(&false) == Greater);
|
|
|
|
assert!(false.cmp(&true) == Less);
|
2013-04-11 08:33:41 -04:00
|
|
|
}
|
|
|
|
}
|