use crate::inner::VarInt;
use std::ops::Mul;
impl Mul for VarInt {
type Output = VarInt;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let (lhs, rhs) = VarInt::normalize(self, rhs);
if lhs.is_zero() || rhs.is_zero() {
return VarInt::I8(0);
}
if lhs.is_one() {
return rhs;
}
if rhs.is_one() {
return lhs;
}
match (lhs, rhs) {
(VarInt::I8(a), VarInt::I8(b)) => match a.checked_mul(b) {
Some(v) => VarInt::I8(v),
None => VarInt::I16(a as i16 * b as i16),
},
(VarInt::I16(a), VarInt::I16(b)) => match a.checked_mul(b) {
Some(v) => VarInt::I16(v),
None => VarInt::I32(a as i32 * b as i32),
},
(VarInt::I32(a), VarInt::I32(b)) => match a.checked_mul(b) {
Some(v) => VarInt::I32(v),
None => VarInt::I64(a as i64 * b as i64),
},
(VarInt::I64(a), VarInt::I64(b)) => match a.checked_mul(b) {
Some(v) => VarInt::I64(v),
None => VarInt::I128(a as i128 * b as i128),
},
(VarInt::I128(a), VarInt::I128(b)) => match a.checked_mul(b) {
Some(v) => VarInt::I128(v),
None => panic!("overflow: {:?} * {:?}", a, b),
},
(lhs, rhs) => panic!("normalization failed: {:?} * {:?}", lhs, rhs),
}
}
}