core: Inherit the bool module

This commit is contained in:
Alex Crichton 2014-04-30 21:11:17 -07:00
parent 92095d125a
commit 6636215a44
4 changed files with 41 additions and 36 deletions

View File

@ -14,7 +14,6 @@
//!
//! Implementations of the following traits:
//!
//! * `FromStr`
//! * `Not`
//! * `Ord`
//! * `TotalOrd`
@ -24,11 +23,9 @@
//!
//! A `to_bit` conversion function.
use from_str::FromStr;
use num::{Int, one, zero};
use option::{None, Option, Some};
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering, TotalEq};
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
#[cfg(not(test))] use default::Default;
@ -55,28 +52,6 @@ pub fn to_bit<N: Int>(p: bool) -> N {
// Trait impls on `bool`
/////////////////////////////////////////////////////////////////////////////
impl FromStr for bool {
/// 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]
fn from_str(s: &str) -> Option<bool> {
match s {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
}
#[cfg(not(test))]
impl Not<bool> for bool {
/// The logical complement of a boolean value.
@ -190,6 +165,9 @@ impl Eq for bool {
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
}
#[cfg(not(test))]
impl TotalEq for bool {}
#[cfg(not(test))]
impl Default for bool {
fn default() -> bool { false }
@ -260,13 +238,6 @@ mod tests {
assert_eq!(!false, true);
}
#[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);
}
#[test]
fn test_to_str() {
assert_eq!(false.to_str(), "false".to_owned());

View File

@ -41,6 +41,7 @@ pub mod container;
mod unit;
pub mod any;
pub mod bool;
pub mod finally;
pub mod raw;
pub mod char;

View File

@ -10,7 +10,7 @@
//! The `FromStr` trait for types that can be created from strings
use option::Option;
use option::{Option, Some, None};
/// A trait to abstract the idea of creating a new instance of a type from a
/// string.
@ -24,3 +24,37 @@ pub trait FromStr {
pub fn from_str<A: FromStr>(s: &str) -> Option<A> {
FromStr::from_str(s)
}
impl FromStr for bool {
/// 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]
fn from_str(s: &str) -> Option<bool> {
match s {
"true" => Some(true),
"false" => Some(false),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use prelude::*;
#[test]
fn test_bool_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);
}
}

View File

@ -138,6 +138,7 @@ extern crate core;
#[cfg(not(test))] pub use ty = core::ty;
pub use core::any;
pub use core::bool;
pub use core::cast;
pub use core::char;
pub use core::clone;
@ -192,8 +193,6 @@ pub mod prelude;
#[path = "num/f32.rs"] pub mod f32;
#[path = "num/f64.rs"] pub mod f64;
pub mod bool;
pub mod slice;
pub mod vec;
pub mod str;