源码
use std::ops::Add;
use super::constant::{Constant, NotZeroConstant};
use super::variable::{Variable, FloatType};
use super::merge::Merge;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Adder<const N: i32, T: FloatType>(pub Constant<N>, pub Variable<T>)
where
Constant<N>: NotZeroConstant;
impl<const N: i32, T: FloatType> Merge for Adder<N, T>
where
Constant<N>: NotZeroConstant,
T: From<i32> + Add<Output = T>,
{
type Output = Variable<T>;
#[inline]
fn merge(self) -> Self::Output {
Variable(T::from(N) + self.1.0)
}
}
impl<const N: i32, T: FloatType, const B: i32> Add<Constant<B>> for Adder<N, T>
where
Constant<N>: NotZeroConstant,
Constant<{ N + B }>: Add<Variable<T>>,
{
type Output = <Constant<{ N + B }> as Add<Variable<T>>>::Output;
#[inline]
fn add(self, _b: Constant<B>) -> Self::Output {
Constant::<{ N + B }> + self.1
}
}
impl<const N: i32, T: FloatType> Add<Variable<T>> for Adder<N, T>
where
Constant<N>: NotZeroConstant,
T: Add<T, Output = T>,
Variable<T>: Add<Variable<T>, Output = Variable<T>>,
{
type Output = Self;
#[inline]
fn add(self, b: Variable<T>) -> Self::Output {
Adder(self.0, self.1 + b)
}
}