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

fixed-hash: Derive PartialEq and Eq instead of implementing them manually #742

Merged
merged 3 commits into from
Apr 22, 2023

Conversation

fhartwig
Copy link
Contributor

The motivation for this is that Rust only lets you use named constants (as opposed to literals) for pattern matching if the value's type has derived PartialEq and Eq implementations. So trying to compile the following code:

use ethereum_types::H160;

const ONE: H160 = H160::repeat_byte(1);;
const TWO: H160 = H160::repeat_byte(2);

fn main() {
    let x = H160::repeat_byte(3);
    match x {
        ONE => println!("it's a one!"),
        TWO => println!("it's a two!"),
        _ => println!("something else"),
    }
}

currently results in the following compilation error:

error: to use a constant of type `H160` in a pattern, `H160` must be annotated with `#[derive(PartialEq, Eq)]`
 --> src/main.rs:9:9
  |
9 |         ONE => println!("it's a one!"),
  |         ^^^

error: to use a constant of type `H160` in a pattern, `H160` must be annotated with `#[derive(PartialEq, Eq)]`
  --> src/main.rs:10:9
   |
10 |         TWO => println!("it's a two!"),
   |         ^^^

As far as I can tell, the existing explicit PartialEq implementation exists for historical reasons and isn't necessary any more, the derived implementation should be equivalent. Assuming that is true, the only potential drawback of this change that I can see is that it poses a backwards compatibility hazard, i.e. if in the future you would want to manually implement PartialEq again for some reason, doing so would break the code of anyone using e.g. named H160 constants in pattern matches.

Copy link
Member

@ordian ordian left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Could you add a test similar to the one introduced in #421?

@fhartwig
Copy link
Contributor Author

Sure, done!

@ordian ordian added A8-looksgood changelog Needs to be added to the changelog labels Apr 22, 2023
@ordian
Copy link
Member

ordian commented Apr 22, 2023

Thanks! Only cargo +nightly fmt is missing: https://github.com/paritytech/parity-common/actions/runs/4769435148/jobs/8484483754?pr=742

@fhartwig
Copy link
Contributor Author

Sorry about that, fixed.

@ordian ordian merged commit 910097e into paritytech:master Apr 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A8-looksgood changelog Needs to be added to the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants