Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion masp_primitives/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::BTreeMap;
use std::{
io::{self, Write},
iter::Sum,
ops::{Add, AddAssign, Sub, SubAssign},
ops::{Add, AddAssign, Neg, Sub, SubAssign},
};

#[derive(Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -196,6 +196,17 @@ impl SubAssign for AllowedConversion {
}
}

impl Neg for AllowedConversion {
type Output = Self;

fn neg(self) -> Self {
Self {
assets: -self.assets,
generator: -self.generator,
}
}
}

impl Sum for AllowedConversion {
fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
iter.fold(AllowedConversion::from(ValueSum::zero()), Add::add)
Expand Down
21 changes: 16 additions & 5 deletions masp_primitives/src/transaction/components/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,26 @@ where
type Output = ValueSum<Unit, <Lhs as CheckedMul<Rhs>>::Output>;

fn mul(self, rhs: Rhs) -> Self::Output {
self.checked_mul(rhs).expect("overflow detected")
}
}

impl<Unit, Lhs, Rhs> CheckedMul<Rhs> for ValueSum<Unit, Lhs>
where
Unit: Hash + Ord + BorshSerialize + BorshDeserialize + Clone,
Lhs: BorshSerialize + BorshDeserialize + PartialEq + Eq + Copy + Default + CheckedMul<Rhs>,
Rhs: Copy,
<Lhs as CheckedMul<Rhs>>::Output: Default + BorshSerialize + BorshDeserialize + Eq,
{
type Output = ValueSum<Unit, <Lhs as CheckedMul<Rhs>>::Output>;

fn checked_mul(self, rhs: Rhs) -> Option<Self::Output> {
let mut comps = BTreeMap::new();
for (atype, amount) in self.0.iter() {
comps.insert(
atype.clone(),
amount.checked_mul(rhs).expect("overflow detected"),
);
comps.insert(atype.clone(), amount.checked_mul(rhs)?);
}
comps.retain(|_, v| *v != <Lhs as CheckedMul<Rhs>>::Output::default());
ValueSum(comps)
Some(ValueSum(comps))
}
}

Expand Down
Loading