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
use std::cmp::Ordering;
use std::fmt::{Debug, Formatter};

#[derive(PartialEq)]
pub struct UnwrapOrd<T: PartialOrd + PartialEq>(pub T);

impl<T: PartialOrd + PartialEq> Eq for UnwrapOrd<T> {}

impl<T: PartialOrd + PartialEq> PartialOrd for UnwrapOrd<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: PartialOrd + PartialEq> Ord for UnwrapOrd<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}

impl<T: PartialOrd + PartialEq + Debug> Debug for UnwrapOrd<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "UncheckOrd({:?})", self.0)
    }
}

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

    #[test]
    fn it_works() {
        let v = [1.0, 3.0, 2.0];
        let mut v: Vec<_> = v.iter().copied().map(|x| UnwrapOrd(x)).collect();

        v.sort();

        assert_eq!(
            v,
            vec![
                UnwrapOrd(1.0_f64),
                UnwrapOrd(2.0_f64),
                UnwrapOrd(3.0_f64)
            ]
        );
    }
}