From 63053640f1e5789719b3f88f92b3006ae3caecf4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 13 Sep 2024 18:24:33 +0000 Subject: [PATCH] add util functors for is_zero/is_equal; move clamp to math utils Signed-off-by: Jason Volk --- src/core/utils/math.rs | 35 +++++++++++++++++++++++++++++++++++ src/core/utils/mod.rs | 12 ++---------- src/macros/utils.rs | 7 ++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/src/core/utils/math.rs b/src/core/utils/math.rs index 8c4b01be..215de339 100644 --- a/src/core/utils/math.rs +++ b/src/core/utils/math.rs @@ -53,6 +53,38 @@ macro_rules! validated { ($($input:tt)+) => { $crate::expected!($($input)+) } } +/// Functor for equality to zero +#[macro_export] +macro_rules! is_zero { + () => { + $crate::is_matching!(0) + }; +} + +/// Functor for equality i.e. .is_some_and(is_equal!(2)) +#[macro_export] +macro_rules! is_equal_to { + ($val:expr) => { + |x| (x == $val) + }; +} + +/// Functor for less i.e. .is_some_and(is_less_than!(2)) +#[macro_export] +macro_rules! is_less_than { + ($val:expr) => { + |x| (x < $val) + }; +} + +/// Functor for matches! i.e. .is_some_and(is_matching!('A'..='Z')) +#[macro_export] +macro_rules! is_matching { + ($val:expr) => { + |x| matches!(x, $val) + }; +} + /// Returns false if the exponential backoff has expired based on the inputs #[inline] #[must_use] @@ -118,3 +150,6 @@ fn try_into_err, Src>(e: >::Error) -> Erro type_name::() )) } + +#[inline] +pub fn clamp(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) } diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 29d0b87b..03b755e9 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -14,8 +14,6 @@ pub mod sys; mod tests; pub mod time; -use std::cmp; - pub use ::ctor::{ctor, dtor}; pub use algorithm::common_elements; pub use bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8}; @@ -24,6 +22,7 @@ pub use debug::slice_truncated as debug_slice_truncated; pub use hash::calculate_hash; pub use html::Escape as HtmlEscape; pub use json::{deserialize_from_str, to_canonical_object}; +pub use math::clamp; pub use mutex_map::{Guard as MutexMapGuard, MutexMap}; pub use rand::string as random_string; pub use string::{str_from_bytes, string_from_bytes}; @@ -31,14 +30,7 @@ pub use sys::available_parallelism; pub use time::now_millis as millis_since_unix_epoch; #[inline] -pub fn clamp(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) } - -#[inline] -pub fn exchange(state: &mut T, source: T) -> T { - let ret = state.clone(); - *state = source; - ret -} +pub fn exchange(state: &mut T, source: T) -> T { std::mem::replace(state, source) } #[must_use] pub fn generate_keypair() -> Vec { diff --git a/src/macros/utils.rs b/src/macros/utils.rs index 58074e3a..197dd90e 100644 --- a/src/macros/utils.rs +++ b/src/macros/utils.rs @@ -41,8 +41,5 @@ pub(crate) fn camel_to_snake_string(s: &str) -> String { output } -pub(crate) fn exchange(state: &mut T, source: T) -> T { - let ret = state.clone(); - *state = source; - ret -} +#[inline] +pub(crate) fn exchange(state: &mut T, source: T) -> T { std::mem::replace(state, source) }