Skip to content

Commit

Permalink
Merge pull request #1799 from CosmWasm/1754-not-impl
Browse files Browse the repository at this point in the history
Implement `Not` for all integers
  • Loading branch information
chipshort committed Jul 27, 2023
2 parents 89f750e + e2a1458 commit 2dfe895
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to

## [Unreleased]

### Added

- cosmwasm-std: Implement `Not` for `Uint{64,128,256}` ([#1799]).

[#1799]: https://github.com/CosmWasm/cosmwasm/pull/1799

### Changed

- cosmwasm-vm: Avoid using loupe for getting the `Module` size in the file
Expand Down
9 changes: 9 additions & 0 deletions packages/std/src/math/int128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,15 @@ mod tests {
assert_eq!(num.i128(), i128::MIN);
}

#[test]
fn int128_not_works() {
assert_eq!(!Int128::new(222), Int128::new(!222));
assert_eq!(!Int128::new(-222), Int128::new(!-222));

assert_eq!(!Int128::MAX, Int128::new(!i128::MAX));
assert_eq!(!Int128::MIN, Int128::new(!i128::MIN));
}

#[test]
fn int128_zero_works() {
let zero = Int128::zero();
Expand Down
12 changes: 12 additions & 0 deletions packages/std/src/math/int256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,18 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn int256_not_works() {
let num = Int256::new([1; 32]);
let a = (!num).to_be_bytes();
assert_eq!(a, [254; 32]);

assert_eq!(!Int256::from(-1234806i128), Int256::from(!-1234806i128));

assert_eq!(!Int256::MAX, Int256::MIN);
assert_eq!(!Int256::MIN, Int256::MAX);
}

#[test]
fn int256_zero_works() {
let zero = Int256::zero();
Expand Down
12 changes: 12 additions & 0 deletions packages/std/src/math/int512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,18 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn int512_not_works() {
let num = Int512::new([1; 64]);
let a = (!num).to_be_bytes();
assert_eq!(a, [254; 64]);

assert_eq!(!Int512::from(-1234806i128), Int512::from(!-1234806i128));

assert_eq!(!Int512::MAX, Int512::MIN);
assert_eq!(!Int512::MIN, Int512::MAX);
}

#[test]
fn int512_zero_works() {
let zero = Int512::zero();
Expand Down
9 changes: 9 additions & 0 deletions packages/std/src/math/int64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,15 @@ mod tests {
assert_eq!(num.i64(), i64::MIN);
}

#[test]
fn int64_not_works() {
assert_eq!(!Int64::new(222), Int64::new(!222));
assert_eq!(!Int64::new(-222), Int64::new(!-222));

assert_eq!(!Int64::MAX, Int64::new(!i64::MAX));
assert_eq!(!Int64::MIN, Int64::new(!i64::MIN));
}

#[test]
fn int64_zero_works() {
let zero = Int64::zero();
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ mod tests {
+ Shr<&'a u32>
+ ShrAssign<u32>
+ ShrAssign<&'a u32>
+ Not<Output = Self>
{
}

Expand Down
17 changes: 17 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::ops::{
Sub, SubAssign,
};
use core::str::FromStr;
use std::ops::Not;

use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
Expand Down Expand Up @@ -515,6 +516,14 @@ impl Rem for Uint128 {
}
forward_ref_binop!(impl Rem, rem for Uint128, Uint128);

impl Not for Uint128 {
type Output = Self;

fn not(self) -> Self::Output {
Self(!self.0)
}
}

impl RemAssign<Uint128> for Uint128 {
fn rem_assign(&mut self, rhs: Uint128) {
*self = *self % rhs;
Expand Down Expand Up @@ -607,6 +616,14 @@ mod tests {
assert_eq!(core::mem::size_of::<Uint128>(), 16);
}

#[test]
fn uint128_not_works() {
assert_eq!(!Uint128::new(1234806), Uint128::new(!1234806));

assert_eq!(!Uint128::MAX, Uint128::new(!u128::MAX));
assert_eq!(!Uint128::MIN, Uint128::new(!u128::MIN));
}

#[test]
fn uint128_zero_works() {
let zero = Uint128::zero();
Expand Down
19 changes: 19 additions & 0 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::str::FromStr;
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::ops::Not;

use crate::errors::{
CheckedMultiplyFractionError, CheckedMultiplyRatioError, ConversionOverflowError,
Expand Down Expand Up @@ -500,6 +501,14 @@ impl Rem for Uint256 {
}
forward_ref_binop!(impl Rem, rem for Uint256, Uint256);

impl Not for Uint256 {
type Output = Self;

fn not(self) -> Self::Output {
Self(!self.0)
}
}

impl RemAssign<Uint256> for Uint256 {
fn rem_assign(&mut self, rhs: Uint256) {
*self = *self % rhs;
Expand Down Expand Up @@ -684,6 +693,16 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn uint256_not_works() {
let num = Uint256::new([1; 32]);
let a = (!num).to_be_bytes();
assert_eq!(a, [254; 32]);

assert_eq!(!Uint256::MAX, Uint256::MIN);
assert_eq!(!Uint256::MIN, Uint256::MAX);
}

#[test]
fn uint256_zero_works() {
let zero = Uint256::zero();
Expand Down
10 changes: 10 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,16 @@ mod tests {
assert_eq!(be_bytes, resulting_bytes);
}

#[test]
fn uint512_not_works() {
let num = Uint512::new([1; 64]);
let a = (!num).to_be_bytes();
assert_eq!(a, [254; 64]);

assert_eq!(!Uint512::MAX, Uint512::MIN);
assert_eq!(!Uint512::MIN, Uint512::MAX);
}

#[test]
fn uint512_zero_works() {
let zero = Uint512::zero();
Expand Down
17 changes: 17 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use core::ops::{
use forward_ref::{forward_ref_binop, forward_ref_op_assign};
use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::ops::Not;

use crate::errors::{
CheckedMultiplyFractionError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
Expand Down Expand Up @@ -406,6 +407,14 @@ impl Rem for Uint64 {
}
forward_ref_binop!(impl Rem, rem for Uint64, Uint64);

impl Not for Uint64 {
type Output = Self;

fn not(self) -> Self::Output {
Self(!self.0)
}
}

impl RemAssign<Uint64> for Uint64 {
fn rem_assign(&mut self, rhs: Uint64) {
*self = *self % rhs;
Expand Down Expand Up @@ -557,6 +566,14 @@ mod tests {
assert_eq!(core::mem::size_of::<Uint64>(), 8);
}

#[test]
fn uint64_not_works() {
assert_eq!(!Uint64::new(1234806), Uint64::new(!1234806));

assert_eq!(!Uint64::MAX, Uint64::new(!u64::MAX));
assert_eq!(!Uint64::MIN, Uint64::new(!u64::MIN));
}

#[test]
fn uint64_zero_works() {
let zero = Uint64::zero();
Expand Down

0 comments on commit 2dfe895

Please sign in to comment.