diff --git a/src/lib.rs b/src/lib.rs index 68c3a05..6845078 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -454,6 +454,16 @@ macro_rules! impl_ordered_float_binop { } } + // Work around for: https://github.com/reem/rust-ordered-float/issues/91 + impl<'a, T: $imp + Copy> $imp for &'a OrderedFloat { + type Output = OrderedFloat; + + #[inline] + fn $method(self, other: Self) -> Self::Output { + OrderedFloat((self.0).$method(other.0)) + } + } + impl $imp for OrderedFloat { type Output = OrderedFloat; diff --git a/tests/test.rs b/tests/test.rs index 014acc0..c1f31c2 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -659,6 +659,7 @@ fn add() { assert_eq!(OrderedFloat(0.0) + OrderedFloat(0.0), 0.0); assert_eq!(OrderedFloat(0.0) + &OrderedFloat(0.0), 0.0); assert_eq!(&OrderedFloat(0.0) + OrderedFloat(0.0), 0.0); + assert_eq!(&OrderedFloat(0.0) + &OrderedFloat(0.0), 0.0); assert_eq!(OrderedFloat(0.0) + 0.0, 0.0); assert_eq!(OrderedFloat(0.0) + &0.0, 0.0); assert_eq!(&OrderedFloat(0.0) + 0.0, 0.0); @@ -858,6 +859,35 @@ fn test_pow_fails_on_nan() { a.pow(b); } +#[test] +fn test_ref_ref_binop_regression() { + // repro from: + // https://github.com/reem/rust-ordered-float/issues/91 + // + // impl<'a, T> $imp for &'a OrderedFloat + // where + // &'a T: $imp + // { + // type Output = OrderedFloat<<&'a T as $imp>::Output>; + // #[inline] + // fn $method(self, other: Self) -> Self::Output { + // OrderedFloat((self.0).$method(&other.0)) + // } + // } + fn regression(p: T) -> T + where + for<'a> &'a T: std::ops::Sub<&'a T, Output = T>, + { + &p - &p + } + + assert_eq!(regression(0.0_f64), 0.0); + + let x = OrderedFloat(50.0); + let y = OrderedFloat(40.0); + assert_eq!(&x - &y, OrderedFloat(10.0)); +} + #[cfg(feature = "arbitrary")] mod arbitrary_test { use super::{NotNan, OrderedFloat};