-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Open
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.Status: The feature has not been implemented.S-waiting-on-t-libs-apiStatus: Awaiting decision from T-libs-apiStatus: Awaiting decision from T-libs-apiT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Description
Feature gate: #![feature(clamp_to)], as well as possibly perma-unstable #[feature(clamp_bounds)].
This is a tracking issue for rust-lang/libs-team#665, which adds less confusable alternatives for x.min(y) and x.max(y).
Public API
// Perma-unstable
trait ClampBounds<T> {
fn clamp(self, x: T) -> T;
}
impl ClampBounds<T> for RangeInclusive<T> where T: Ord {}
impl ClampBounds<f32> for RangeInclusive<f32> {}
// likewise for RangeFrom, RangeToInclusive, and RangeFull, and for f16/f64/f128
trait Ord {
fn clamp_to(self, range: impl ClampBounds<Self>) -> Self;
}
// For each floating-point type:
impl f32 {
fn clamp_to(self, range: impl ClampBounds<Self>) -> Self;
}Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
- ACP: ACP: Intuitive alternative for
.min()and.max()libs-team#665 - Implementation 1: Implement clamp_min and clamp_max #147786
- Implementation 2: Implement clamp_to #150075
- Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
Is having these functions, where clamp_min == max, actually less confusing than the status quo?- It is not. Switched to
clamp_towith ranges.
- It is not. Switched to
- Should there be a lint that suggests replacing
x.max(y)withx.clamp_to(y..)? - What semantics should the float versions have regarding NaN?
NAN.max(0.0)is 0, whileNAN.clamp(0.0, 0.0)is NaN, and0.0.clamp(NAN, NAN)panics.
Footnotes
Metadata
Metadata
Assignees
Labels
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCS-tracking-unimplementedStatus: The feature has not been implemented.Status: The feature has not been implemented.S-waiting-on-t-libs-apiStatus: Awaiting decision from T-libs-apiStatus: Awaiting decision from T-libs-apiT-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.