Skip to content
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

Deprecate PartialEq between Addr and String #1671

Merged
merged 1 commit into from
May 2, 2023
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
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