1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*! as_bool provides an expanded notion of what is *true* and what is *false*.

    Specifically with the AsBool trait, which an implementing type can
    use to express how it should be represented in a boolean context.

    This crate also provides implementations of AsBool for Rust's builtin types
    and collections from the Standard Library. These implementations provide a
    truth table similar to the *Groovy Truth* implemented in the Groovy
    programming language. The truth table can be described as follow:

    * booleans behave as expected.
    * all non-zero numbers are `true`.
    * `0` , `0.0` , `f32::NAN`, `f64::NAN`, and `'\0'` are `false`.
    * non-empty strings are `true`.
    * empty strings are `false`.
    * non-empty collections are `true`.
    * empty collections are `false`.
    * `None` is always `false`.
    * `Err` is always `false`.
    * `Ok` and `Some` are unwrapped and the contained item is evaluated according
    to the preceding rules.
**/
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque};

/// `AsBool` defines a type's behavior in a boolean context. Basically, it converts
/// the implementing type to `bool`.
pub trait AsBool {
    fn as_bool(&self) -> bool;
}

// Booleans
impl AsBool for bool {
    fn as_bool(&self) -> bool {
        *self
    }
}

// Integers
impl AsBool for usize {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for u8 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for u16 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for u32 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for u64 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for u128 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for isize {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for i8 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for i16 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for i32 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for i64 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}
impl AsBool for i128 {
    fn as_bool(&self) -> bool {
        *self != 0
    }
}

// Floats
impl AsBool for f32 {
    fn as_bool(&self) -> bool {
        if self.is_nan() {
            false
        } else {
            *self != 0.0
        }
    }
}
impl AsBool for f64 {
    fn as_bool(&self) -> bool {
        if self.is_nan() {
            false
        } else {
            *self != 0.0
        }
    }
}

// Tuples
impl AsBool for () {
    fn as_bool(&self) -> bool {
        false
    }
}

// Arrays
impl<T> AsBool for [T] {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}

// Collections
impl<T> AsBool for Vec<T> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<T> AsBool for VecDeque<T> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<T> AsBool for LinkedList<T> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<K, V> AsBool for HashMap<K, V> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<K, V> AsBool for BTreeMap<K, V> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<T, S> AsBool for HashSet<T, S> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<T> AsBool for BTreeSet<T> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl<T> AsBool for BinaryHeap<T> {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}

// Text
impl AsBool for char {
    fn as_bool(&self) -> bool {
        *self != '\0'
    }
}
impl AsBool for &str {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}
impl AsBool for String {
    fn as_bool(&self) -> bool {
        !self.is_empty()
    }
}

// Option
impl<T: AsBool> AsBool for Option<T> {
    fn as_bool(&self) -> bool {
        if let Some(t) = self {
            t.as_bool()
        } else {
            false
        }
    }
}

// Result
impl<T: AsBool, E> AsBool for std::result::Result<T, E> {
    fn as_bool(&self) -> bool {
        if let Ok(t) = self {
            t.as_bool()
        } else {
            false
        }
    }
}

// Tests
#[cfg(test)]
mod tests {
    use crate::AsBool;
    use std::collections::{
        BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, LinkedList, VecDeque,
    };

    #[test]
    fn the_works() {
        assert!(true.as_bool());
        assert!(!false.as_bool());
        assert!((1 as usize).as_bool());
        assert!(!(0 as usize).as_bool());
        assert!((1 as u8).as_bool());
        assert!(!(0 as u8).as_bool());
        assert!((1 as u16).as_bool());
        assert!(!(0 as u16).as_bool());
        assert!((1 as u32).as_bool());
        assert!(!(0 as u32).as_bool());
        assert!((1 as u64).as_bool());
        assert!(!(0 as u64).as_bool());
        assert!((1 as u128).as_bool());
        assert!(!(0 as u128).as_bool());
        assert!((1 as isize).as_bool());
        assert!(!(0 as isize).as_bool());
        assert!((1 as i8).as_bool());
        assert!(!(0 as i8).as_bool());
        assert!((1 as i16).as_bool());
        assert!(!(0 as i16).as_bool());
        assert!((1 as i32).as_bool());
        assert!(!(0 as i32).as_bool());
        assert!((1 as i64).as_bool());
        assert!(!(0 as i64).as_bool());
        assert!((1 as i128).as_bool());
        assert!(!(0 as i128).as_bool());
        assert!((1.0 as f32).as_bool());
        assert!(!(0.0 as f32).as_bool());
        assert!((1.0 as f64).as_bool());
        assert!(!(0.0 as f64).as_bool());
        assert!(!(f32::NAN).as_bool());
        assert!(!(f64::NAN).as_bool());
        assert!('a'.as_bool());
        assert!(!'\0'.as_bool());
        assert!("a".as_bool());
        assert!(!"".as_bool());
        assert!("a".to_string().as_bool());
        assert!(!"".to_string().as_bool());

        assert!(!().as_bool());
        assert!(![true; 0].as_bool());
        assert!([true; 1].as_bool());

        let mut hm: HashMap<u8, bool> = HashMap::new();
        assert!(!hm.as_bool());
        hm.insert(1, true);
        assert!(hm.as_bool());

        let mut bm: BTreeMap<u8, bool> = BTreeMap::new();
        assert!(!bm.as_bool());
        bm.insert(1, true);
        assert!(bm.as_bool());

        let mut hs: HashSet<bool> = HashSet::new();
        assert!(!hs.as_bool());
        hs.insert(true);
        assert!(hs.as_bool());

        let mut bs: BTreeSet<bool> = BTreeSet::new();
        assert!(!bs.as_bool());
        bs.insert(true);
        assert!(bs.as_bool());

        let mut bh: BinaryHeap<bool> = BinaryHeap::new();
        assert!(!bh.as_bool());
        bh.push(true);
        assert!(bh.as_bool());

        let mut l: LinkedList<bool> = LinkedList::new();
        assert!(!l.as_bool());
        l.push_back(true);
        assert!(l.as_bool());

        let mut vd: VecDeque<bool> = VecDeque::new();
        assert!(!vd.as_bool());
        vd.push_back(true);
        assert!(vd.as_bool());

        let mut v: Vec<bool> = Vec::new();
        assert!(!v.as_bool());
        v.push(true);
        assert!(v.as_bool());

        assert!(Some(true).as_bool());
        assert!(!Some(false).as_bool());
        let n: Option<bool> = None;
        assert!(!n.as_bool());
        let mut o: Result<bool, bool> = Ok(true);
        let mut e: Result<bool, bool> = Err(true);
        assert!(o.as_bool());
        assert!(!e.as_bool());
        o = Ok(false);
        e = Err(false);
        assert!(!o.as_bool());
        assert!(!e.as_bool());
    }
}