Skip to content

Generalise lifetimes for binop implementations#181

Merged
mbrubeck merged 3 commits intoreem:masterfrom
Jacobgarm:patch-1
Mar 25, 2026
Merged

Generalise lifetimes for binop implementations#181
mbrubeck merged 3 commits intoreem:masterfrom
Jacobgarm:patch-1

Conversation

@Jacobgarm
Copy link
Contributor

The current implementations of the binary operator traits for OrderedFloat and NotNan expect both operands to have the same lifetime (or at least not general lifetimes). This is fine in the majority of cases, but fails when working with higher order trait bounds. As an example, the following code doesn't compile on master:

fn foo<T>(x: T) -> T
where
    for<'a, 'b> &'a T: Add<&'b T, Output = T>,
{
    &x + &x
}

fn main() {
    foo(OrderedFloat::from(2.0));
}

The change is to manually speficify the type of the left hand side in the implementation, rather than using Self. This makes the above example compile.

Copy link
Collaborator

@mbrubeck mbrubeck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mbrubeck mbrubeck merged commit f6b9d9b into reem:master Mar 25, 2026
2 checks passed
@Jacobgarm Jacobgarm deleted the patch-1 branch March 25, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants