pub(crate) trait MathExt {
fn in_bucket_of(self, size: Self) -> Self;
}
macro_rules! impl_math_ext {
($($T:ty),*) => {
$(
#[allow(clippy::cast_lossless)]
impl MathExt for $T {
#[inline(always)]
fn in_bucket_of(self, size: Self) -> Self {
if size == 0 {
return 1;
}
self / size + (self % size != 0 || self == 0) as $T
}
}
)*
};
}
impl_math_ext!(u8, u16, u32, u64, usize, i32, i64);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_calculate_proper_bucket() {
assert_eq!(1, 0.in_bucket_of(20));
assert_eq!(1, 7.in_bucket_of(20));
assert_eq!(1, 19.in_bucket_of(20));
assert_eq!(1, 20.in_bucket_of(20));
assert_eq!(2, 21.in_bucket_of(20));
assert_eq!(1, 15.in_bucket_of(15));
assert_eq!(73, 652.in_bucket_of(9));
assert_eq!(9, 89.in_bucket_of(10));
}
}