Skip to content

Implement PartialEq for references on all math types #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 15, 2022
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 @@ -17,6 +17,8 @@ and this project adheres to
`Decimal`/`Decimal256`.
- cosmwasm-std: Implement `pow`/`saturating_pow` for `Decimal`/`Decimal256`.
- cosmwasm-std: Implement `ceil`/`floor` for `Decimal`/`Decimal256`.
- cosmwasm-std: Implement `PartialEq` for reference on one side and owned value
on the other for all `Uint` and `Decimal` types
- cosmwasm-std: Implement `saturating_add`/`sub`/`mul` for
`Decimal`/`Decimal256`.
- cosmwasm-std: Implement `MIN` const value for all `Uint` and `Decimal` types
Expand Down
36 changes: 34 additions & 2 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl Decimal {
/// Rounds value up after decimal places. Returns OverflowError on overflow.
pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError> {
let floor = self.floor();
if &floor == self {
if floor == self {
Ok(floor)
} else {
floor
Expand Down Expand Up @@ -647,6 +647,18 @@ impl<'de> de::Visitor<'de> for DecimalVisitor {
}
}

impl PartialEq<&Decimal> for Decimal {
fn eq(&self, rhs: &&Decimal) -> bool {
self == *rhs
}
}
Copy link
Member

@webmaster128 webmaster128 Jul 11, 2022

Choose a reason for hiding this comment

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

Ah, can we implement that using forward_ref_binop! too? That should give us 3 implementations: reference left, right and both

Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't seem so. forward_ref_binop! looks for an Output associated type (like when you're trying to do this for Add), but PartialEq doesn't have such a type.

Copy link
Contributor

@uint uint Aug 15, 2022

Choose a reason for hiding this comment

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

We also don't need an implementation for refs on both sides. That case is taken care of by deref coercion.

We're only missing one for LHS, like:

impl PartialEq<Decimal> for &Decimal {

Copy link
Member

Choose a reason for hiding this comment

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

Ah, good to know, thanks @uint. Can you add tests for all combinations and the missing implementation (maybe as PR into this PR)?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure thing. Let's get 1.1.0 wrapped up.


impl PartialEq<Decimal> for &Decimal {
fn eq(&self, rhs: &Decimal) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1750,7 +1762,7 @@ mod tests {
);

let empty: Vec<Decimal> = vec![];
assert_eq!(Decimal::zero(), empty.iter().sum());
assert_eq!(Decimal::zero(), empty.iter().sum::<Decimal>());
}

#[test]
Expand Down Expand Up @@ -1983,4 +1995,24 @@ mod tests {
Err(RoundUpOverflowError { .. })
));
}

#[test]
fn decimal_partial_eq() {
let test_cases = [
("1", "1", true),
("0.5", "0.5", true),
("0.5", "0.51", false),
("0", "0.00000", true),
]
.into_iter()
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
36 changes: 34 additions & 2 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl Decimal256 {
/// Rounds value up after decimal places. Returns OverflowError on overflow.
pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError> {
let floor = self.floor();
if &floor == self {
if floor == self {
Ok(floor)
} else {
floor
Expand Down Expand Up @@ -672,6 +672,18 @@ impl<'de> de::Visitor<'de> for Decimal256Visitor {
}
}

impl PartialEq<&Decimal256> for Decimal256 {
fn eq(&self, rhs: &&Decimal256) -> bool {
self == *rhs
}
}

impl PartialEq<Decimal256> for &Decimal256 {
fn eq(&self, rhs: &Decimal256) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1885,7 +1897,7 @@ mod tests {
);

let empty: Vec<Decimal256> = vec![];
assert_eq!(Decimal256::zero(), empty.iter().sum());
assert_eq!(Decimal256::zero(), empty.iter().sum::<Decimal256>());
}

#[test]
Expand Down Expand Up @@ -2130,4 +2142,24 @@ mod tests {
);
assert_eq!(Decimal256::MAX.checked_ceil(), Err(RoundUpOverflowError));
}

#[test]
fn decimal256_partial_eq() {
let test_cases = [
("1", "1", true),
("0.5", "0.5", true),
("0.5", "0.51", false),
("0", "0.00000", true),
]
.into_iter()
.map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
31 changes: 29 additions & 2 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,18 @@ where
}
}

impl PartialEq<&Uint128> for Uint128 {
fn eq(&self, rhs: &&Uint128) -> bool {
self == *rhs
}
}

impl PartialEq<Uint128> for &Uint128 {
fn eq(&self, rhs: &Uint128) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -854,10 +866,10 @@ mod tests {
let nums = vec![Uint128(17), Uint128(123), Uint128(540), Uint128(82)];
let expected = Uint128(762);

let sum_as_ref = nums.iter().sum();
let sum_as_ref: Uint128 = nums.iter().sum();
assert_eq!(expected, sum_as_ref);

let sum_as_owned = nums.into_iter().sum();
let sum_as_owned: Uint128 = nums.into_iter().sum();
assert_eq!(expected, sum_as_owned);
}

Expand Down Expand Up @@ -984,4 +996,19 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint128_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| (Uint128::new(lhs), Uint128::new(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
33 changes: 31 additions & 2 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,18 @@ where
}
}

impl PartialEq<&Uint256> for Uint256 {
fn eq(&self, rhs: &&Uint256) -> bool {
self == *rhs
}
}

impl PartialEq<Uint256> for &Uint256 {
fn eq(&self, rhs: &Uint256) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1410,10 +1422,10 @@ mod tests {
];
let expected = Uint256::from(762u32);

let sum_as_ref = nums.iter().sum();
let sum_as_ref: Uint256 = nums.iter().sum();
assert_eq!(expected, sum_as_ref);

let sum_as_owned = nums.into_iter().sum();
let sum_as_owned: Uint256 = nums.into_iter().sum();
assert_eq!(expected, sum_as_owned);
}

Expand Down Expand Up @@ -1563,4 +1575,21 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint256_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected): (u64, u64, bool)| {
(Uint256::from(lhs), Uint256::from(rhs), expected)
});

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
33 changes: 31 additions & 2 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@ where
}
}

