1
//! `BoolIndexedArray`
2

            
3
use super::*;
4

            
5
/// Array of two values, one for `false` and one for `true`
6
///
7
/// This is a wrapper around `[T; 2]`, which views the array as being indexed by `bool`.
8
///
9
/// Can be constructed from a `[T; 2]` using `From`.
10
/// Implements `Default` if `T` is `Default`.
11
///
12
/// Ordering compares first the `true` value, then the `false` one.
13
//
14
// We use array rather than tuple, because in Rust an array is always better than a tuple
15
// if the values are known to be of the same type.
16
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)] //
17
#[derive(derive_more::Into, derive_more::From)]
18
// TODO move this somewhere else maybe? flesh out this API?
19
pub(crate) struct BoolIndexedArray<T>([T; 2]);
20

            
21
impl<T> std::ops::Index<bool> for BoolIndexedArray<T> {
22
    type Output = T;
23
10
    fn index(&self, b: bool) -> &T {
24
10
        &self.0[usize::from(b)]
25
10
    }
26
}
27
impl<T> std::ops::IndexMut<bool> for BoolIndexedArray<T> {
28
13
    fn index_mut(&mut self, b: bool) -> &mut T {
29
13
        &mut self.0[usize::from(b)]
30
13
    }
31
}
32

            
33
impl<T> BoolIndexedArray<T> {
34
    /// Treating this as a vote tally, are there (strictly) more yeses than noes?
35
5
    pub(crate) fn more_yes_than_no(&self) -> bool
36
5
    where
37
5
        T: Ord,
38
    {
39
5
        self[true] > self[false]
40
5
    }
41
}