20 lines
555 B
Rust
20 lines
555 B
Rust
/// A value bounded by a minimum and a maximum
|
|
///
|
|
/// If input is less than min then this returns min.
|
|
/// If input is greater than max then this returns max.
|
|
/// Otherwise this returns input.
|
|
///
|
|
/// **Panics** in debug mode if `!(min <= max)`.
|
|
///
|
|
/// Original implementation from num-traits licensed as MIT
|
|
pub fn clamp<T: PartialOrd>(input: T, min: T, max: T) -> T {
|
|
debug_assert!(min <= max, "min must be less than or equal to max");
|
|
if input < min {
|
|
min
|
|
} else if input > max {
|
|
max
|
|
} else {
|
|
input
|
|
}
|
|
}
|