From 19b5f3390d2f7292d01d83fe9fba11d418d0a793 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Tue, 23 Aug 2022 12:18:48 +0200 Subject: [PATCH] core: add arithmetic operators for 'TxAmount' --- src/core.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/core.rs b/src/core.rs index 25d5578..8e62c1d 100644 --- a/src/core.rs +++ b/src/core.rs @@ -30,3 +30,39 @@ pub struct TxAmount(#[serde_as(as = "DisplayFromStr")] pub Decimal); impl TxAmount { pub const ZERO: Self = Self(Dec!(0)); } + +impl std::ops::Add for TxAmount { + type Output = Self; + + fn add(self, rhs: TxAmount) -> Self::Output { + Self(self.0 + rhs.0) + } +} + +impl std::ops::AddAssign for TxAmount { + fn add_assign(&mut self, rhs: TxAmount) { + *self = *self + rhs; + } +} + +impl std::ops::Sub for TxAmount { + type Output = Self; + + fn sub(self, rhs: TxAmount) -> Self::Output { + Self(self.0 - rhs.0) + } +} + +impl std::ops::SubAssign for TxAmount { + fn sub_assign(&mut self, rhs: TxAmount) { + *self = *self - rhs; + } +} + +impl std::ops::Neg for TxAmount { + type Output = Self; + + fn neg(self) -> Self::Output { + Self(-self.0) + } +}