Add clamp function

This commit is contained in:
Jacob Kiesel 2017-02-07 09:48:34 -07:00
parent 817ab00f64
commit 28633b7e6d
1 changed files with 7 additions and 0 deletions

View File

@ -109,3 +109,10 @@ pub mod traits {
pub mod rational {
pub use num_rational::*;
}
pub fn clamp<T: PartialOrd + Copy>(input: T, min: T, max: T) -> T {
debug_assert!(min < max, "min must be less than max");
if input < min {min}
else if input > max {max}
else {input}
}