summaryrefslogtreecommitdiff
path: root/src/ord_adaptor.rs
blob: c7c67631dd8e2d2f1f5c1729ed794f70216c3c4c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! Newtypes to use floating numbers in sorting algs

use std::cmp::Ordering;

/// Defaults to `Ordering::Less` if the wrapped value can't be compared
#[derive(PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct DefaultLess<T: PartialOrd>(pub T);

// Not ideal, but `Ord` requires `Eq`, so...
impl<T: PartialOrd> Eq for DefaultLess<T> {}

#[allow(clippy::derive_ord_xor_partial_ord)]
impl<T: PartialOrd> Ord for DefaultLess<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.partial_cmp(other).unwrap_or(Ordering::Less)
    }
}