-
-
Notifications
You must be signed in to change notification settings - Fork 15.2k
Implement clamp_to #150075
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
base: main
Are you sure you want to change the base?
Implement clamp_to #150075
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| use crate::marker::Destruct; | ||
| use crate::ops::{RangeFrom, RangeFull, RangeInclusive, RangeToInclusive}; | ||
|
|
||
| /// Trait for ranges supported by [`Ord::clamp_to`]. | ||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| pub const trait ClampBounds<T>: Sized { | ||
| /// The implementation of [`Ord::clamp_to`]. | ||
| fn clamp(self, value: T) -> T | ||
| where | ||
| T: [const] Destruct; | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl<T> ClampBounds<T> for RangeFrom<T> | ||
| where | ||
| T: [const] Ord, | ||
| { | ||
| fn clamp(self, value: T) -> T | ||
| where | ||
| T: [const] Destruct, | ||
| { | ||
| value.max(self.start) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl<T> ClampBounds<T> for RangeToInclusive<T> | ||
| where | ||
| T: [const] Ord, | ||
| { | ||
| fn clamp(self, value: T) -> T | ||
| where | ||
| T: [const] Destruct, | ||
| { | ||
| value.min(self.end) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl<T> ClampBounds<T> for RangeInclusive<T> | ||
| where | ||
| T: [const] Ord, | ||
| { | ||
| fn clamp(self, value: T) -> T | ||
| where | ||
| T: [const] Destruct, | ||
| { | ||
| let (start, end) = self.into_inner(); | ||
| value.clamp(start, end) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl<T> ClampBounds<T> for RangeFull { | ||
| fn clamp(self, value: T) -> T { | ||
| value | ||
| } | ||
| } | ||
|
|
||
| macro impl_for_float($t:ty) { | ||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl ClampBounds<$t> for RangeFrom<$t> { | ||
| fn clamp(self, value: $t) -> $t { | ||
| assert!(!self.start.is_nan(), "start was NaN"); | ||
| value.max(self.start) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl ClampBounds<$t> for RangeToInclusive<$t> { | ||
| fn clamp(self, value: $t) -> $t { | ||
| assert!(!self.end.is_nan(), "end was NaN"); | ||
| value.min(self.end) | ||
| } | ||
| } | ||
|
|
||
| #[unstable(feature = "clamp_bounds", issue = "147781")] | ||
| #[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")] | ||
| const impl ClampBounds<$t> for RangeInclusive<$t> { | ||
| fn clamp(self, value: $t) -> $t { | ||
| let (start, end) = self.into_inner(); | ||
| assert!(start <= end, "start > end, or either was NaN"); | ||
| value.clamp(start, end) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // #[unstable(feature = "f16", issue = "116909")] | ||
| impl_for_float!(f16); | ||
| impl_for_float!(f32); | ||
| impl_for_float!(f64); | ||
| // #[unstable(feature = "f128", issue = "116909")] | ||
| impl_for_float!(f128); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1441,6 +1441,41 @@ impl f16 { | |
| self.clamp(-limit, limit) | ||
| } | ||
|
|
||
| /// Restrict a value to a certain range, unless it is NaN. | ||
| /// | ||
| /// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is | ||
| /// `min..`, `..=max`, or `min..=max`, respectively. However, unlike `max` and `min`, it will | ||
| /// panic if any bound is NaN. | ||
| /// | ||
| /// Note that this function returns NaN if the initial value was NaN as | ||
| /// well. | ||
| /// | ||
| /// Exclusive ranges are not permitted. | ||
| /// | ||
| /// # Panics | ||
| /// | ||
| /// Panics on `min..=max` if `min > max`, or if any bound is NaN. | ||
| /// | ||
| /// # Examples | ||
| /// | ||
| /// ``` | ||
| /// #![feature(f16, clamp_to)] | ||
| /// assert_eq!((-3.0f16).clamp_to(-2.0..=1.0), -2.0); | ||
| /// assert_eq!(0.0f16.clamp_to(-2.0..=1.0), 0.0); | ||
| /// assert_eq!(2.0f16.clamp_to(..=1.0), 1.0); | ||
| /// assert_eq!(5.0f16.clamp_to(7.0..), 7.0); | ||
| /// assert!(f16::NAN.clamp_to(1.0..=2.0).is_nan()); | ||
|
Comment on lines
+1462
to
+1467
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar with |
||
| /// ``` | ||
| #[must_use] | ||
| #[inline] | ||
| #[unstable(feature = "clamp_to", issue = "147781")] | ||
| pub fn clamp_to<R>(self, range: R) -> Self | ||
| where | ||
| R: crate::cmp::ClampBounds<Self>, | ||
| { | ||
| range.clamp(self) | ||
| } | ||
|
|
||
| /// Computes the absolute value of `self`. | ||
| /// | ||
| /// This function always returns the precise result. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1359,6 +1359,48 @@ float_test! { | |
| } | ||
| } | ||
|
|
||
| float_test! { | ||
| name: clamp_to_min_greater_than_max, | ||
| attrs: { | ||
| const: #[cfg(false)], | ||
| f16: #[should_panic, cfg(target_has_reliable_f16)], | ||
| f32: #[should_panic], | ||
| f64: #[should_panic], | ||
| f128: #[should_panic, cfg(target_has_reliable_f128)], | ||
| }, | ||
| test { | ||
| let _ = Float::ONE.clamp_to(3.0..=1.0); | ||
| } | ||
| } | ||
|
|
||
| float_test! { | ||
| name: clamp_to_min_is_nan, | ||
| attrs: { | ||
| const: #[cfg(false)], | ||
| f16: #[should_panic, cfg(target_has_reliable_f16)], | ||
| f32: #[should_panic], | ||
| f64: #[should_panic], | ||
| f128: #[should_panic, cfg(target_has_reliable_f128)], | ||
| }, | ||
| test { | ||
| let _ = Float::ONE.clamp_to(Float::NAN..=1.0); | ||
| } | ||
| } | ||
|
|
||
| float_test! { | ||
| name: clamp_to_max_is_nan, | ||
| attrs: { | ||
| const: #[cfg(false)], | ||
| f16: #[should_panic, cfg(target_has_reliable_f16)], | ||
| f32: #[should_panic], | ||
| f64: #[should_panic], | ||
| f128: #[should_panic, cfg(target_has_reliable_f128)], | ||
| }, | ||
| test { | ||
| let _ = Float::ONE.clamp_to(3.0..=Float::NAN); | ||
| } | ||
| } | ||
|
Comment on lines
+1362
to
+1402
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since these require libcalls, they need to be gated under |
||
|
|
||
| float_test! { | ||
| name: total_cmp, | ||
| attrs: { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requires a gate behind
#[cfg(target_has_reliable_f128_math)](see the above tests, excludingclamp_magnitudewhich apparently hasn't gotten updated)View changes since the review