You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Use of clamp in generics does not compose with f32 and f64, maybe relax restriction to PartialOrd? #85508
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.
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?[/+]on May 20, 2021
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?[/+]on May 20, 2021
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.
Activity
[-]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?[/+]stephanemagnenat commentedon May 20, 2021
This is of course not a compiler bug, but rather a typing limitation in std.
[-]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?[/+]stephanemagnenat commentedon Aug 23, 2021
Similarly, it is not possible to generalize over float types when using
minandmaxbecause they are notOrd.This is work-aroundable using the
num-traitcrate with functionsclamp_minandclamp_max.Xaeroxe commentedon 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 commentedon Apr 26, 2023
Closing this as it is intended by the RFC