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
//! Trait for the equality [`OptionOperations`].

use crate::OptionOperations;

/// Trait for the equality [`OptionOperations`].
pub trait OptionEq<Rhs, InnerRhs = Rhs> {
    /// Tests whether `self` is equal to `other`.
    ///
    /// Returns `None` if they can't be compared, e.g. if
    /// at most one argument is `None`.
    #[must_use]
    fn opt_eq(&self, other: Rhs) -> Option<bool>;

    /// Tests whether `self` is not equal to `other`.
    ///
    /// Returns `None` if they can't be compared, e.g. if
    /// at most one argument is `None`.
    #[must_use]
    fn opt_ne(&self, other: Rhs) -> Option<bool> {
        self.opt_eq(other).map(|res| !res)
    }
}

impl<T, Rhs> OptionEq<&Rhs, Rhs> for T
where
    T: OptionOperations + PartialEq<Rhs>,
{
    fn opt_eq(&self, rhs: &Rhs) -> Option<bool> {
        Some(self.eq(rhs))
    }
}

impl<T, Rhs> OptionEq<Rhs> for T
where
    T: OptionOperations + for<'a> OptionEq<&'a Rhs, Rhs>,
{
    fn opt_eq(&self, rhs: Rhs) -> Option<bool> {
        self.opt_eq(&rhs)
    }
}

impl<T, InnerRhs> OptionEq<&Option<InnerRhs>, InnerRhs> for T
where
    T: OptionOperations + for<'a> OptionEq<&'a InnerRhs, InnerRhs>,
{
    fn opt_eq(&self, rhs: &Option<InnerRhs>) -> Option<bool> {
        rhs.as_ref().and_then(|inner_rhs| self.opt_eq(inner_rhs))
    }
}

impl<T, InnerRhs> OptionEq<Option<InnerRhs>, InnerRhs> for T
where
    T: OptionOperations + for<'a> OptionEq<&'a InnerRhs, InnerRhs>,
{
    fn opt_eq(&self, rhs: Option<InnerRhs>) -> Option<bool> {
        rhs.as_ref().and_then(|inner_rhs| self.opt_eq(inner_rhs))
    }
}

impl<T, Rhs> OptionEq<&Rhs, Rhs> for Option<T>
where
    T: OptionOperations + for<'a> OptionEq<&'a Rhs, Rhs>,
{
    fn opt_eq(&self, rhs: &Rhs) -> Option<bool> {
        self.as_ref().and_then(|inner_self| inner_self.opt_eq(rhs))
    }
}

impl<T, Rhs> OptionEq<Rhs> for Option<T>
where
    T: OptionOperations + for<'a> OptionEq<&'a Rhs, Rhs>,
{
    fn opt_eq(&self, rhs: Rhs) -> Option<bool> {
        self.opt_eq(&rhs)
    }
}

impl<T, InnerRhs> OptionEq<&Option<InnerRhs>, InnerRhs> for Option<T>
where
    T: OptionOperations + for<'a> OptionEq<&'a InnerRhs, InnerRhs>,
{
    fn opt_eq(&self, rhs: &Option<InnerRhs>) -> Option<bool> {
        match (self, rhs) {
            (Some(inner_self), Some(inner_rhs)) => inner_self.opt_eq(inner_rhs),
            (None, None) => Some(true),
            _ => None,
        }
    }
}

impl<T, InnerRhs> OptionEq<Option<InnerRhs>, InnerRhs> for Option<T>
where
    T: OptionOperations + for<'a> OptionEq<&'a InnerRhs, InnerRhs>,
{
    fn opt_eq(&self, rhs: Option<InnerRhs>) -> Option<bool> {
        match (self, rhs.as_ref()) {
            (Some(inner_self), Some(inner_rhs)) => inner_self.opt_eq(inner_rhs),
            (None, None) => Some(true),
            _ => None,
        }
    }
}

#[cfg(test)]
mod test {
    use super::OptionEq;
    use crate::OptionOperations;

    #[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
    struct MyInt(u64);

    impl OptionOperations for MyInt {}

    const MY_1: MyInt = MyInt(1);
    const MY_2: MyInt = MyInt(2);
    const SOME_1: Option<MyInt> = Some(MY_1);
    const SOME_2: Option<MyInt> = Some(MY_2);
    const NONE: Option<MyInt> = None;

    #[test]
    fn opt_eq() {
        assert_eq!(MY_1.opt_eq(MY_1), Some(true));
        assert_eq!(MY_1.opt_eq(SOME_1), Some(true));
        assert_eq!(SOME_1.opt_eq(MY_1), Some(true));
        assert_eq!(SOME_1.opt_eq(SOME_1), Some(true));

        assert_eq!(MY_1.opt_eq(MY_2), Some(false));
        assert_eq!(MY_1.opt_eq(SOME_2), Some(false));
        assert_eq!(SOME_1.opt_eq(MY_2), Some(false));
        assert_eq!(SOME_1.opt_eq(SOME_2), Some(false));

        assert_eq!(MY_1.opt_eq(NONE), None);
        assert_eq!(NONE.opt_eq(SOME_2), None);
        assert_eq!(SOME_1.opt_eq(NONE), None);
        assert_eq!(NONE.opt_eq(SOME_2), None);
        assert_eq!(NONE.opt_eq(NONE), Some(true));
    }

    #[test]
    fn opt_ne() {
        assert_eq!(MY_1.opt_ne(MY_1), Some(false));
        assert_eq!(MY_1.opt_ne(SOME_1), Some(false));
        assert_eq!(SOME_1.opt_ne(MY_1), Some(false));
        assert_eq!(SOME_1.opt_ne(SOME_1), Some(false));

        assert_eq!(MY_1.opt_ne(MY_2), Some(true));
        assert_eq!(MY_1.opt_ne(SOME_2), Some(true));
        assert_eq!(SOME_1.opt_ne(MY_2), Some(true));
        assert_eq!(SOME_1.opt_ne(SOME_2), Some(true));

        assert_eq!(MY_1.opt_ne(NONE), None);
        assert_eq!(NONE.opt_ne(SOME_2), None);
        assert_eq!(SOME_1.opt_ne(NONE), None);
        assert_eq!(NONE.opt_ne(SOME_2), None);
        assert_eq!(NONE.opt_ne(NONE), Some(false));
    }
}