Skip to content

Commit 70d1150

Browse files
committed
Auto merge of #58710 - EdorianDark:master, r=sfackler
Add clamp for ranges. Implements #44095 Ready for merge
2 parents bc44841 + 6041ec3 commit 70d1150

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

src/libcore/cmp.rs

+26
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,32 @@ pub trait Ord: Eq + PartialOrd<Self> {
567567
where Self: Sized {
568568
if self <= other { self } else { other }
569569
}
570+
571+
/// Returns max if self is greater than max, and min if self is less than min.
572+
/// Otherwise this will return self. Panics if min > max.
573+
///
574+
/// # Examples
575+
///
576+
/// ```
577+
/// #![feature(clamp)]
578+
///
579+
/// assert!((-3).clamp(-2, 1) == -2);
580+
/// assert!(0.clamp(-2, 1) == 0);
581+
/// assert!(2.clamp(-2, 1) == 1);
582+
/// ```
583+
#[unstable(feature = "clamp", issue = "44095")]
584+
fn clamp(self, min: Self, max: Self) -> Self
585+
where Self: Sized {
586+
assert!(min <= max);
587+
if self < min {
588+
min
589+
}
590+
else if self > max {
591+
max
592+
} else {
593+
self
594+
}
595+
}
570596
}
571597

572598
#[stable(feature = "rust1", since = "1.0.0")]

src/libstd/f32.rs

+21
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,27 @@ impl f32 {
956956
pub fn atanh(self) -> f32 {
957957
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
958958
}
959+
/// Returns max if self is greater than max, and min if self is less than min.
960+
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
961+
///
962+
/// # Examples
963+
///
964+
/// ```
965+
/// #![feature(clamp)]
966+
/// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32);
967+
/// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32);
968+
/// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32);
969+
/// ```
970+
#[unstable(feature = "clamp", issue = "44095")]
971+
#[inline]
972+
pub fn clamp(self, min: f32, max: f32) -> f32 {
973+
assert!(min <= max);
974+
let mut x = self;
975+
if x < min { x = min; }
976+
if x > max { x = max; }
977+
x
978+
}
979+
959980
}
960981

961982
#[cfg(test)]

src/libstd/f64.rs

+21
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,27 @@ impl f64 {
878878
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p()
879879
}
880880

881+
/// Returns max if self is greater than max, and min if self is less than min.
882+
/// Otherwise this returns self. Panics if min > max, min equals NaN, or max equals NaN.
883+
///
884+
/// # Examples
885+
///
886+
/// ```
887+
/// #![feature(clamp)]
888+
/// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64);
889+
/// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64);
890+
/// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64);
891+
/// ```
892+
#[unstable(feature = "clamp", issue = "44095")]
893+
#[inline]
894+
pub fn clamp(self, min: f64, max: f64) -> f64 {
895+
assert!(min <= max);
896+
let mut x = self;
897+
if x < min { x = min; }
898+
if x > max { x = max; }
899+
x
900+
}
901+
881902
// Solaris/Illumos requires a wrapper around log, log2, and log10 functions
882903
// because of their non-standard behavior (e.g., log(-n) returns -Inf instead
883904
// of expected NaN).

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@
245245
#![feature(cfg_target_thread_local)]
246246
#![feature(char_error_internals)]
247247
#![feature(checked_duration_since)]
248+
#![feature(clamp)]
248249
#![feature(compiler_builtins_lib)]
249250
#![feature(concat_idents)]
250251
#![feature(const_cstr_unchecked)]

0 commit comments

Comments
 (0)