Skip to content

Commit

Permalink
Merge pull request #162 from gechelberger/master
Browse files Browse the repository at this point in the history
feat: [resolves #145] &OrderedFloat <op> &OrderedFloat impl workaround
  • Loading branch information
mbrubeck authored Nov 3, 2024
2 parents 324b3e1 + c435cba commit 49f8f9d
Show file tree
Hide file tree
Showing 2 changed files with 40 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
30 changes: 30 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,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<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 49f8f9d

Please sign in to comment.