From 5b8622080ed9d6bf262d189c84d4f9b7d8b2c7a7 Mon Sep 17 00:00:00 2001 From: Greg Echelberger Date: Sun, 3 Nov 2024 22:22:31 +0000 Subject: [PATCH 1/2] feat: &OrderedFloat &OrderedFloat impl workaround --- src/lib.rs | 10 ++++++++++ tests/test.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) 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..42247c5 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,36 @@ 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}; From c435cbaf5ad8d6036a8593c90cf4cee434d21063 Mon Sep 17 00:00:00 2001 From: Matt Brubeck Date: Sun, 3 Nov 2024 15:16:57 -0800 Subject: [PATCH 2/2] rustfmt --- tests/test.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/test.rs b/tests/test.rs index 42247c5..c1f31c2 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -859,14 +859,13 @@ fn test_pow_fails_on_nan() { a.pow(b); } - #[test] fn test_ref_ref_binop_regression() { - // repro from: + // repro from: // https://github.com/reem/rust-ordered-float/issues/91 // // impl<'a, T> $imp for &'a OrderedFloat - // where + // where // &'a T: $imp // { // type Output = OrderedFloat<<&'a T as $imp>::Output>;