From 28633b7e6dd7375f336384cd561ec9bafffda3d0 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 09:48:34 -0700 Subject: [PATCH 1/8] Add clamp function --- src/lib.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 77b34c3..14bb02d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,3 +109,10 @@ pub mod traits { pub mod rational { pub use num_rational::*; } + +pub fn clamp(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} +} From 07ff5b62b92d500a9f13ad0de66b414f341accc7 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 09:52:23 -0700 Subject: [PATCH 2/8] Changing to >= and <= as it's a slight optimization --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 14bb02d..5b98681 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -112,7 +112,7 @@ pub mod rational { pub fn clamp(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} + if input <= min {min} + else if input >= max {max} else {input} } From a5445b7619155ca795d29b0786c9723d5f424b64 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 10:18:23 -0700 Subject: [PATCH 3/8] Adding documentation comments --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5b98681..bfef442 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,7 +109,10 @@ pub mod traits { pub mod rational { pub use num_rational::*; } - +/// A value bounded by a minimum and a maximum +/// +/// If input is less than min then min is returned, if input is greater than max then max is +/// returned. Otherwise input is returned. pub fn clamp(input: T, min: T, max: T) -> T { debug_assert!(min < max, "min must be less than max"); if input <= min {min} From b346f9c2df07e94c387a2341769f9a07865426f7 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 10:18:23 -0700 Subject: [PATCH 4/8] Add a new line for improved formatting. --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index bfef442..0a2cdee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,7 @@ pub mod traits { pub mod rational { pub use num_rational::*; } + /// A value bounded by a minimum and a maximum /// /// If input is less than min then min is returned, if input is greater than max then max is From 182e08a0919de488c635c6a7ec15024a85bc9d8d Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 10:49:28 -0700 Subject: [PATCH 5/8] Add inline attribute --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 0a2cdee..9e92a91 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,6 +114,7 @@ pub mod rational { /// /// If input is less than min then min is returned, if input is greater than max then max is /// returned. Otherwise input is returned. +#[inline] pub fn clamp(input: T, min: T, max: T) -> T { debug_assert!(min < max, "min must be less than max"); if input <= min {min} From f8dcec366b93e3d40e301d12c33698cc3c1bd2d8 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 13:02:32 -0700 Subject: [PATCH 6/8] Made requested changes. --- src/lib.rs | 14 +------------- traits/src/lib.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9e92a91..8eefb08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -81,7 +81,7 @@ pub use num_traits::{Num, Zero, One, Signed, Unsigned, Bounded, one, zero, abs, abs_sub, signum, Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, PrimInt, Float, ToPrimitive, FromPrimitive, NumCast, cast, - pow, checked_pow}; + pow, checked_pow, clamp}; #[cfg(feature = "num-bigint")] pub mod bigint { @@ -109,15 +109,3 @@ pub mod traits { pub mod rational { pub use num_rational::*; } - -/// A value bounded by a minimum and a maximum -/// -/// If input is less than min then min is returned, if input is greater than max then max is -/// returned. Otherwise input is returned. -#[inline] -pub fn clamp(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} -} diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 341d1f4..49904c4 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -241,6 +241,35 @@ macro_rules! float_trait_impl { } float_trait_impl!(Num for f32 f64); +/// A value bounded by a minimum and a maximum +/// +/// If input is less than min then min is returned, if input is greater than max then max is +/// returned. Otherwise input is returned. +#[inline] +pub fn clamp(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 + } +} + +#[test] +fn clamp_test() { + // Int test + assert_eq!(1, clamp(1, -1, 2)); + assert_eq!(-1, clamp(-2, -1, 2)); + assert_eq!(2, clamp(3, -1, 2)); + + // Float test + assert_eq!(1.0, clamp(1.0, -1.0, 2.0)); + assert_eq!(-1.0, clamp(-2.0, -1.0, 2.0)); + assert_eq!(2.0, clamp(3.0, -1.0, 2.0)); +} + #[test] fn from_str_radix_unwrap() { // The Result error must impl Debug to allow unwrap() From 17665ec50d55fdc1570fcecea4b2b25cb832c49a Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Tue, 7 Feb 2017 15:03:00 -0700 Subject: [PATCH 7/8] Update lib.rs Make assert debug error more accurate. --- traits/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 49904c4..23063fe 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -247,7 +247,7 @@ float_trait_impl!(Num for f32 f64); /// returned. Otherwise input is returned. #[inline] pub fn clamp(input: T, min: T, max: T) -> T { - debug_assert!(min <= max, "min must be less than max"); + debug_assert!(min <= max, "min must be less than or equal to max"); if input < min { min } else if input > max { From c78a48fd35065e4d62e19fac988f5d753ad70727 Mon Sep 17 00:00:00 2001 From: Jacob Kiesel Date: Wed, 8 Feb 2017 15:10:01 -0700 Subject: [PATCH 8/8] Improve documentation comments Remove passive voice from documentation comments. --- traits/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/traits/src/lib.rs b/traits/src/lib.rs index 23063fe..4b4ed9c 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -243,8 +243,9 @@ float_trait_impl!(Num for f32 f64); /// A value bounded by a minimum and a maximum /// -/// If input is less than min then min is returned, if input is greater than max then max is -/// returned. Otherwise input is returned. +/// If input is less than min then this returns min. +/// If input is greater than max then this returns max. +/// Otherwise this returns input. #[inline] pub fn clamp(input: T, min: T, max: T) -> T { debug_assert!(min <= max, "min must be less than or equal to max");