Explorar o código

Add clamp function

Jacob Kiesel %!s(int64=8) %!d(string=hai) anos
pai
achega
28633b7e6d
Modificáronse 1 ficheiros con 7 adicións e 0 borrados
  1. 7 0
      src/lib.rs

+ 7 - 0
src/lib.rs

@@ -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}
+}