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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to
`Decimal{,256}::checked_pow`.
- cosmwasm-std: Implement `Sub`/`SubAssign` for `Uint64`.
- cosmwasm-std: Implement `Mul`/`MulAssign` for `Uint64`.
- cosmwasm-std: Implement `RemAssign` for
`Uint64`/`Uint128`/`Uint256`/`Uint512`.
- cosmwasm-crypto: Upgrade ed25519-zebra to version 3.

### Changed
Expand Down
4 changes: 2 additions & 2 deletions packages/std/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ mod tests {
+ DivAssign<&'a Self>
+ Rem
+ Rem<&'a Self>
// + RemAssign
// + RemAssign<&'a Self>
+ RemAssign
+ RemAssign<&'a Self>
+ Sized
+ Copy
where
Expand Down
41 changes: 40 additions & 1 deletion packages/std/src/math/uint128.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::convert::{TryFrom, TryInto};
use std::fmt::{self};
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Shr, ShrAssign, Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shr, ShrAssign, Sub, SubAssign,
};
use std::str::FromStr;

Expand Down Expand Up @@ -374,6 +374,13 @@ impl Rem for Uint128 {
}
forward_ref_binop!(impl Rem, rem for Uint128, Uint128);

impl RemAssign<Uint128> for Uint128 {
fn rem_assign(&mut self, rhs: Uint128) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Uint128, Uint128);

impl ShrAssign<u32> for Uint128 {
fn shr_assign(&mut self, rhs: u32) {
*self = *self >> rhs;
Expand Down Expand Up @@ -852,4 +859,36 @@ mod tests {
fn uint128_rem_panics_for_zero() {
let _ = Uint128::new(10) % Uint128::zero();
}

#[test]
#[allow(clippy::op_ref)]
fn uint128_rem_works() {
assert_eq!(
Uint128::from(12u32) % Uint128::from(10u32),
Uint128::from(2u32)
);
assert_eq!(Uint128::from(50u32) % Uint128::from(5u32), Uint128::zero());

// works for refs
let a = Uint128::from(42u32);
let b = Uint128::from(5u32);
let expected = Uint128::from(2u32);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn uint128_rem_assign_works() {
let mut a = Uint128::from(30u32);
a %= Uint128::from(4u32);
assert_eq!(a, Uint128::from(2u32));

// works for refs
let mut a = Uint128::from(25u32);
let b = Uint128::from(6u32);
a %= &b;
assert_eq!(a, Uint128::from(1u32));
}
}
42 changes: 41 additions & 1 deletion packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Shl, Shr, ShrAssign, Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shl, Shr, ShrAssign, Sub,
SubAssign,
};
use std::str::FromStr;

Expand Down Expand Up @@ -449,6 +450,13 @@ impl Rem for Uint256 {
}
forward_ref_binop!(impl Rem, rem for Uint256, Uint256);

impl RemAssign<Uint256> for Uint256 {
fn rem_assign(&mut self, rhs: Uint256) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Uint256, Uint256);

impl Mul<Uint256> for Uint256 {
type Output = Self;

Expand Down Expand Up @@ -1431,4 +1439,36 @@ mod tests {
fn uint256_rem_panics_for_zero() {
let _ = Uint256::from(10u32) % Uint256::zero();
}

#[test]
#[allow(clippy::op_ref)]
fn uint256_rem_works() {
assert_eq!(
Uint256::from(12u32) % Uint256::from(10u32),
Uint256::from(2u32)
);
assert_eq!(Uint256::from(50u32) % Uint256::from(5u32), Uint256::zero());

// works for refs
let a = Uint256::from(42u32);
let b = Uint256::from(5u32);
let expected = Uint256::from(2u32);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn uint256_rem_assign_works() {
let mut a = Uint256::from(30u32);
a %= Uint256::from(4u32);
assert_eq!(a, Uint256::from(2u32));

// works for refs
let mut a = Uint256::from(25u32);
let b = Uint256::from(6u32);
a %= &b;
assert_eq!(a, Uint256::from(1u32));
}
}
41 changes: 40 additions & 1 deletion packages/std/src/math/uint512.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::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Shr, ShrAssign, Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shr, ShrAssign, Sub, SubAssign,
};
use std::str::FromStr;

Expand Down Expand Up @@ -540,6 +540,13 @@ impl Rem for Uint512 {
}
forward_ref_binop!(impl Rem, rem for Uint512, Uint512);

impl RemAssign<Uint512> for Uint512 {
fn rem_assign(&mut self, rhs: Uint512) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Uint512, Uint512);

impl Mul<Uint512> for Uint512 {
type Output = Self;

Expand Down Expand Up @@ -1155,4 +1162,36 @@ mod tests {
fn uint512_rem_panics_for_zero() {
let _ = Uint512::from(10u32) % Uint512::zero();
}

#[test]
#[allow(clippy::op_ref)]
fn uint512_rem_works() {
assert_eq!(
Uint512::from(12u32) % Uint512::from(10u32),
Uint512::from(2u32)
);
assert_eq!(Uint512::from(50u32) % Uint512::from(5u32), Uint512::zero());

// works for refs
let a = Uint512::from(42u32);
let b = Uint512::from(5u32);
let expected = Uint512::from(2u32);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn uint512_rem_assign_works() {
let mut a = Uint512::from(30u32);
a %= Uint512::from(4u32);
assert_eq!(a, Uint512::from(2u32));

// works for refs
let mut a = Uint512::from(25u32);
let b = Uint512::from(6u32);
a %= &b;
assert_eq!(a, Uint512::from(1u32));
}
}
41 changes: 40 additions & 1 deletion packages/std/src/math/uint64.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::convert::{TryFrom, TryInto};
use std::fmt::{self};
use std::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Shr, ShrAssign, Sub, SubAssign,
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Shr, ShrAssign, Sub, SubAssign,
};

use crate::errors::{DivideByZeroError, OverflowError, OverflowOperation, StdError};
Expand Down Expand Up @@ -281,6 +281,13 @@ impl Rem for Uint64 {
}
forward_ref_binop!(impl Rem, rem for Uint64, Uint64);

impl RemAssign<Uint64> for Uint64 {
fn rem_assign(&mut self, rhs: Uint64) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Uint64, Uint64);

impl Shr<u32> for Uint64 {
type Output = Self;

Expand Down Expand Up @@ -761,4 +768,36 @@ mod tests {
fn uint64_rem_panics_for_zero() {
let _ = Uint64::new(10) % Uint64::zero();
}

#[test]
#[allow(clippy::op_ref)]
fn uint64_rem_works() {
assert_eq!(
Uint64::from(12u32) % Uint64::from(10u32),
Uint64::from(2u32)
);
assert_eq!(Uint64::from(50u32) % Uint64::from(5u32), Uint64::zero());

// works for refs
let a = Uint64::from(42u32);
let b = Uint64::from(5u32);
let expected = Uint64::from(2u32);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn uint64_rem_assign_works() {
let mut a = Uint64::from(30u32);
a %= Uint64::from(4u32);
assert_eq!(a, Uint64::from(2u32));

// works for refs
let mut a = Uint64::from(25u32);
let b = Uint64::from(6u32);
a %= &b;
assert_eq!(a, Uint64::from(1u32));
}
}