impl PartialEq<&Uint512> for Uint512 {
fn eq(&self, rhs: &&Uint512) -> bool {
self == *rhs
}
}

impl PartialEq<Uint512> for &Uint512 {
fn eq(&self, rhs: &Uint512) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -1045,10 +1057,10 @@ mod tests {
];
let expected = Uint512::from(762u32);

let sum_as_ref = nums.iter().sum();
let sum_as_ref: Uint512 = nums.iter().sum();
assert_eq!(expected, sum_as_ref);

let sum_as_owned = nums.into_iter().sum();
let sum_as_owned: Uint512 = nums.into_iter().sum();
assert_eq!(expected, sum_as_owned);
}

Expand Down Expand Up @@ -1198,4 +1210,21 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint512_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected): (u64, u64, bool)| {
(Uint512::from(lhs), Uint512::from(rhs), expected)
});

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}
31 changes: 29 additions & 2 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ where
}
}

impl PartialEq<&Uint64> for Uint64 {
fn eq(&self, rhs: &&Uint64) -> bool {
self == *rhs
}
}

impl PartialEq<Uint64> for &Uint64 {
fn eq(&self, rhs: &Uint64) -> bool {
*self == rhs
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -769,10 +781,10 @@ mod tests {
let nums = vec![Uint64(17), Uint64(123), Uint64(540), Uint64(82)];
let expected = Uint64(762);

let sum_as_ref = nums.iter().sum();
let sum_as_ref: Uint64 = nums.iter().sum();
assert_eq!(expected, sum_as_ref);

let sum_as_owned = nums.into_iter().sum();
let sum_as_owned: Uint64 = nums.into_iter().sum();
assert_eq!(expected, sum_as_owned);
}

Expand Down Expand Up @@ -897,4 +909,19 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
fn uint64_partial_eq() {
let test_cases = [(1, 1, true), (42, 42, true), (42, 24, false), (0, 0, true)]
.into_iter()
.map(|(lhs, rhs, expected)| (Uint64::new(lhs), Uint64::new(rhs), expected));

#[allow(clippy::op_ref)]
for (lhs, rhs, expected) in test_cases {
assert_eq!(lhs == rhs, expected);
assert_eq!(&lhs == rhs, expected);
assert_eq!(lhs == &rhs, expected);
assert_eq!(&lhs == &rhs, expected);
}
}
}