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
use core::ops::Bound;

use super::{Domain, Iterable};

impl Domain for bool {
    const DISCRETE: bool = true;

    /// Returns:
    ///
    /// |  `self` |     Output   |
    /// |---------|--------------|
    /// |   true  |  Some(false) |
    /// |  false  |     None     |
    #[must_use]
    fn predecessor(&self) -> Option<Self> {
        match self {
            true => Some(false),
            false => None,
        }
    }

    /// Returns:
    ///
    /// |  `self` |    Output    |
    /// |---------|--------------|
    /// |   true  |     None     |
    /// |  false  |  Some(true)  |
    #[must_use]
    fn successor(&self) -> Option<Self> {
        match self {
            true => None,
            false => Some(true),
        }
    }

    /// Returns `Included(false)`.
    #[must_use]
    fn minimum() -> Bound<Self> {
        Bound::Included(false)
    }

    /// Returns `Included(true)`.
    #[must_use]
    fn maximum() -> Bound<Self> {
        Bound::Included(true)
    }

    #[must_use]
    fn is_next_to(&self, other: &Self) -> bool {
        self != other
    }

    #[must_use]
    fn shares_neighbour_with(&self, _other: &Self) -> bool {
        false
    }
}

impl Iterable for bool {
    type Output = Self;

    #[must_use]
    fn next(&self) -> Option<Self::Output> {
        self.successor()
    }
}

#[cfg(test)]
mod tests {
    use crate::Domain;

    #[test]
    fn is_next_to() {
        assert!(!true.is_next_to(&true));
        assert!(true.is_next_to(&false));
        assert!(false.is_next_to(&true));
        assert!(!false.is_next_to(&false));
    }
    #[test]
    fn shares_neighbour_with() {
        assert!(!true.shares_neighbour_with(&true));
        assert!(!true.shares_neighbour_with(&false));
        assert!(!false.shares_neighbour_with(&true));
        assert!(!false.shares_neighbour_with(&false));
    }
    #[test]
    fn predecessor() {
        assert_eq!(true.predecessor(), Some(false));
        assert_eq!(false.predecessor(), None);
    }
    #[test]
    fn successor() {
        assert_eq!(true.successor(), None);
        assert_eq!(false.successor(), Some(true));
    }
}