Skip to content

Commit

Permalink
Deprecate PartialEq between Addr and String
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed May 2, 2023
1 parent 721195c commit 08e5f88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to

## [Unreleased]

### Deprecated

- cosmwasm-std: The PartialEq implementations between `Addr` and `&str`/`String`
are deprecated because they are not considered to be safe. In almost all cases
you want to convert both sides of the equation to `Addr` first. If you really
want to do a string comparison, use `Addr::as_str()` explicitly. ([#1671])

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

## [1.2.4] - 2023-04-17

### Fixed
Expand Down
22 changes: 15 additions & 7 deletions packages/std/src/addresses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,39 @@ impl AsRef<str> for Addr {
}

/// Implement `Addr == &str`
///
/// Deprecated. This comparison unsafe. Convert both sides to Addr first.
/// Will be removed soon: https://github.com/CosmWasm/cosmwasm/issues/1669
impl PartialEq<&str> for Addr {
fn eq(&self, rhs: &&str) -> bool {
self.0 == *rhs
}
}

/// Implement `&str == Addr`
///
/// Deprecated. This comparison unsafe. Convert both sides to Addr first.
/// Will be removed soon: https://github.com/CosmWasm/cosmwasm/issues/1669
impl PartialEq<Addr> for &str {
fn eq(&self, rhs: &Addr) -> bool {
*self == rhs.0
}
}

/// Implement `Addr == String`
///
/// Deprecated. This comparison unsafe. Convert both sides to Addr first.
/// Will be removed soon: https://github.com/CosmWasm/cosmwasm/issues/1669
impl PartialEq<String> for Addr {
fn eq(&self, rhs: &String) -> bool {
&self.0 == rhs
}
}

/// Implement `String == Addr`
///
/// Deprecated. This comparison unsafe. Convert both sides to Addr first.
/// Will be removed soon: https://github.com/CosmWasm/cosmwasm/issues/1669
impl PartialEq<Addr> for String {
fn eq(&self, rhs: &Addr) -> bool {
self == &rhs.0
Expand Down Expand Up @@ -426,20 +438,16 @@ mod tests {
assert_eq!(addr.as_ref(), "literal-string");
}

// Please note that this will be removed soon
// https://github.com/CosmWasm/cosmwasm/issues/1669
#[test]
fn addr_implements_partial_eq_with_str() {
fn addr_implements_partial_eq_with_str_and_string() {
let addr = Addr::unchecked("cos934gh9034hg04g0h134");

// `Addr == &str`
assert_eq!(addr, "cos934gh9034hg04g0h134");
// `&str == Addr`
assert_eq!("cos934gh9034hg04g0h134", addr);
}

#[test]
fn addr_implements_partial_eq_with_string() {
let addr = Addr::unchecked("cos934gh9034hg04g0h134");

// `Addr == String`
assert_eq!(addr, String::from("cos934gh9034hg04g0h134"));
// `String == Addr`
Expand Down

0 comments on commit 08e5f88

Please sign in to comment.