traiter/numbers/
wrapping_mul.rs

1pub trait WrappingMul<Other = Self> {
2    type Output;
3
4    /// Returns wrapping multiplication.
5    /// ```
6    /// use traiter::numbers::WrappingMul;
7    /// // signed integers
8    /// assert_eq!(WrappingMul::wrapping_mul(i8::MIN, -1i8), i8::MIN);
9    /// assert_eq!(WrappingMul::wrapping_mul(i8::MAX, -1i8), -i8::MAX);
10    /// assert_eq!(WrappingMul::wrapping_mul(i8::MAX, i8::MIN), i8::MIN);
11    /// // unsigned integers
12    /// assert_eq!(WrappingMul::wrapping_mul(u8::MAX, 2u8), 254u8);
13    /// assert_eq!(WrappingMul::wrapping_mul(u8::MAX, 3u8), 253u8);
14    /// assert_eq!(WrappingMul::wrapping_mul(u8::MAX, 4u8), 252u8);
15    /// ```
16    fn wrapping_mul(self, other: Other) -> Self::Output;
17}
18
19macro_rules! integer_wrapping_mul_impl {
20    ($($integer:ty)*) => ($(
21        impl WrappingMul for $integer {
22            type Output = Self;
23
24            #[inline(always)]
25            fn wrapping_mul(self, other: Self) -> Self::Output {
26                <$integer>::wrapping_mul(self, other)
27            }
28        }
29    )*)
30}
31
32integer_wrapping_mul_impl!(
33    i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize
34);