mod add;
mod div;
mod mul;
mod sub;
use crate::inner::float::VarFloat;
use crate::VarUInt;
use std::ops::{Add, Div, Mul, Sub};
macro_rules! impl_var_float {
($($trait:ident: $method:tt($op:tt)),*) => {
$(
impl $trait for VarFloat {
type Output = VarFloat;
#[inline]
#[deny(arithmetic_overflow)]
fn $method(self, rhs: Self) -> Self::Output {
match Self::normalize(self, rhs) {
(VarFloat::F32(lhs), VarFloat::F32(rhs)) => VarFloat::F32(lhs $op rhs),
(VarFloat::F64(lhs), VarFloat::F64(rhs)) => VarFloat::F64(lhs $op rhs),
(lhs, rhs) => panic!("normalization failed: {} {} {}", lhs, stringify!($op), rhs),
}
}
}
)*
};
}
macro_rules! impl_var_uint {
($($trait:ident: $method:tt($op:tt)),*) => {
$(
impl $trait for VarUInt {
type Output = VarUInt;
#[inline]
#[deny(arithmetic_overflow)]
fn $method(self, rhs: Self) -> Self::Output {
match Self::normalize(self, rhs) {
(VarUInt::U8(lhs), VarUInt::U8(rhs)) => VarUInt::U8(lhs $op rhs),
(VarUInt::U16(lhs), VarUInt::U16(rhs)) => VarUInt::U16(lhs $op rhs),
(VarUInt::U32(lhs), VarUInt::U32(rhs)) => VarUInt::U32(lhs $op rhs),
(VarUInt::U64(lhs), VarUInt::U64(rhs)) => VarUInt::U64(lhs $op rhs),
(VarUInt::U128(lhs), VarUInt::U128(rhs)) => VarUInt::U128(lhs $op rhs),
(lhs, rhs) => panic!("normalization failed: {:?} {} {:?}", lhs, stringify!($op), rhs),
}
}
}
)*
};
}
impl_var_float! {
Add: add(+), Sub: sub(-), Mul: mul(*), Div: div(/)
}
impl_var_uint! {
Add: add(+), Sub: sub(-), Mul: mul(*), Div: div(/)
}