Skip to content

Commit 6636215

Browse files
committed
core: Inherit the bool module
1 parent 92095d1 commit 6636215

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

src/libstd/bool.rs renamed to src/libcore/bool.rs

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//!
1515
//! Implementations of the following traits:
1616
//!
17-
//! * `FromStr`
1817
//! * `Not`
1918
//! * `Ord`
2019
//! * `TotalOrd`
@@ -24,11 +23,9 @@
2423
//!
2524
//! A `to_bit` conversion function.
2625
27-
use from_str::FromStr;
2826
use num::{Int, one, zero};
29-
use option::{None, Option, Some};
3027

31-
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering};
28+
#[cfg(not(test))] use cmp::{Eq, Ord, TotalOrd, Ordering, TotalEq};
3229
#[cfg(not(test))] use ops::{Not, BitAnd, BitOr, BitXor};
3330
#[cfg(not(test))] use default::Default;
3431

@@ -55,28 +52,6 @@ pub fn to_bit<N: Int>(p: bool) -> N {
5552
// Trait impls on `bool`
5653
/////////////////////////////////////////////////////////////////////////////
5754

58-
impl FromStr for bool {
59-
/// Parse a `bool` from a string.
60-
///
61-
/// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
62-
///
63-
/// # Examples
64-
///
65-
/// ```rust
66-
/// assert_eq!(from_str::<bool>("true"), Some(true));
67-
/// assert_eq!(from_str::<bool>("false"), Some(false));
68-
/// assert_eq!(from_str::<bool>("not even a boolean"), None);
69-
/// ```
70-
#[inline]
71-
fn from_str(s: &str) -> Option<bool> {
72-
match s {
73-
"true" => Some(true),
74-
"false" => Some(false),
75-
_ => None,
76-
}
77-
}
78-
}
79-
8055
#[cfg(not(test))]
8156
impl Not<bool> for bool {
8257
/// The logical complement of a boolean value.
@@ -190,6 +165,9 @@ impl Eq for bool {
190165
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
191166
}
192167

168+
#[cfg(not(test))]
169+
impl TotalEq for bool {}
170+
193171
#[cfg(not(test))]
194172
impl Default for bool {
195173
fn default() -> bool { false }
@@ -260,13 +238,6 @@ mod tests {
260238
assert_eq!(!false, true);
261239
}
262240

263-
#[test]
264-
fn test_from_str() {
265-
assert_eq!(from_str::<bool>("true"), Some(true));
266-
assert_eq!(from_str::<bool>("false"), Some(false));
267-
assert_eq!(from_str::<bool>("not even a boolean"), None);
268-
}
269-
270241
#[test]
271242
fn test_to_str() {
272243
assert_eq!(false.to_str(), "false".to_owned());

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod container;
4141

4242
mod unit;
4343
pub mod any;
44+
pub mod bool;
4445
pub mod finally;
4546
pub mod raw;
4647
pub mod char;

src/libstd/from_str.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
//! The `FromStr` trait for types that can be created from strings
1212
13-
use option::Option;
13+
use option::{Option, Some, None};
1414

1515
/// A trait to abstract the idea of creating a new instance of a type from a
1616
/// string.
@@ -24,3 +24,37 @@ pub trait FromStr {
2424
pub fn from_str<A: FromStr>(s: &str) -> Option<A> {
2525
FromStr::from_str(s)
2626
}
27+
28+
impl FromStr for bool {
29+
/// Parse a `bool` from a string.
30+
///
31+
/// Yields an `Option<bool>`, because `s` may or may not actually be parseable.
32+
///
33+
/// # Examples
34+
///
35+
/// ```rust
36+
/// assert_eq!(from_str::<bool>("true"), Some(true));
37+
/// assert_eq!(from_str::<bool>("false"), Some(false));
38+
/// assert_eq!(from_str::<bool>("not even a boolean"), None);
39+
/// ```
40+
#[inline]
41+
fn from_str(s: &str) -> Option<bool> {
42+
match s {
43+
"true" => Some(true),
44+
"false" => Some(false),
45+
_ => None,
46+
}
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod test {
52+
use prelude::*;
53+
54+
#[test]
55+
fn test_bool_from_str() {
56+
assert_eq!(from_str::<bool>("true"), Some(true));
57+
assert_eq!(from_str::<bool>("false"), Some(false));
58+
assert_eq!(from_str::<bool>("not even a boolean"), None);
59+
}
60+
}

src/libstd/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ extern crate core;
138138
#[cfg(not(test))] pub use ty = core::ty;
139139

140140
pub use core::any;
141+
pub use core::bool;
141142
pub use core::cast;
142143
pub use core::char;
143144
pub use core::clone;
@@ -192,8 +193,6 @@ pub mod prelude;
192193
#[path = "num/f32.rs"] pub mod f32;
193194
#[path = "num/f64.rs"] pub mod f64;
194195

195-
pub mod bool;
196-
197196
pub mod slice;
198197
pub mod vec;
199198
pub mod str;

0 commit comments

Comments
 (0)