1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crate::LBNum;
use core::ops;

impl ops::Mul for LBNum {
    type Output = Self;

    /// Multiplies `self` by another `LBNum`. **This will consume `rhs`**.
    #[inline(always)]
    fn mul(mut self, rhs: Self) -> Self {
        self *= rhs;
        self
    }
}
impl ops::MulAssign for LBNum {
    /// Multiplies `self` by another `LBNum` in place. **This will consume `rhs`**.
    #[inline]
    fn mul_assign(&mut self, mut rhs: Self) {
        let mut result = Self::ZERO;
        loop {
            if rhs == Self::ZERO {break;}
            result += self as &Self;
            rhs.decrement();
        }
        *self = result;
    }
}

macro_rules! impl_mul_by_primitive {
    ($ty:ident) => {
        impl ops::Mul<$ty> for LBNum {
            type Output = Self;

            #[inline(always)]
            fn mul(mut self, rhs: $ty) -> Self {
                self *= rhs;
                self
            }
        }
        impl ops::MulAssign<$ty> for LBNum {
            #[inline]
            fn mul_assign(&mut self, mut rhs: $ty) {
                let mut result = Self::ZERO;
                loop {
                    if rhs == 0 {break;}
                    result += self as &Self;
                    rhs -= 1;
                }
                *self = result;
            }
        }
    };
}

impl_mul_by_primitive!(u8   );
impl_mul_by_primitive!(u16  );
impl_mul_by_primitive!(u32  );
impl_mul_by_primitive!(u64  );
impl_mul_by_primitive!(u128 );
impl_mul_by_primitive!(usize);