8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- /*!
12
-
13
- The `bool` module contains useful code to help work with boolean values.
14
-
15
- A quick summary:
16
-
17
- ## Trait implementations for `bool`
18
-
19
- Implementations of the following traits:
20
-
21
- * `FromStr`
22
- * `Ord`
23
- * `TotalOrd`
24
- * `Eq`
25
-
26
- ## Various functions to compare `bool`s
27
-
28
- All of the standard comparison functions one would expect: `and`, `eq`, `or`,
29
- and more.
30
-
31
- Also, a few conversion functions: `to_bit` and `to_str`.
32
-
33
- Finally, some inquries into the nature of truth: `is_true` and `is_false`.
34
-
35
- */
11
+ //! Boolean logic
36
12
37
13
#[ cfg( not( test) ) ]
38
14
use cmp:: { Eq , Ord , TotalOrd , Ordering } ;
39
15
use option:: { None , Option , Some } ;
40
16
use from_str:: FromStr ;
41
17
42
- /**
43
- * Negation of a boolean value.
44
- *
45
- * # Examples
46
- * ~~~
47
- * rusti> core::bool::not(true)
48
- * false
49
- * ~~~
50
- * rusti> core::bool::not(false)
51
- * true
52
- * ~~~
53
- */
18
+ /// Negation / inverse
54
19
pub fn not ( v : bool ) -> bool { !v }
55
20
56
- /**
57
- * Conjunction of two boolean values.
58
- *
59
- * # Examples
60
- * ~~~
61
- * rusti> core::bool::and(true, false)
62
- * false
63
- * ~~~
64
- * rusti> core::bool::and(true, true)
65
- * true
66
- * ~~~
67
- */
21
+ /// Conjunction
68
22
pub fn and ( a : bool , b : bool ) -> bool { a && b }
69
23
70
- /**
71
- * Disjunction of two boolean values.
72
- *
73
- * # Examples
74
- * ~~~
75
- * rusti> core::bool::or(true, false)
76
- * true
77
- * ~~~
78
- * rusti> core::bool::or(false, false)
79
- * false
80
- * ~~~
81
- */
24
+ /// Disjunction
82
25
pub fn or ( a : bool , b : bool ) -> bool { a || b }
83
26
84
27
/**
85
- * An 'exclusive or' of two boolean values.
86
- *
87
- * 'exclusive or' is identical to `or(and(a, not(b)), and(not(a), b))`.
88
- *
89
- * # Examples
90
- * ~~~
91
- * rusti> core::bool::xor(true, false)
92
- * true
93
- * ~~~
94
- * rusti> core::bool::xor(true, true)
95
- * false
96
- * ~~~
97
- */
28
+ * Exclusive or
29
+ *
30
+ * Identical to `or(and(a, not(b)), and(not(a), b))`
31
+ */
98
32
pub fn xor ( a : bool , b : bool ) -> bool { ( a && !b) || ( !a && b) }
99
33
100
- /**
101
- * Implication between two boolean values.
102
- *
103
- * Implication is often phrased as 'if a then b.'
104
- *
105
- * 'if a then b' is equivalent to `!a || b`.
106
- *
107
- * # Examples
108
- * ~~~
109
- * rusti> core::bool::implies(true, true)
110
- * true
111
- * ~~~
112
- * rusti> core::bool::implies(true, false)
113
- * false
114
- * ~~~
115
- */
34
+ /// Implication in the logic, i.e. from `a` follows `b`
116
35
pub fn implies ( a : bool , b : bool ) -> bool { !a || b }
117
36
118
- /**
119
- * Equality between two boolean values.
120
- *
121
- * Two booleans are equal if they have the same value.
122
- *
123
- * # Examples
124
- * ~~~
125
- * rusti> core::bool::eq(false, true)
126
- * false
127
- * ~~~
128
- * rusti> core::bool::eq(false, false)
129
- * true
130
- * ~~~
131
- */
37
+ /// true if truth values `a` and `b` are indistinguishable in the logic
132
38
pub fn eq ( a : bool , b : bool ) -> bool { a == b }
133
39
134
- /**
135
- * Non-equality between two boolean values.
136
- *
137
- * Two booleans are not equal if they have different values.
138
- *
139
- * # Examples
140
- * ~~~
141
- * rusti> core::bool::ne(false, true)
142
- * true
143
- * ~~~
144
- * rusti> core::bool::ne(false, false)
145
- * false
146
- * ~~~
147
- */
40
+ /// true if truth values `a` and `b` are distinguishable in the logic
148
41
pub fn ne ( a : bool , b : bool ) -> bool { a != b }
149
42
150
- /**
151
- * Is a given boolean value true?
152
- *
153
- * # Examples
154
- * ~~~
155
- * rusti> core::bool::is_true(true)
156
- * true
157
- * ~~~
158
- * rusti> core::bool::is_true(false)
159
- * false
160
- * ~~~
161
- */
43
+ /// true if `v` represents truth in the logic
162
44
pub fn is_true ( v : bool ) -> bool { v }
163
45
164
- /**
165
- * Is a given boolean value false?
166
- *
167
- * # Examples
168
- * ~~~
169
- * rusti> core::bool::is_false(false)
170
- * true
171
- * ~~~
172
- * rusti> core::bool::is_false(true)
173
- * false
174
- * ~~~
175
- */
46
+ /// true if `v` represents falsehood in the logic
176
47
pub fn is_false ( v : bool ) -> bool { !v }
177
48
178
- /**
179
- * Parse a `bool` from a `str`.
180
- *
181
- * Yields an `Option<bool>`, because `str` may or may not actually be parseable.
182
- *
183
- * # Examples
184
- * ~~~
185
- * rusti> FromStr::from_str::<bool>("true")
186
- * Some(true)
187
- * ~~~
188
- * rusti> FromStr::from_str::<bool>("false")
189
- * Some(false)
190
- * ~~~
191
- * rusti> FromStr::from_str::<bool>("not even a boolean")
192
- * None
193
- * ~~~
194
- */
49
+ /// Parse logic value from `s`
195
50
impl FromStr for bool {
196
51
fn from_str ( s : & str ) -> Option < bool > {
197
52
match s {
@@ -202,49 +57,19 @@ impl FromStr for bool {
202
57
}
203
58
}
204
59
205
- /**
206
- * Convert a `bool` to a `str`.
207
- *
208
- * # Examples
209
- * ~~~
210
- * rusti> std::bool::to_str(true)
211
- * "true"
212
- * ~~~
213
- * rusti> std::bool::to_str(false)
214
- * "false"
215
- * ~~~
216
- */
60
+ /// Convert `v` into a string
217
61
pub fn to_str ( v : bool ) -> ~str { if v { ~"true " } else { ~"false " } }
218
62
219
63
/**
220
- * Iterates over all truth values, passing them to the given block.
221
- *
222
- * There are no guarantees about the order values will be given.
223
- *
224
- * # Examples
225
- * ~~~
226
- * do core::bool::all_values |x: bool| {
227
- * println(core::bool::to_str(x));
228
- * }
229
- * ~~~
230
- */
64
+ * Iterates over all truth values by passing them to `blk` in an unspecified
65
+ * order
66
+ */
231
67
pub fn all_values ( blk : & fn ( v : bool ) ) {
232
68
blk ( true ) ;
233
69
blk ( false ) ;
234
70
}
235
71
236
- /**
237
- * Convert a `bool` to a `u8`.
238
- *
239
- * # Examples
240
- * ~~~
241
- * rusti> std::bool::to_bit(true)
242
- * 1
243
- * ~~~
244
- * rusti> std::bool::to_bit(false)
245
- * 0
246
- * ~~~
247
- */
72
+ /// converts truth value to an 8 bit byte
248
73
#[ inline( always) ]
249
74
pub fn to_bit ( v : bool ) -> u8 { if v { 1u8 } else { 0u8 } }
250
75
0 commit comments