unwrap_ord/
lib.rs

1use std::cmp::Ordering;
2use std::fmt::{Debug, Formatter};
3
4#[derive(PartialEq)]
5pub struct UnwrapOrd<T: PartialOrd + PartialEq>(pub T);
6
7impl<T: PartialOrd + PartialEq> Eq for UnwrapOrd<T> {}
8
9impl<T: PartialOrd + PartialEq> PartialOrd for UnwrapOrd<T> {
10    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
11        Some(self.cmp(other))
12    }
13}
14
15impl<T: PartialOrd + PartialEq> Ord for UnwrapOrd<T> {
16    fn cmp(&self, other: &Self) -> Ordering {
17        self.0.partial_cmp(&other.0).unwrap()
18    }
19}
20
21impl<T: PartialOrd + PartialEq + Debug> Debug for UnwrapOrd<T> {
22    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
23        write!(f, "UncheckOrd({:?})", self.0)
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::UnwrapOrd;
30
31    #[test]
32    fn it_works() {
33        let v = [1.0, 3.0, 2.0];
34        let mut v: Vec<_> = v.iter().copied().map(|x| UnwrapOrd(x)).collect();
35
36        v.sort();
37
38        assert_eq!(
39            v,
40            vec![
41                UnwrapOrd(1.0_f64),
42                UnwrapOrd(2.0_f64),
43                UnwrapOrd(3.0_f64)
44            ]
45        );
46    }
47}