humanize_rs/num/
checked.rs

1use std::ops::Mul;
2
3// Performs multiplication that returns `None` instead of wrapping around on underflow or
4/// overflow.
5pub trait CheckedMul: Copy + Sized + Mul<Self, Output = Self> {
6    /// Multiplies two numbers, checking for underflow or overflow. If underflow
7    /// or overflow happens, `None` is returned.
8    fn checked_mul(self, rhs: Self) -> Option<Self>;
9}
10
11macro_rules! impl_checked_mul {
12    ($T:ty) => {
13        impl CheckedMul for $T {
14            fn checked_mul(self, rhs: $T) -> Option<$T> {
15                <$T>::checked_mul(self, rhs)
16            }
17        }
18    };
19}
20
21impl_checked_mul!(i8);
22impl_checked_mul!(u8);
23impl_checked_mul!(i16);
24impl_checked_mul!(u16);
25impl_checked_mul!(i32);
26impl_checked_mul!(u32);
27impl_checked_mul!(i64);
28impl_checked_mul!(u64);
29impl_checked_mul!(isize);
30impl_checked_mul!(usize);
31
32#[cfg(has_i128)]
33impl_checked_mul!(i128);
34#[cfg(has_i128)]
35impl_checked_mul!(u128);