2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// 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-05-26 09:39:55 -05:00
|
|
|
/*!
|
|
|
|
|
|
|
|
The `bool` module contains useful code to help work with boolean values.
|
|
|
|
|
|
|
|
A quick summary:
|
|
|
|
|
|
|
|
## Trait implementations for `bool`
|
|
|
|
|
|
|
|
Implementations of the following traits:
|
|
|
|
|
|
|
|
* `FromStr`
|
|
|
|
* `Ord`
|
|
|
|
* `TotalOrd`
|
|
|
|
* `Eq`
|
|
|
|
|
|
|
|
## 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`.
|
|
|
|
|
|
|
|
Finally, some inquries into the nature of truth: `is_true` and `is_false`.
|
|
|
|
|
|
|
|
*/
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 07:33:41 -05:00
|
|
|
use cmp::{Eq, Ord, TotalOrd, Ordering};
|
2013-01-08 21:37:25 -06:00
|
|
|
use option::{None, Option, Some};
|
2013-03-06 12:05:59 -06:00
|
|
|
use from_str::FromStr;
|
2013-05-31 10:54:15 -05:00
|
|
|
use to_str::ToStr;
|
2013-03-06 12:05:59 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Negation of a boolean value.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::not(true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::not(false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn not(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Conjunction of two boolean values.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::and(true, false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::and(true, true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn and(a: bool, b: bool) -> bool { a && b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Disjunction of two boolean values.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::or(true, false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::or(false, false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn or(a: bool, b: bool) -> bool { a || b }
|
2012-03-10 20:01:01 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-05-26 09:39:55 -05:00
|
|
|
* An 'exclusive or' of two boolean values.
|
|
|
|
*
|
|
|
|
* 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::xor(true, false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::xor(true, true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn xor(a: bool, b: bool) -> bool { (a && !b) || (!a && b) }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Implication between two boolean values.
|
|
|
|
*
|
|
|
|
* Implication is often phrased as 'if a then b.'
|
|
|
|
*
|
|
|
|
* 'if a then b' is equivalent to `!a || b`.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::implies(true, true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::implies(true, false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn implies(a: bool, b: bool) -> bool { !a || b }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Is a given boolean value true?
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::is_true(true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::is_true(false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_true(v: bool) -> bool { v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Is a given boolean value false?
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::is_false(false)
|
2013-05-26 09:39:55 -05:00
|
|
|
* true
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-27 08:19:57 -05:00
|
|
|
* rusti> std::bool::is_false(true)
|
2013-05-26 09:39:55 -05:00
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_false(v: bool) -> bool { !v }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Parse a `bool` from a `str`.
|
|
|
|
*
|
|
|
|
* Yields an `Option<bool>`, because `str` may or may not actually be parseable.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-26 09:39:55 -05:00
|
|
|
* rusti> FromStr::from_str::<bool>("true")
|
|
|
|
* Some(true)
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-26 09:39:55 -05:00
|
|
|
* rusti> FromStr::from_str::<bool>("false")
|
|
|
|
* Some(false)
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-26 09:39:55 -05:00
|
|
|
* rusti> FromStr::from_str::<bool>("not even a boolean")
|
|
|
|
* None
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-06 12:05:59 -06:00
|
|
|
impl FromStr for bool {
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str(s: &str) -> Option<bool> {
|
2013-05-18 01:27:54 -05:00
|
|
|
match s {
|
|
|
|
"true" => Some(true),
|
|
|
|
"false" => Some(false),
|
|
|
|
_ => None,
|
2013-03-06 12:05:59 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Convert a `bool` to a `str`.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-31 10:54:15 -05:00
|
|
|
* rusti> true.to_str()
|
2013-05-26 09:39:55 -05:00
|
|
|
* "true"
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-31 10:54:15 -05:00
|
|
|
* rusti> false.to_str()
|
2013-05-26 09:39:55 -05:00
|
|
|
* "false"
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-05-31 10:54:15 -05:00
|
|
|
impl ToStr for bool {
|
|
|
|
#[inline(always)]
|
|
|
|
fn to_str(&self) -> ~str {
|
|
|
|
if *self { ~"true" } else { ~"false" }
|
|
|
|
}
|
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-05-26 09:39:55 -05:00
|
|
|
* Iterates over all truth values, passing them to the given block.
|
|
|
|
*
|
|
|
|
* There are no guarantees about the order values will be given.
|
|
|
|
*
|
|
|
|
* # Examples
|
|
|
|
* ~~~
|
2013-05-27 08:19:57 -05:00
|
|
|
* do std::bool::all_values |x: bool| {
|
2013-05-31 10:54:15 -05:00
|
|
|
* println(x.to_str())
|
2013-05-26 09:39:55 -05:00
|
|
|
* }
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-03-07 16:38:38 -06:00
|
|
|
pub fn all_values(blk: &fn(v: bool)) {
|
2011-12-13 18:25:51 -06:00
|
|
|
blk(true);
|
|
|
|
blk(false);
|
|
|
|
}
|
|
|
|
|
2013-05-26 09:39:55 -05:00
|
|
|
/**
|
|
|
|
* Convert a `bool` to a `u8`.
|
|
|
|
*
|
|
|
|
* # Examples
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-26 09:39:55 -05:00
|
|
|
* rusti> std::bool::to_bit(true)
|
|
|
|
* 1
|
|
|
|
* ~~~
|
2013-05-27 08:49:54 -05:00
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
2013-05-26 09:39:55 -05:00
|
|
|
* rusti> std::bool::to_bit(false)
|
|
|
|
* 0
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-04-11 07:33:41 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 07:33:41 -05:00
|
|
|
impl Ord for bool {
|
|
|
|
#[inline(always)]
|
|
|
|
fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
|
|
|
|
}
|
|
|
|
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 07:33:41 -05:00
|
|
|
impl TotalOrd for bool {
|
|
|
|
#[inline(always)]
|
|
|
|
fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
|
|
|
|
}
|
|
|
|
|
2013-05-31 10:54:15 -05:00
|
|
|
/**
|
|
|
|
* Equality between two boolean values.
|
|
|
|
*
|
|
|
|
* Two booleans are equal if they have the same value.
|
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
|
|
|
* rusti> false.eq(&true)
|
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
|
|
|
* rusti> false == false
|
|
|
|
* true
|
|
|
|
* ~~~
|
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
|
|
|
* rusti> false != true
|
|
|
|
* true
|
|
|
|
* ~~~
|
|
|
|
*
|
|
|
|
* ~~~ {.rust}
|
|
|
|
* rusti> false.ne(&false)
|
|
|
|
* false
|
|
|
|
* ~~~
|
|
|
|
*/
|
2013-05-08 06:11:23 -05:00
|
|
|
#[cfg(not(test))]
|
2013-04-11 07:33:41 -05:00
|
|
|
impl Eq for bool {
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
|
2013-04-11 07:33:41 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-04-11 07:33:41 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use prelude::*;
|
2013-03-06 12:05:59 -06:00
|
|
|
|
2013-04-11 07:33:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bool_from_str() {
|
|
|
|
do all_values |v| {
|
2013-05-31 10:54:15 -05:00
|
|
|
assert!(Some(v) == FromStr::from_str(v.to_str()))
|
2013-04-11 07:33:41 -05:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
2013-04-11 07:33:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bool_to_str() {
|
2013-05-31 10:54:15 -05:00
|
|
|
assert_eq!(false.to_str(), ~"false");
|
|
|
|
assert_eq!(true.to_str(), ~"true");
|
2013-04-11 07:33:41 -05:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
|
2013-04-11 07:33:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bool_to_bit() {
|
|
|
|
do all_values |v| {
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(to_bit(v), if is_true(v) { 1u8 } else { 0u8 });
|
2013-04-11 07:33:41 -05:00
|
|
|
}
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
2013-04-11 07:33:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_bool_ord() {
|
|
|
|
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]
|
|
|
|
fn test_bool_totalord() {
|
|
|
|
assert_eq!(true.cmp(&true), Equal);
|
|
|
|
assert_eq!(false.cmp(&false), Equal);
|
|
|
|
assert_eq!(true.cmp(&false), Greater);
|
|
|
|
assert_eq!(false.cmp(&true), Less);
|
|
|
|
}
|
|
|
|
}
|