use std::ops::{AddAssign, Mul};
pub trait MulAcc<A = Self, B = A> {
fn mul_acc(&mut self, a: &A, b: &B);
}
impl<N, A, B> MulAcc<A, B> for N
where
for<'x> &'x A: Mul<&'x B, Output = N>,
N: AddAssign<N>,
{
fn mul_acc(&mut self, a: &A, b: &B) {
self.add_assign(a * b);
}
}
#[cfg(test)]
mod tests {
use super::MulAcc;
#[test]
fn mul_acc_f64() {
let mut a = 1f64;
let b = 2.;
let c = 3.;
a.mul_acc(&b, &c);
assert_eq!(a, 7.);
}
#[derive(Debug, Copy, Clone, Default)]
struct Wrapped<T: Default + Copy + std::fmt::Debug>(T);
impl MulAcc<Wrapped<i8>, Wrapped<i16>> for Wrapped<i32> {
fn mul_acc(&mut self, a: &Wrapped<i8>, b: &Wrapped<i16>) {
self.0 = self.0 + a.0 as i32 * b.0 as i32;
}
}
#[test]
fn mul_acc_mixed_param_sizes() {
let mut a = Wrapped::<i32>(0x40000007i32);
let b = Wrapped::<i8>(0x20i8);
let c = Wrapped::<i16>(0x3000i16);
a.mul_acc(&b, &c);
assert_eq!(a.0, 0x40060007i32);
}
}