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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to
`Uint64`/`Uint128`/`Uint256`/`Uint512`.
- cosmwasm-std: Implement `pow`/`checked_pow` for `Uint64`/`Uint128`/`Uint512`.
- cosmwasm-std: Implement `SubAssign`/`AddAssign` for `Decimal`/`Decimal256`.
- cosmwasm-std: Implement `MulAssign` for `Decimal`/`Decimal256`.
- cosmwasm-crypto: Upgrade ed25519-zebra to version 3.

### Changed
Expand Down
33 changes: 32 additions & 1 deletion packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt::{self, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

Expand Down Expand Up @@ -377,6 +377,14 @@ impl Mul for Decimal {
}
}
}
forward_ref_binop!(impl Mul, mul for Decimal, Decimal);

impl MulAssign for Decimal {
fn mul_assign(&mut self, rhs: Decimal) {
*self = *self * rhs;
}
}
forward_ref_op_assign!(impl MulAssign, mul_assign for Decimal, Decimal);

/// Both d*u and u*d with d: Decimal and u: Uint128 returns an Uint128. There is no
/// specific reason for this decision other than the initial use cases we have. If you
Expand Down Expand Up @@ -958,6 +966,7 @@ mod tests {
}

#[test]
#[allow(clippy::op_ref)]
fn decimal_implements_mul() {
let one = Decimal::one();
let two = one + one;
Expand Down Expand Up @@ -1064,6 +1073,28 @@ mod tests {
max * dec("0.000000000000000001"),
dec("340.282366920938463463")
);

// works for refs
let a = Decimal::percent(20);
let b = Decimal::percent(30);
let expected = Decimal::percent(6);
assert_eq!(a * b, expected);
assert_eq!(&a * b, expected);
assert_eq!(a * &b, expected);
assert_eq!(&a * &b, expected);
}

#[test]
fn decimal_mul_assign_works() {
let mut a = Decimal::percent(15);
a *= Decimal::percent(60);
assert_eq!(a, Decimal::percent(9));

// works for refs
let mut a = Decimal::percent(50);
let b = Decimal::percent(20);
a *= &b;
assert_eq!(a, Decimal::percent(10));
}

#[test]
Expand Down
33 changes: 32 additions & 1 deletion packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::cmp::Ordering;
use std::convert::TryInto;
use std::fmt::{self, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

Expand Down Expand Up @@ -389,6 +389,14 @@ impl Mul for Decimal256 {
}
}
}
forward_ref_binop!(impl Mul, mul for Decimal256, Decimal256);

impl MulAssign for Decimal256 {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
forward_ref_op_assign!(impl MulAssign, mul_assign for Decimal256, Decimal256);

/// Both d*u and u*d with d: Decimal256 and u: Uint256 returns an Uint256. There is no
/// specific reason for this decision other than the initial use cases we have. If you
Expand Down Expand Up @@ -1045,6 +1053,7 @@ mod tests {
}

#[test]
#[allow(clippy::op_ref)]
fn decimal256_implements_mul() {
let one = Decimal256::one();
let two = one + one;
Expand Down Expand Up @@ -1151,6 +1160,28 @@ mod tests {
max * dec("0.000000000000000001"),
dec("115792089237316195423570985008687907853269.984665640564039457")
);

// works for refs
let a = Decimal256::percent(20);
let b = Decimal256::percent(30);
let expected = Decimal256::percent(6);
assert_eq!(a * b, expected);
assert_eq!(&a * b, expected);
assert_eq!(a * &b, expected);
assert_eq!(&a * &b, expected);
}

#[test]
fn decimal256_mul_assign_works() {
let mut a = Decimal256::percent(15);
a *= Decimal256::percent(60);
assert_eq!(a, Decimal256::percent(9));

// works for refs
let mut a = Decimal256::percent(50);
let b = Decimal256::percent(20);
a *= &b;
assert_eq!(a, Decimal256::percent(10));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions packages/std/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ mod tests {
+ SubAssign
+ SubAssign<&'a Self>
+ Mul
// + Mul<&'a Self>
// + MulAssign
// + MulAssign<&'a Self>
+ Mul<&'a Self>
+ MulAssign
+ MulAssign<&'a Self>
// + Div
// + Div<&'a Self>
// + DivAssign
Expand Down