Skip to content

Commit

Permalink
feat: &OrderedFloat <op> &OrderedFloat impl workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Echelberger committed Nov 3, 2024
1 parent 324b3e1 commit 5b86220
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> for &'a OrderedFloat<T> {
type Output = OrderedFloat<T::Output>;

#[inline]
fn $method(self, other: Self) -> Self::Output {
OrderedFloat((self.0).$method(other.0))
}
}

impl<T: $imp> $imp<T> for OrderedFloat<T> {
type Output = OrderedFloat<T::Output>;

Expand Down
31 changes: 31 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<Self> for &'a OrderedFloat<T>
// 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<T>(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};
Expand Down

0 comments on commit 5b86220

Please sign in to comment.