Implement clamp_to

Remove trivial bounds

Panic on NaN

Make assert messages consistent with field names

Add clamp_to coretests

Update to fmt style

Use target_has_reliable_fN_math

Change existing clamp tests to use reliable_math

Use reliable_math on doctests too

Add #[expect] for NaN check
This commit is contained in:
Kyuuhachi
2025-12-16 23:03:24 +01:00
parent d9dd0703ba
commit bd174e1b20
8 changed files with 331 additions and 8 deletions

View File

@@ -26,7 +26,10 @@
#![stable(feature = "rust1", since = "1.0.0")]
mod bytewise;
mod clamp;
pub(crate) use bytewise::BytewiseEq;
#[unstable(feature = "clamp_bounds", issue = "147781")]
pub use clamp::ClampBounds;
use self::Ordering::*;
use crate::marker::{Destruct, PointeeSized};
@@ -1169,6 +1172,35 @@ pub const trait Ord: [const] Eq + [const] PartialOrd<Self> + PointeeSized {
self
}
}
/// Restrict a value to a certain range.
///
/// This is equal to `max`, `min`, or `clamp`, depending on whether the range is `min..`,
/// `..=max`, or `min..=max`, respectively. Exclusive ranges are not permitted.
///
/// # Panics
///
/// Panics on `min..=max` if `min > max`.
///
/// # Examples
///
/// ```
/// #![feature(clamp_to)]
/// assert_eq!((-3).clamp_to(-2..=1), -2);
/// assert_eq!(0.clamp_to(-2..=1), 0);
/// assert_eq!(2.clamp_to(..=1), 1);
/// assert_eq!(5.clamp_to(7..), 7);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "clamp_to", issue = "147781")]
fn clamp_to<R>(self, range: R) -> Self
where
Self: Sized + [const] Destruct,
R: [const] ClampBounds<Self>,
{
range.clamp(self)
}
}
/// Derive macro generating an impl of the trait [`Ord`].

View File

@@ -0,0 +1,104 @@
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> {
#[expect(
clippy::neg_cmp_op_on_partial_ord,
reason = "NaN check is intentionally included in comparison"
)]
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);

View File

@@ -1483,7 +1483,7 @@ impl f128 {
///
/// ```
/// #![feature(f128)]
/// # #[cfg(target_has_reliable_f128)] {
/// # #[cfg(target_has_reliable_f128_math)] {
///
/// assert!((-3.0f128).clamp(-2.0, 1.0) == -2.0);
/// assert!((0.0f128).clamp(-2.0, 1.0) == 0.0);
@@ -1552,6 +1552,43 @@ impl f128 {
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(f128, clamp_to)]
/// # #[cfg(target_has_reliable_f128_math)] {
/// assert_eq!((-3.0f128).clamp_to(-2.0..=1.0), -2.0);
/// assert_eq!(0.0f128.clamp_to(-2.0..=1.0), 0.0);
/// assert_eq!(2.0f128.clamp_to(..=1.0), 1.0);
/// assert_eq!(5.0f128.clamp_to(7.0..), 7.0);
/// assert!(f128::NAN.clamp_to(1.0..=2.0).is_nan());
/// # }
/// ```
#[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.

View File

@@ -1469,7 +1469,7 @@ impl f16 {
///
/// ```
/// #![feature(f16)]
/// # #[cfg(target_has_reliable_f16)] {
/// # #[cfg(target_has_reliable_f16_math)] {
///
/// assert!((-3.0f16).clamp(-2.0, 1.0) == -2.0);
/// assert!((0.0f16).clamp(-2.0, 1.0) == 0.0);
@@ -1538,6 +1538,43 @@ 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)]
/// # #[cfg(target_has_reliable_f16_math)] {
/// 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());
/// # }
/// ```
#[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.

View File

@@ -1709,6 +1709,41 @@ impl f32 {
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(clamp_to)]
/// assert_eq!((-3.0f32).clamp_to(-2.0..=1.0), -2.0);
/// assert_eq!(0.0f32.clamp_to(-2.0..=1.0), 0.0);
/// assert_eq!(2.0f32.clamp_to(..=1.0), 1.0);
/// assert_eq!(5.0f32.clamp_to(7.0..), 7.0);
/// assert!(f32::NAN.clamp_to(1.0..=2.0).is_nan());
/// ```
#[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.

View File

@@ -1687,6 +1687,41 @@ impl f64 {
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(clamp_to)]
/// assert_eq!((-3.0f64).clamp_to(-2.0..=1.0), -2.0);
/// assert_eq!(0.0f64.clamp_to(-2.0..=1.0), 0.0);
/// assert_eq!(2.0f64.clamp_to(..=1.0), 1.0);
/// assert_eq!(5.0f64.clamp_to(7.0..), 7.0);
/// assert!(f64::NAN.clamp_to(1.0..=2.0).is_nan());
/// ```
#[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.

View File

@@ -14,6 +14,7 @@
#![feature(cfg_overflow_checks)]
#![feature(cfg_target_has_reliable_f16_f128)]
#![feature(char_internals)]
#![feature(clamp_to)]
#![feature(clone_to_uninit)]
#![feature(cmp_minmax)]
#![feature(cmp_splat)]

View File

@@ -1328,10 +1328,10 @@ float_test! {
name: clamp_min_greater_than_max,
attrs: {
const: #[cfg(false)],
f16: #[should_panic, cfg(target_has_reliable_f16)],
f16: #[should_panic, cfg(target_has_reliable_f16_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128)],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
test {
let _ = Float::ONE.clamp(3.0, 1.0);
@@ -1342,10 +1342,10 @@ float_test! {
name: clamp_min_is_nan,
attrs: {
const: #[cfg(false)],
f16: #[should_panic, cfg(target_has_reliable_f16)],
f16: #[should_panic, cfg(target_has_reliable_f16_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128)],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
test {
let _ = Float::ONE.clamp(Float::NAN, 1.0);
@@ -1356,16 +1356,58 @@ float_test! {
name: clamp_max_is_nan,
attrs: {
const: #[cfg(false)],
f16: #[should_panic, cfg(target_has_reliable_f16)],
f16: #[should_panic, cfg(target_has_reliable_f16_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128)],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
test {
let _ = Float::ONE.clamp(3.0, Float::NAN);
}
}
float_test! {
name: clamp_to_min_greater_than_max,
attrs: {
const: #[cfg(false)],
f16: #[should_panic, cfg(target_has_reliable_f16_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
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_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
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_math)],
f32: #[should_panic],
f64: #[should_panic],
f128: #[should_panic, cfg(target_has_reliable_f128_math)],
},
test {
let _ = Float::ONE.clamp_to(3.0..=Float::NAN);
}
}
float_test! {
name: total_cmp,
attrs: {