diff --git a/CHANGELOG.md b/CHANGELOG.md index c8e4be7f25..03d3e50af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ and this project adheres to - 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-std: Implement `is_zero`/`atomics`/`decimal_places` as const for Uint + and Decimal types. - cosmwasm-crypto: Upgrade ed25519-zebra to version 3. ### Changed diff --git a/packages/std/src/math/decimal.rs b/packages/std/src/math/decimal.rs index c7d78c97eb..d5522db999 100644 --- a/packages/std/src/math/decimal.rs +++ b/packages/std/src/math/decimal.rs @@ -119,7 +119,7 @@ impl Decimal { ) } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -141,7 +141,7 @@ impl Decimal { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint128::new(1)); /// ``` - pub fn atomics(&self) -> Uint128 { + pub const fn atomics(&self) -> Uint128 { self.0 } @@ -149,7 +149,7 @@ impl Decimal { /// but this could potentially change as the type evolves. /// /// See also [`Decimal::atomics()`]. - pub fn decimal_places(&self) -> u32 { + pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES as u32 } diff --git a/packages/std/src/math/decimal256.rs b/packages/std/src/math/decimal256.rs index 98c5059214..a71d29fdd8 100644 --- a/packages/std/src/math/decimal256.rs +++ b/packages/std/src/math/decimal256.rs @@ -131,7 +131,7 @@ impl Decimal256 { ) } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0.is_zero() } @@ -153,7 +153,7 @@ impl Decimal256 { /// assert_eq!(b.decimal_places(), 18); /// assert_eq!(b.atomics(), Uint256::from(1u128)); /// ``` - pub fn atomics(&self) -> Uint256 { + pub const fn atomics(&self) -> Uint256 { self.0 } @@ -161,7 +161,7 @@ impl Decimal256 { /// but this could potentially change as the type evolves. /// /// See also [`Decimal256::atomics()`]. - pub fn decimal_places(&self) -> u32 { + pub const fn decimal_places(&self) -> u32 { Self::DECIMAL_PLACES as u32 } diff --git a/packages/std/src/math/uint128.rs b/packages/std/src/math/uint128.rs index 2cde35fdd2..1cf031e52a 100644 --- a/packages/std/src/math/uint128.rs +++ b/packages/std/src/math/uint128.rs @@ -63,7 +63,7 @@ impl Uint128 { self.0.to_le_bytes() } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0 == 0 } diff --git a/packages/std/src/math/uint256.rs b/packages/std/src/math/uint256.rs index 7b506b1f23..ced2407874 100644 --- a/packages/std/src/math/uint256.rs +++ b/packages/std/src/math/uint256.rs @@ -206,8 +206,9 @@ impl Uint256 { ] } - pub fn is_zero(&self) -> bool { - self.0.is_zero() + pub const fn is_zero(&self) -> bool { + let words = (self.0).0; + words[0] == 0 && words[1] == 0 && words[2] == 0 && words[3] == 0 } pub fn pow(self, exp: u32) -> Self { diff --git a/packages/std/src/math/uint512.rs b/packages/std/src/math/uint512.rs index 7c7aa0fb52..42aba05b62 100644 --- a/packages/std/src/math/uint512.rs +++ b/packages/std/src/math/uint512.rs @@ -58,7 +58,7 @@ impl Uint512 { /// Creates a Uint512(value) from a big endian representation. It's just an alias for /// `from_big_endian`. - pub fn new(value: [u8; 64]) -> Self { + pub const fn new(value: [u8; 64]) -> Self { Self::from_be_bytes(value) } @@ -305,8 +305,16 @@ impl Uint512 { ] } - pub fn is_zero(&self) -> bool { - self.0.is_zero() + pub const fn is_zero(&self) -> bool { + let words = (self.0).0; + words[0] == 0 + && words[1] == 0 + && words[2] == 0 + && words[3] == 0 + && words[4] == 0 + && words[5] == 0 + && words[6] == 0 + && words[7] == 0 } pub fn pow(self, exp: u32) -> Self { diff --git a/packages/std/src/math/uint64.rs b/packages/std/src/math/uint64.rs index a719d90613..8ed7759b19 100644 --- a/packages/std/src/math/uint64.rs +++ b/packages/std/src/math/uint64.rs @@ -59,7 +59,7 @@ impl Uint64 { self.0.to_le_bytes() } - pub fn is_zero(&self) -> bool { + pub const fn is_zero(&self) -> bool { self.0 == 0 }