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
//! Traits for the minimun and maximum [`OptionOperations`].

// Required for doc
#[allow(unused)]
use crate::OptionOperations;

use crate::OptionOrd;

/// Trait for values and `Option`s that can be compared
/// to get the minimum or maximum.
///
/// Implementing this type leads to the following auto-implementations:
///
/// - `OptionMinMax<Option<InnerRhs>> for T`.
/// - `OptionMinMax<Rhs> for Option<T>`.
/// - `OptionMinMax<Option<InnerRhs>> for Option<T>`.
/// - ... and some variants with references.
///
/// This trait is auto-implemented for [`OptionOperations`] types
/// implementing `OptionOrd<Rhs>`.
pub trait OptionMinMax<Other, Inner = Other> {
    /// Compares and returns the minimum of two values.
    ///
    /// Returns `None` if they can't be compared, e.g. if
    /// at most one argument is `None`.
    #[must_use]
    fn opt_min(self, other: Other) -> Option<Inner>;

    /// Compares and returns the maximum of two values.
    ///
    /// Returns `None` if they can't be compared, e.g. if
    /// at most one argument is `None`.
    #[must_use]
    fn opt_max(self, other: Other) -> Option<Inner>;
}

impl<T> OptionMinMax<T> for T
where
    T: for<'a> OptionOrd<&'a T, T>,
{
    fn opt_min(self, other: T) -> Option<T> {
        self.opt_lt(&other)
            .map(|is_lt| if is_lt { self } else { other })
    }

    fn opt_max(self, other: T) -> Option<T> {
        self.opt_gt(&other)
            .map(|is_gt| if is_gt { self } else { other })
    }
}

impl<T> OptionMinMax<Option<T>, T> for T
where
    T: for<'a> OptionOrd<&'a T, T>,
{
    fn opt_min(self, other: Option<T>) -> Option<T> {
        other.and_then(|inner_other| {
            self.opt_lt(&inner_other)
                .map(|is_lt| if is_lt { self } else { inner_other })
        })
    }

    fn opt_max(self, other: Option<T>) -> Option<T> {
        other.and_then(|inner_other| {
            self.opt_gt(&inner_other)
                .map(|is_gt| if is_gt { self } else { inner_other })
        })
    }
}

impl<T> OptionMinMax<T> for Option<T>
where
    T: for<'a> OptionOrd<&'a T, T>,
{
    fn opt_min(self, other: T) -> Option<T> {
        self.and_then(|inner_self| {
            inner_self
                .opt_lt(&other)
                .map(|is_lt| if is_lt { inner_self } else { other })
        })
    }

    fn opt_max(self, other: T) -> Option<T> {
        self.and_then(|inner_self| {
            inner_self
                .opt_gt(&other)
                .map(|is_gt| if is_gt { inner_self } else { other })
        })
    }
}

impl<T> OptionMinMax<Option<T>, T> for Option<T>
where
    T: for<'a> OptionOrd<&'a T, T>,
{
    fn opt_min(self, other: Option<T>) -> Option<T> {
        self.zip(other).and_then(|(inner_self, inner_other)| {
            inner_self
                .opt_lt(&inner_other)
                .map(|is_lt| if is_lt { inner_self } else { inner_other })
        })
    }

    fn opt_max(self, other: Option<T>) -> Option<T> {
        self.zip(other).and_then(|(inner_self, inner_other)| {
            inner_self
                .opt_gt(&inner_other)
                .map(|is_gt| if is_gt { inner_self } else { inner_other })
        })
    }
}

#[cfg(test)]
mod test {
    use super::OptionMinMax;
    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 min() {
        assert_eq!(SOME_1.opt_min(SOME_2), SOME_1);
        assert_eq!(SOME_2.opt_min(SOME_1), SOME_1);
        assert_eq!(SOME_1.opt_min(NONE), None);

        assert_eq!(SOME_1.opt_min(MY_2), SOME_1);
        assert_eq!(SOME_2.opt_min(MY_1), SOME_1);

        assert_eq!(MY_1.opt_min(MY_2), SOME_1);
        assert_eq!(MY_2.opt_min(MY_1), SOME_1);

        assert_eq!(MY_1.opt_min(SOME_2), SOME_1);
        assert_eq!(MY_2.opt_min(SOME_1), SOME_1);

        assert_eq!(MY_1.opt_min(NONE), None);
        assert_eq!(NONE.opt_min(MY_1), None);

        assert_eq!(SOME_1.opt_min(NONE).or(SOME_1), SOME_1);
    }

    #[test]
    fn max() {
        assert_eq!(SOME_1.opt_max(SOME_2), SOME_2);
        assert_eq!(SOME_2.opt_max(SOME_1), SOME_2);
        assert_eq!(SOME_1.opt_max(NONE), None);

        assert_eq!(SOME_1.opt_max(MY_2), SOME_2);
        assert_eq!(SOME_2.opt_max(MY_1), SOME_2);

        assert_eq!(MY_1.opt_max(MY_2), SOME_2);
        assert_eq!(MY_2.opt_max(MY_1), SOME_2);

        assert_eq!(MY_1.opt_max(SOME_2), SOME_2);
        assert_eq!(MY_2.opt_max(SOME_1), SOME_2);

        assert_eq!(MY_1.opt_max(NONE), None);
        assert_eq!(NONE.opt_max(MY_1), None);

        assert_eq!(SOME_1.opt_max(NONE).or(SOME_1), SOME_1);
    }
}