Skip to content

Commit b3b8c05

Browse files
committed
auto merge of #5845 : thestinger/rust/bool, r=catamorphism
This is mostly just to make deriving more convenient, which is probably why Haskell does this too.
2 parents bfeb6d1 + 61b2999 commit b3b8c05

File tree

1 file changed

+72
-27
lines changed

1 file changed

+72
-27
lines changed

src/libcore/bool.rs

+72-27
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
1211
//! Boolean logic
1312
13+
#[cfg(notest)]
14+
use cmp::{Eq, Ord, TotalOrd, Ordering};
1415
use option::{None, Option, Some};
1516
use from_str::FromStr;
1617

17-
#[cfg(notest)] use cmp;
18-
1918
/// Negation / inverse
2019
pub fn not(v: bool) -> bool { !v }
2120

@@ -73,40 +72,86 @@ pub fn all_values(blk: &fn(v: bool)) {
7372
}
7473

7574
/// converts truth value to an 8 bit byte
75+
#[inline(always)]
7676
pub fn to_bit(v: bool) -> u8 { if v { 1u8 } else { 0u8 } }
7777

7878
#[cfg(notest)]
79-
impl cmp::Eq for bool {
79+
impl Ord for bool {
80+
#[inline(always)]
81+
fn lt(&self, other: &bool) -> bool { to_bit(*self) < to_bit(*other) }
82+
#[inline(always)]
83+
fn le(&self, other: &bool) -> bool { to_bit(*self) <= to_bit(*other) }
84+
#[inline(always)]
85+
fn gt(&self, other: &bool) -> bool { to_bit(*self) > to_bit(*other) }
86+
#[inline(always)]
87+
fn ge(&self, other: &bool) -> bool { to_bit(*self) >= to_bit(*other) }
88+
}
89+
90+
#[cfg(notest)]
91+
impl TotalOrd for bool {
92+
#[inline(always)]
93+
fn cmp(&self, other: &bool) -> Ordering { to_bit(*self).cmp(&to_bit(*other)) }
94+
}
95+
96+
#[cfg(notest)]
97+
impl Eq for bool {
98+
#[inline(always)]
8099
fn eq(&self, other: &bool) -> bool { (*self) == (*other) }
100+
#[inline(always)]
81101
fn ne(&self, other: &bool) -> bool { (*self) != (*other) }
82102
}
83103

84-
#[test]
85-
pub fn test_bool_from_str() {
86-
use from_str::FromStr;
104+
#[cfg(test)]
105+
mod tests {
106+
use super::*;
107+
use prelude::*;
87108

88-
do all_values |v| {
89-
assert!(Some(v) == FromStr::from_str(to_str(v)))
109+
#[test]
110+
fn test_bool_from_str() {
111+
use from_str::FromStr;
112+
113+
do all_values |v| {
114+
assert!(Some(v) == FromStr::from_str(to_str(v)))
115+
}
90116
}
91-
}
92117

93-
#[test]
94-
pub fn test_bool_to_str() {
95-
assert!(to_str(false) == ~"false");
96-
assert!(to_str(true) == ~"true");
97-
}
118+
#[test]
119+
fn test_bool_to_str() {
120+
assert!(to_str(false) == ~"false");
121+
assert!(to_str(true) == ~"true");
122+
}
98123

99-
#[test]
100-
pub fn test_bool_to_bit() {
101-
do all_values |v| {
102-
assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
124+
#[test]
125+
fn test_bool_to_bit() {
126+
do all_values |v| {
127+
assert!(to_bit(v) == if is_true(v) { 1u8 } else { 0u8 });
128+
}
103129
}
104-
}
105130

106-
// Local Variables:
107-
// mode: rust;
108-
// fill-column: 78;
109-
// indent-tabs-mode: nil
110-
// c-basic-offset: 4
111-
// buffer-file-coding-system: utf-8-unix
112-
// End:
131+
#[test]
132+
fn test_bool_ord() {
133+
assert!(true > false);
134+
assert!(!(false > true));
135+
136+
assert!(false < true);
137+
assert!(!(true < false));
138+
139+
assert!(false <= false);
140+
assert!(false >= false);
141+
assert!(true <= true);
142+
assert!(true >= true);
143+
144+
assert!(false <= true);
145+
assert!(!(false >= true));
146+
assert!(true >= false);
147+
assert!(!(true <= false));
148+
}
149+
150+
#[test]
151+
fn test_bool_totalord() {
152+
assert_eq!(true.cmp(&true), Equal);
153+
assert_eq!(false.cmp(&false), Equal);
154+
assert_eq!(true.cmp(&false), Greater);
155+
assert_eq!(false.cmp(&true), Less);
156+
}
157+
}

0 commit comments

Comments
 (0)