vortex_array/
partial_ord.rs

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
use std::cmp::Ordering;

pub trait PartialMin: PartialOrd<Self>
where
    Self: Sized,
{
    /// Returns the minimum of two values, if they are comparable.
    #[inline]
    fn partial_min(self, other: Self) -> Option<Self> {
        if self.partial_cmp(&other)? == Ordering::Less {
            Some(self)
        } else {
            Some(other)
        }
    }
}

pub fn partial_min<T: PartialOrd + PartialMin>(a: T, b: T) -> Option<T> {
    a.partial_min(b)
}

impl<T: PartialOrd> PartialMin for T {}

pub trait PartialMax: PartialOrd<Self>
where
    Self: Sized,
{
    /// Returns the maximum of two values, if they are comparable.
    #[inline]
    fn partial_max(self, other: Self) -> Option<Self> {
        if self.partial_cmp(&other)? == Ordering::Greater {
            Some(self)
        } else {
            Some(other)
        }
    }
}

impl<T: PartialOrd> PartialMax for T {}

pub fn partial_max<T: PartialOrd + PartialMax>(a: T, b: T) -> Option<T> {
    a.partial_max(b)
}