Skip to content

Commit

Permalink
fix: use *self in fmt::Display implementation for `ParsePublicKey…
Browse files Browse the repository at this point in the history
…Error`

The `fmt` method in `fmt::Display` takes `&self` as a shared reference.
To pattern match on the enum variants, we can dereference `self` to access the underlying value (`ParsePublicKeyError`) instead of matching on the reference.

This fix changed the match block from `self` to `*self` to explicitly pattern match the enum variants directly as is done in other error code.

Without dereferencing, the match works with `&ParsePublicKeyError`, causing mismatched types. Even though non-exact type matching is supported in this codebase, we tend to be explicit when pattern matching.

This update improves the readability and correctness of the `fmt::Display` implementation.
  • Loading branch information
rockcoolsaint committed Jan 12, 2025
1 parent 57469bf commit 34e0334
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions bitcoin/src/crypto/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,9 +1034,9 @@ impl From<Infallible> for ParsePublicKeyError {
impl fmt::Display for ParsePublicKeyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ParsePublicKeyError::*;
match self {
Encoding(e) => write_err!(f, "string error"; e),
InvalidChar(char) => write!(f, "hex error {}", char),
match *self {
Encoding(ref e) => write_err!(f, "string error"; e),
InvalidChar(ref char) => write!(f, "hex error {}", char),
InvalidHexLength(got) =>
write!(f, "pubkey string should be 66 or 130 digits long, got: {}", got),
}
Expand All @@ -1048,9 +1048,10 @@ impl std::error::Error for ParsePublicKeyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use ParsePublicKeyError::*;

match self {
Encoding(e) => Some(e),
InvalidChar(_) | InvalidHexLength(_) => None,
match *self {
Encoding(ref e) => Some(e),
InvalidChar(ref e) => Some(e),
InvalidHexLength(_) => None,
}
}
}
Expand Down

0 comments on commit 34e0334

Please sign in to comment.