@@ -66,8 +66,13 @@ pub fn all_values(blk: |v: bool|) {
66
66
// Methods on `bool`
67
67
/////////////////////////////////////////////////////////////////////////////
68
68
69
- /// Extension methods on a `bool`
70
- pub trait Bool {
69
+ #[ cfg( not( stage0) , not( test) ) ]
70
+ #[ lang = "bool_impl" ]
71
+ /// The boolean primitive for Rust.
72
+ ///
73
+ /// This primitive can have two values, `true` and `false`, and is type type
74
+ /// that is used to conditions in `if` statements and `while` loops.
75
+ impl bool {
71
76
/// Conjunction of two boolean values.
72
77
///
73
78
/// # Examples
@@ -78,7 +83,7 @@ pub trait Bool {
78
83
/// assert_eq!(false.and(true), false);
79
84
/// assert_eq!(false.and(false), false);
80
85
/// ```
81
- fn and ( self , b : bool ) -> bool ;
86
+ pub fn and ( self , b : bool ) -> bool { self && b }
82
87
83
88
/// Disjunction of two boolean values.
84
89
///
@@ -90,7 +95,8 @@ pub trait Bool {
90
95
/// assert_eq!(false.or(true), true);
91
96
/// assert_eq!(false.or(false), false);
92
97
/// ```
93
- fn or ( self , b : bool ) -> bool ;
98
+ #[ inline]
99
+ pub fn or ( self , b : bool ) -> bool { self || b }
94
100
95
101
/// An 'exclusive or' of two boolean values.
96
102
///
@@ -104,7 +110,8 @@ pub trait Bool {
104
110
/// assert_eq!(false.xor(true), true);
105
111
/// assert_eq!(false.xor(false), false);
106
112
/// ```
107
- fn xor ( self , b : bool ) -> bool ;
113
+ #[ inline]
114
+ pub fn xor ( self , b : bool ) -> bool { self ^ b }
108
115
109
116
/// Implication between two boolean values.
110
117
///
@@ -120,7 +127,8 @@ pub trait Bool {
120
127
/// assert_eq!(false.implies(true), true);
121
128
/// assert_eq!(false.implies(false), true);
122
129
/// ```
123
- fn implies ( self , b : bool ) -> bool ;
130
+ #[ inline]
131
+ pub fn implies ( self , b : bool ) -> bool { !self || b }
124
132
125
133
/// Convert a `bool` to a `u8`.
126
134
///
@@ -130,9 +138,24 @@ pub trait Bool {
130
138
/// assert_eq!(true.to_bit::<u8>(), 1u8);
131
139
/// assert_eq!(false.to_bit::<u8>(), 0u8);
132
140
/// ```
141
+ #[ inline]
142
+ pub fn to_bit < N : FromPrimitive > ( self ) -> N {
143
+ if self { FromPrimitive :: from_u8 ( 1 ) . unwrap ( ) }
144
+ else { FromPrimitive :: from_u8 ( 0 ) . unwrap ( ) }
145
+ }
146
+ }
147
+
148
+ #[ cfg( stage0) ]
149
+ #[ allow( missing_doc) ]
150
+ pub trait Bool {
151
+ fn and ( self , b : bool ) -> bool ;
152
+ fn or ( self , b : bool ) -> bool ;
153
+ fn xor ( self , b : bool ) -> bool ;
154
+ fn implies ( self , b : bool ) -> bool ;
133
155
fn to_bit < N : FromPrimitive > ( self ) -> N ;
134
156
}
135
157
158
+ #[ cfg( stage0) ]
136
159
impl Bool for bool {
137
160
#[ inline]
138
161
fn and ( self , b : bool ) -> bool { self && b }
0 commit comments