Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of clamp in generics does not compose with f32 and f64, maybe relax restriction to PartialOrd? #85508

Closed
stephanemagnenat opened this issue May 20, 2021 · 4 comments
Labels
C-bug Category: This is a bug.

Comments

@stephanemagnenat
Copy link

stephanemagnenat commented May 20, 2021

When using T::clamp() in generics, T must be Ord. Types f32 and f64 are not Ord but implements clamp since 1.50.0, yet they cannot be used in generics due to the Ord constraint.

A possible solution I can think of is relax the constraint for clamp() from Ord to PartialOrd.

I tried this code:

fn c<T: PartialOrd>(v: T, min: T, max: T) -> T {
    v.clamp(min, max)
}

fn main() {
    println!("{}", c(1.0, 0.0, 2.0));
}

I expected to see this happen: compilation success

Instead, this happened: error[E0599]: the method clamp exists for type parameter T, but its trait bounds were not satisfied

Meta

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-unknown-linux-gnu
release: 1.52.1
LLVM version: 12.0.0
@stephanemagnenat stephanemagnenat added the C-bug Category: This is a bug. label May 20, 2021
@stephanemagnenat stephanemagnenat changed the title Use of clamp in generics does not combine with f64, maybe relax restriction to PartialOrd? Use of clamp in generics does not combine with f32 and f64, maybe relax restriction to PartialOrd? May 20, 2021
@stephanemagnenat
Copy link
Author

This is of course not a compiler bug, but rather a typing limitation in std.

@stephanemagnenat stephanemagnenat changed the title Use of clamp in generics does not combine with f32 and f64, maybe relax restriction to PartialOrd? Use of clamp in generics does not compose with f32 and f64, maybe relax restriction to PartialOrd? May 20, 2021
@stephanemagnenat
Copy link
Author

stephanemagnenat commented Aug 23, 2021

Similarly, it is not possible to generalize over float types when using min and max because they are not Ord.

This is work-aroundable using the num-trait crate with functions clamp_min and clamp_max.

@Xaeroxe
Copy link
Contributor

Xaeroxe commented Jan 1, 2022

Yeah actually we talked about this when clamp was introduced. rust-lang/rfcs#1961 (comment)

The conclusion we came to is that PartialOrd was too likely to lead to surprising behavior. Thanks to the num crate you can still write your software the way you want to though.

@Dylan-DPC
Copy link
Member

Closing this as it is intended by the RFC

@Dylan-DPC Dylan-DPC closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants