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 @@ -10,6 +10,7 @@ and this project adheres to

- cosmwasm-std: Implement `Decimal{,256}::checked_mul` and
`Decimal{,256}::checked_pow`.
- cosmwasm-std: Implement `Sub`/`SubAssign` for `Uint64`.
- cosmwasm-crypto: Upgrade ed25519-zebra to version 3.

## [1.0.0-beta6] - 2022-03-07
Expand Down
42 changes: 42 additions & 0 deletions packages/std/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,45 @@ pub use uint128::Uint128;
pub use uint256::Uint256;
pub use uint512::Uint512;
pub use uint64::Uint64;

#[cfg(test)]
mod tests {
use super::*;
use std::ops::*;

/// An trait that ensures other traits are implemented for our number types
trait AllImpl<'a>:
Add
+ Add<&'a Self>
+ AddAssign
+ AddAssign<&'a Self>
+ Sub
+ Sub<&'a Self>
+ SubAssign
+ SubAssign<&'a Self>
// Uncomment when implementing Mul/MulAssign for Uint64
// + Mul
// + Mul<&'a Self>
// + MulAssign
// + MulAssign<&'a Self>
+ Div
+ Div<&'a Self>
+ DivAssign
+ DivAssign<&'a Self>
+ Rem
+ Rem<&'a Self>
// Uncomment when implementing RemAssign
// + RemAssign
// + RemAssign<&'a Self>
+ Sized
+ Copy
where
Self: 'a,
{
}

impl AllImpl<'_> for Uint64 {}
impl AllImpl<'_> for Uint128 {}
impl AllImpl<'_> for Uint256 {}
impl AllImpl<'_> for Uint512 {}
Comment on lines +55 to +58
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
57 changes: 38 additions & 19 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use forward_ref::forward_ref_binop;
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt::{self};
use std::ops::{self, Rem};
use std::ops::{self, Rem, Sub, SubAssign};
use std::str::FromStr;

use crate::errors::{DivideByZeroError, OverflowError, OverflowOperation, StdError};
Expand Down Expand Up @@ -266,14 +266,14 @@ impl ops::Sub<Uint128> for Uint128 {
)
}
}
forward_ref_binop!(impl Sub, sub for Uint128, Uint128);

impl<'a> ops::Sub<&'a Uint128> for Uint128 {
type Output = Self;

fn sub(self, rhs: &'a Uint128) -> Self {
self - *rhs
impl SubAssign<Uint128> for Uint128 {
fn sub_assign(&mut self, rhs: Uint128) {
*self = *self - rhs;
}
}
forward_ref_op_assign!(impl SubAssign, sub_assign for Uint128, Uint128);

impl ops::Mul<Uint128> for Uint128 {
type Output = Self;
Expand Down Expand Up @@ -347,18 +347,6 @@ impl<'a> ops::AddAssign<&'a Uint128> for Uint128 {
}
}

impl ops::SubAssign<Uint128> for Uint128 {
fn sub_assign(&mut self, rhs: Uint128) {
*self = *self - rhs;
}
}

impl<'a> ops::SubAssign<&'a Uint128> for Uint128 {
fn sub_assign(&mut self, rhs: &'a Uint128) {
*self = *self - rhs;
}
}

impl ops::MulAssign<Uint128> for Uint128 {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
Expand Down Expand Up @@ -672,12 +660,43 @@ mod tests {
let _ = almost_max + Uint128(12);
}

#[test]
#[allow(clippy::op_ref)]
fn uint128_sub_works() {
assert_eq!(Uint128(2) - Uint128(1), Uint128(1));
assert_eq!(Uint128(2) - Uint128(0), Uint128(2));
assert_eq!(Uint128(2) - Uint128(2), Uint128(0));

// works for refs
let a = Uint128::new(10);
let b = Uint128::new(3);
let expected = Uint128::new(7);
assert_eq!(a - b, expected);
assert_eq!(a - &b, expected);
assert_eq!(&a - b, expected);
assert_eq!(&a - &b, expected);
}

#[test]
#[should_panic]
fn uint128_sub_overflow_panics() {
let _ = Uint128(1) - Uint128(2);
}

#[test]
fn uint128_sub_assign_works() {
let mut a = Uint128(14);
a -= Uint128(2);
assert_eq!(a, Uint128(12));

// works for refs
let mut a = Uint128::new(10);
let b = Uint128::new(3);
let expected = Uint128::new(7);
a -= &b;
assert_eq!(a, expected);
}

#[test]
fn uint128_multiply_ratio_works() {
let base = Uint128(500);
Expand Down
66 changes: 47 additions & 19 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use forward_ref::forward_ref_binop;
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::{self, Rem, Shl, Shr};
use std::ops::{self, Rem, Shl, Shr, Sub, SubAssign};
use std::str::FromStr;

use crate::errors::{
Expand Down Expand Up @@ -405,14 +405,14 @@ impl ops::Sub<Uint256> for Uint256 {
)
}
}
forward_ref_binop!(impl Sub, sub for Uint256, Uint256);

impl<'a> ops::Sub<&'a Uint256> for Uint256 {
type Output = Self;

fn sub(self, rhs: &'a Uint256) -> Self {
self - *rhs
impl SubAssign<Uint256> for Uint256 {
fn sub_assign(&mut self, rhs: Uint256) {
*self = *self - rhs;
}
}
forward_ref_op_assign!(impl SubAssign, sub_assign for Uint256, Uint256);

impl ops::Div<Uint256> for Uint256 {
type Output = Self;
Expand Down Expand Up @@ -521,18 +521,6 @@ impl<'a> ops::AddAssign<&'a Uint256> for Uint256 {
}
}

impl ops::SubAssign<Uint256> for Uint256 {
fn sub_assign(&mut self, rhs: Uint256) {
*self = *self - rhs;
}
}

impl<'a> ops::SubAssign<&'a Uint256> for Uint256 {
fn sub_assign(&mut self, rhs: &'a Uint256) {
*self = *self - rhs;
}
}

impl ops::MulAssign<Uint256> for Uint256 {
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
Expand Down Expand Up @@ -1221,12 +1209,52 @@ mod tests {
let _ = max + Uint256::from(12u32);
}

#[test]
#[allow(clippy::op_ref)]
fn uint256_sub_works() {
assert_eq!(
Uint256::from(2u32) - Uint256::from(1u32),
Uint256::from(1u32)
);
assert_eq!(
Uint256::from(2u32) - Uint256::from(0u32),
Uint256::from(2u32)
);
assert_eq!(
Uint256::from(2u32) - Uint256::from(2u32),
Uint256::from(0u32)
);

// works for refs
let a = Uint256::from(10u32);
let b = Uint256::from(3u32);
let expected = Uint256::from(7u32);
assert_eq!(a - b, expected);
assert_eq!(a - &b, expected);
assert_eq!(&a - b, expected);
assert_eq!(&a - &b, expected);
}

#[test]
#[should_panic]
fn uint256_sub_overflow_panics() {
let _ = Uint256::from(1u32) - Uint256::from(2u32);
}

#[test]
fn uint256_sub_assign_works() {
let mut a = Uint256::from(14u32);
a -= Uint256::from(2u32);
assert_eq!(a, Uint256::from(12u32));

// works for refs
let mut a = Uint256::from(10u32);
let b = Uint256::from(3u32);
let expected = Uint256::from(7u32);
a -= &b;
assert_eq!(a, expected);
}

#[test]
fn uint256_multiply_ratio_works() {
let base = Uint256::from(500u32);
Expand Down
68 changes: 48 additions & 20 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use forward_ref::forward_ref_binop;
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::{self, Rem, Shr};
use std::ops::{self, Rem, Shr, Sub, SubAssign};
use std::str::FromStr;

use crate::errors::{
Expand Down Expand Up @@ -493,21 +493,21 @@ impl<'a> ops::Add<&'a Uint512> for Uint512 {
}
}

impl ops::Sub<Uint512> for Uint512 {
impl Sub<Uint512> for Uint512 {
type Output = Self;

fn sub(self, rhs: Self) -> Self {
Uint512(self.0.checked_sub(rhs.0).unwrap())
}
}
forward_ref_binop!(impl Sub, sub for Uint512, Uint512);

impl<'a> ops::Sub<&'a Uint512> for Uint512 {
type Output = Self;

fn sub(self, rhs: &'a Uint512) -> Self {
Uint512(self.0.checked_sub(rhs.0).unwrap())
impl ops::SubAssign<Uint512> for Uint512 {
fn sub_assign(&mut self, rhs: Uint512) {
self.0 = self.0.checked_sub(rhs.0).unwrap();
}
}
forward_ref_op_assign!(impl SubAssign, sub_assign for Uint512, Uint512);

impl ops::Div<Uint512> for Uint512 {
type Output = Self;
Expand Down Expand Up @@ -587,18 +587,6 @@ impl<'a> ops::AddAssign<&'a Uint512> for Uint512 {
}
}

impl ops::SubAssign<Uint512> for Uint512 {
fn sub_assign(&mut self, rhs: Uint512) {
self.0 = self.0.checked_sub(rhs.0).unwrap();
}
}

impl<'a> ops::SubAssign<&'a Uint512> for Uint512 {
fn sub_assign(&mut self, rhs: &'a Uint512) {
self.0 = self.0.checked_sub(rhs.0).unwrap();
}
}

impl ops::DivAssign<Uint512> for Uint512 {
fn div_assign(&mut self, rhs: Self) {
self.0 = self.0.checked_div(rhs.0).unwrap();
Expand Down Expand Up @@ -993,12 +981,52 @@ mod tests {
let _ = max + Uint512::from(12u32);
}

#[test]
#[allow(clippy::op_ref)]
fn uint512_sub_works() {
assert_eq!(
Uint512::from(2u32) - Uint512::from(1u32),
Uint512::from(1u32)
);
assert_eq!(
Uint512::from(2u32) - Uint512::from(0u32),
Uint512::from(2u32)
);
assert_eq!(
Uint512::from(2u32) - Uint512::from(2u32),
Uint512::from(0u32)
);

// works for refs
let a = Uint512::from(10u32);
let b = Uint512::from(3u32);
let expected = Uint512::from(7u32);
assert_eq!(a - b, expected);
assert_eq!(a - &b, expected);
assert_eq!(&a - b, expected);
assert_eq!(&a - &b, expected);
}

#[test]
#[should_panic]
fn uint512_sub_overflow_panics() {
let _ = Uint512::from(1u32) - Uint512::from(2u32);
}

#[test]
fn uint512_sub_assign_works() {
let mut a = Uint512::from(14u32);
a -= Uint512::from(2u32);
assert_eq!(a, Uint512::from(12u32));

// works for refs
let mut a = Uint512::from(10u32);
let b = Uint512::from(3u32);
let expected = Uint512::from(7u32);
a -= &b;
assert_eq!(a, expected);
}

#[test]
fn uint512_shr_works() {
let original = Uint512::new([
Expand Down
Loading