units: pull generic op impls on NumOpResult into macro

This commit is contained in:
Andrew Poelstra 2025-02-18 18:29:02 +00:00
parent 21ac5aefe0
commit 353c23fa01
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 38 additions and 88 deletions

View File

@ -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() }
}
// 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 {
type Output = NumOpResult<SignedAmount>;
@ -208,29 +191,12 @@ crate::internal_macros::impl_op_for_references! {
fn sub(self, rhs: NumOpResult<SignedAmount>) -> Self::Output {
match rhs {
R::Valid(amount) => amount - rhs,
R::Valid(amount) => self - amount,
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 {
type Output = NumOpResult<SignedAmount>;
@ -260,6 +226,43 @@ crate::internal_macros::impl_op_for_references! {
}
}
}
impl<T> ops::Add<T> for NumOpResult<T>
where
(T: Copy + ops::Add<NumOpResult<T>, Output = NumOpResult<T>>)
{
type Output = NumOpResult<T>;
fn add(self, rhs: T) -> Self::Output { rhs + self }
}
impl<T> ops::Sub<NumOpResult<T>> for NumOpResult<T>
where
(T: Copy + ops::Sub<Output = NumOpResult<T>>)
{
type Output = NumOpResult<T>;
fn sub(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(R::Valid(lhs), R::Valid(rhs)) => lhs - rhs,
(_, _) => R::Error(NumOpError {}),
}
}
}
impl<T> ops::Sub<T> for NumOpResult<T>
where
(T: Copy + ops::Sub<Output = NumOpResult<T>>)
{
type Output = NumOpResult<T>;
fn sub(self, rhs: T) -> Self::Output {
match self {
R::Valid(amount) => amount - rhs,
R::Error(_) => self,
}
}
}
}
impl ops::Neg for SignedAmount {
@ -268,59 +271,6 @@ impl ops::Neg for SignedAmount {
fn neg(self) -> Self::Output { Self::from_sat(self.to_sat().neg()) }
}
impl<T> ops::Sub for NumOpResult<T>
where
T: ops::Sub<Output = NumOpResult<T>>,
{
type Output = NumOpResult<T>;
fn sub(self, rhs: Self) -> Self::Output {
match (self, rhs) {
(R::Valid(lhs), R::Valid(rhs)) => lhs - rhs,
(_, _) => R::Error(NumOpError {}),
}
}
}
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 {
match (self, rhs) {
(R::Valid(lhs), R::Valid(rhs)) => *lhs - rhs,
(_, _) => R::Error(NumOpError {}),
}
}
}
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 {
match (self, rhs) {
(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 {
match (self, rhs) {
(R::Valid(lhs), R::Valid(rhs)) => *lhs - *rhs,
(_, _) => R::Error(NumOpError {}),
}
}
}
impl core::iter::Sum<NumOpResult<Amount>> for NumOpResult<Amount> {
fn sum<I>(iter: I) -> Self
where