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

macro_rules! compare_float_types {
    ($fn_name_: ident, $type_: ident) => {
        /// Compares floating point numbers.
        pub fn $fn_name_(a: $type_, b: $type_) -> Ordering {
            match (a, b) {
                (x, y) if x.is_nan() && y.is_nan() => Ordering::Equal,
                (x, _) if x.is_nan() => Ordering::Greater,
                (_, y) if y.is_nan() => Ordering::Less,
                (_, _) => a.partial_cmp(&b).unwrap(),
            }
        }
    };
}

compare_float_types! { compare_floats, f64}
compare_float_types! { compare_floats_f32, f32}

/// Unwraps result type.
pub fn unwrap_from_result<T>(result: Result<T, T>) -> T {
    match result {
        Ok(result) => result,
        Err(result) => result,
    }
}