Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1096,6 +1099,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`].
Expand Down
98 changes: 98 additions & 0 deletions library/core/src/cmp/clamp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
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")]
impl<T> const 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")]
impl<T> const 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")]
impl<T> const 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")]
impl<T> const 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")]
impl const ClampBounds<$t> for RangeFrom<$t> {
fn clamp(self, value: $t) -> $t {
value.max(self.start)
}
}

#[unstable(feature = "clamp_bounds", issue = "147781")]
#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")]
impl const ClampBounds<$t> for RangeToInclusive<$t> {
fn clamp(self, value: $t) -> $t {
value.min(self.end)
}
}

#[unstable(feature = "clamp_bounds", issue = "147781")]
#[rustc_const_unstable(feature = "clamp_bounds", issue = "147781")]
impl const ClampBounds<$t> for RangeInclusive<$t> {
fn clamp(self, value: $t) -> $t {
let (start, end) = self.into_inner();
// Deliberately avoid using `clamp` to handle NaN consistently
value.max(start).min(end)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be very surprised if x.clamp_to(a..=b) were to behave differently at all from x.clamp(a, b), regardless of the various values for x, a, and b.

Isn't clamp moving toward making its NaN behaviour consistent, anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but imo it's worse if x.clamp_to(a..) and x.clamp_to(a..=b) handle nan differently. Making all of them panic on nan would be a plausible choice though. It's listed as an unresolved question on the tracking issue.

}
}
}

// #[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);
33 changes: 33 additions & 0 deletions library/core/src/num/f128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,39 @@ impl f128 {
self.clamp(-limit, limit)
}

/// Restrict a value to a certain range.
///
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
/// values, this function treats them as unbounded, like `max` and `min`.
///
/// Exclusive ranges are not permitted.
///
/// # Panics
///
/// Panics on `min..=max` if `min > max`.
///
/// # Examples
///
/// ```
/// #![feature(f128, clamp_to)]
/// 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_eq!(4.0f128.clamp_to(1.0..=f128::NAN), 4.0);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "clamp_to", issue = "147781")]
pub fn clamp_to<R>(self, range: R) -> Self
where
Self: Sized,
R: crate::cmp::ClampBounds<Self>,
{
range.clamp(self)
}

/// Computes the absolute value of `self`.
///
/// This function always returns the precise result.
Expand Down
33 changes: 33 additions & 0 deletions library/core/src/num/f16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,39 @@ impl f16 {
self.clamp(-limit, limit)
}

/// Restrict a value to a certain range.
///
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
/// values, this function treats them as unbounded, like `max` and `min`.
///
/// Exclusive ranges are not permitted.
///
/// # Panics
///
/// Panics on `min..=max` if `min > max`.
///
/// # 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_eq!(4.0f16.clamp_to(1.0..=f16::NAN), 4.0);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "clamp_to", issue = "147781")]
pub fn clamp_to<R>(self, range: R) -> Self
where
Self: Sized,
R: crate::cmp::ClampBounds<Self>,
{
range.clamp(self)
}

/// Computes the absolute value of `self`.
///
/// This function always returns the precise result.
Expand Down
33 changes: 33 additions & 0 deletions library/core/src/num/f32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,39 @@ impl f32 {
self.clamp(-limit, limit)
}

/// Restrict a value to a certain range.
///
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
/// values, this function treats them as unbounded, like `max` and `min`.
///
/// Exclusive ranges are not permitted.
///
/// # Panics
///
/// Panics on `min..=max` if `min > max`.
///
/// # 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_eq!(4.0f32.clamp_to(1.0..=f32::NAN), 4.0);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "clamp_to", issue = "147781")]
pub fn clamp_to<R>(self, range: R) -> Self
where
Self: Sized,
R: crate::cmp::ClampBounds<Self>,
{
range.clamp(self)
}

/// Computes the absolute value of `self`.
///
/// This function always returns the precise result.
Expand Down
33 changes: 33 additions & 0 deletions library/core/src/num/f64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,39 @@ impl f64 {
self.clamp(-limit, limit)
}

/// Restrict a value to a certain range.
///
/// This is largely equal to `max`, `min`, or `clamp`, depending on whether the range is
/// `min..`, `..=max`, or `min..=max`, respectively. However, whereas `clamp` panics on NaN
/// values, this function treats them as unbounded, like `max` and `min`.
///
/// Exclusive ranges are not permitted.
///
/// # Panics
///
/// Panics on `min..=max` if `min > max`.
///
/// # 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_eq!(4.0f64.clamp_to(1.0..=f64::NAN), 4.0);
/// ```
#[must_use]
#[inline]
#[unstable(feature = "clamp_to", issue = "147781")]
pub fn clamp_to<R>(self, range: R) -> Self
where
Self: Sized,
R: crate::cmp::ClampBounds<Self>,
{
range.clamp(self)
}

/// Computes the absolute value of `self`.
///
/// This function always returns the precise result.
Expand Down
Loading