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
use std::cmp::Ordering; #[derive(PartialEq, PartialOrd, Clone, Debug)] pub struct Total<T>(pub T); impl<T> Total<T> where T: Clone, { pub fn unwrap(&self) -> T { self.0.clone() } } impl<T: PartialEq> Eq for Total<T> {} impl<T: PartialOrd> Ord for Total<T> { fn cmp(&self, other: &Total<T>) -> Ordering { self.0.partial_cmp(&other.0).unwrap() } } #[cfg(test)] mod test { use super::*; #[test] fn test_total_ord() { let test = vec![2.2f64, 1.0f64, 8.5f64, 0.5f64, -3.5f64]; let sorted: Vec<f64> = vec![-3.5f64, 0.5f64, 1.0f64, 2.2f64, 8.5f64]; let mut total: Vec<Total<f64>> = test.iter().map(|&i| Total(i)).collect(); total.sort(); let total: Vec<f64> = total.iter().map(|&Total(i)| i).collect(); assert_eq!(sorted, total); } }