units: pull generic op impls on NumOpResult into macro
This commit is contained in:
parent
21ac5aefe0
commit
353c23fa01
|
@ -169,23 +169,6 @@ crate::internal_macros::impl_op_for_references! {
|
||||||
fn rem(self, modulus: u64) -> Self::Output { self.checked_rem(modulus).valid_or_error() }
|
fn rem(self, modulus: u64) -> Self::Output { self.checked_rem(modulus).valid_or_error() }
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME these two should be covered by generic impls below
|
|
||||||
impl ops::Add<Amount> for NumOpResult<Amount> {
|
|
||||||
type Output = NumOpResult<Amount>;
|
|
||||||
|
|
||||||
fn add(self, rhs: Amount) -> Self::Output { rhs + self }
|
|
||||||
}
|
|
||||||
impl ops::Sub<Amount> for NumOpResult<Amount> {
|
|
||||||
type Output = NumOpResult<Amount>;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Amount) -> Self::Output {
|
|
||||||
match self {
|
|
||||||
R::Valid(amount) => amount - rhs,
|
|
||||||
R::Error(_) => self,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::Add<SignedAmount> for SignedAmount {
|
impl ops::Add<SignedAmount> for SignedAmount {
|
||||||
type Output = NumOpResult<SignedAmount>;
|
type Output = NumOpResult<SignedAmount>;
|
||||||
|
|
||||||
|
@ -208,29 +191,12 @@ crate::internal_macros::impl_op_for_references! {
|
||||||
|
|
||||||
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output {
|
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output {
|
||||||
match rhs {
|
match rhs {
|
||||||
R::Valid(amount) => amount - rhs,
|
R::Valid(amount) => self - amount,
|
||||||
R::Error(_) => rhs,
|
R::Error(_) => rhs,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ops::Add<SignedAmount> for NumOpResult<SignedAmount> {
|
|
||||||
type Output = NumOpResult<SignedAmount>;
|
|
||||||
|
|
||||||
fn add(self, rhs: SignedAmount) -> Self::Output { rhs + self }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::Sub<SignedAmount> for NumOpResult<SignedAmount> {
|
|
||||||
type Output = NumOpResult<SignedAmount>;
|
|
||||||
|
|
||||||
fn sub(self, rhs: SignedAmount) -> Self::Output {
|
|
||||||
match self {
|
|
||||||
R::Valid(amount) => amount - rhs,
|
|
||||||
R::Error(_) => self,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::Mul<i64> for SignedAmount {
|
impl ops::Mul<i64> for SignedAmount {
|
||||||
type Output = NumOpResult<SignedAmount>;
|
type Output = NumOpResult<SignedAmount>;
|
||||||
|
|
||||||
|
@ -260,18 +226,20 @@ crate::internal_macros::impl_op_for_references! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ops::Neg for SignedAmount {
|
impl<T> ops::Add<T> for NumOpResult<T>
|
||||||
type Output = Self;
|
where
|
||||||
|
(T: Copy + ops::Add<NumOpResult<T>, Output = NumOpResult<T>>)
|
||||||
|
{
|
||||||
|
type Output = NumOpResult<T>;
|
||||||
|
|
||||||
fn neg(self) -> Self::Output { Self::from_sat(self.to_sat().neg()) }
|
fn add(self, rhs: T) -> Self::Output { rhs + self }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ops::Sub for NumOpResult<T>
|
impl<T> ops::Sub<NumOpResult<T>> for NumOpResult<T>
|
||||||
where
|
where
|
||||||
T: ops::Sub<Output = NumOpResult<T>>,
|
(T: Copy + ops::Sub<Output = NumOpResult<T>>)
|
||||||
{
|
{
|
||||||
type Output = NumOpResult<T>;
|
type Output = NumOpResult<T>;
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
fn sub(self, rhs: Self) -> Self::Output {
|
||||||
|
@ -280,45 +248,27 @@ where
|
||||||
(_, _) => R::Error(NumOpError {}),
|
(_, _) => R::Error(NumOpError {}),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T> ops::Sub<NumOpResult<T>> for &NumOpResult<T>
|
|
||||||
where
|
impl<T> ops::Sub<T> for NumOpResult<T>
|
||||||
T: ops::Sub<Output = NumOpResult<T>> + Copy,
|
where
|
||||||
{
|
(T: Copy + ops::Sub<Output = NumOpResult<T>>)
|
||||||
|
{
|
||||||
type Output = NumOpResult<T>;
|
type Output = NumOpResult<T>;
|
||||||
|
|
||||||
fn sub(self, rhs: NumOpResult<T>) -> Self::Output {
|
fn sub(self, rhs: T) -> Self::Output {
|
||||||
match (self, rhs) {
|
match self {
|
||||||
(R::Valid(lhs), R::Valid(rhs)) => *lhs - rhs,
|
R::Valid(amount) => amount - rhs,
|
||||||
(_, _) => R::Error(NumOpError {}),
|
R::Error(_) => self,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T> ops::Sub<&NumOpResult<T>> for NumOpResult<T>
|
|
||||||
where
|
|
||||||
T: ops::Sub<Output = NumOpResult<T>> + Copy,
|
|
||||||
{
|
|
||||||
type Output = NumOpResult<T>;
|
|
||||||
|
|
||||||
fn sub(self, rhs: &NumOpResult<T>) -> Self::Output {
|
impl ops::Neg for SignedAmount {
|
||||||
match (self, rhs) {
|
type Output = Self;
|
||||||
(R::Valid(lhs), R::Valid(rhs)) => lhs - *rhs,
|
|
||||||
(_, _) => R::Error(NumOpError {}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl<T> ops::Sub for &NumOpResult<T>
|
|
||||||
where
|
|
||||||
T: ops::Sub<Output = NumOpResult<T>> + Copy,
|
|
||||||
{
|
|
||||||
type Output = NumOpResult<T>;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
fn neg(self) -> Self::Output { Self::from_sat(self.to_sat().neg()) }
|
||||||
match (self, rhs) {
|
|
||||||
(R::Valid(lhs), R::Valid(rhs)) => *lhs - *rhs,
|
|
||||||
(_, _) => R::Error(NumOpError {}),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl core::iter::Sum<NumOpResult<Amount>> for NumOpResult<Amount> {
|
impl core::iter::Sum<NumOpResult<Amount>> for NumOpResult<Amount> {
|
||||||
|
|
Loading…
Reference in New Issue