Skip to content

Commit

Permalink
Implement std::error::Error for all error types
Browse files Browse the repository at this point in the history
Now that we have bumped the MSRV we can use `Error::source`. Implement
`std::error::Error` for all error types codebase wide.
  • Loading branch information
tcharding committed Jun 10, 2022
1 parent 5870315 commit 4fb4fa0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
6 changes: 5 additions & 1 deletion src/key.rs
Expand Up @@ -1321,7 +1321,11 @@ impl fmt::Display for InvalidParityValue {

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for InvalidParityValue {}
impl std::error::Error for InvalidParityValue {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

impl From<InvalidParityValue> for Error {
fn from(error: InvalidParityValue) -> Self {
Expand Down
30 changes: 15 additions & 15 deletions src/lib.rs
Expand Up @@ -372,25 +372,25 @@ impl fmt::Display for Error {
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for Error {
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn std::error::Error> {
match self {
Error::IncorrectSignature => None,
Error::InvalidMessage => None,
Error::InvalidPublicKey => None,
Error::InvalidSignature => None,
Error::InvalidSecretKey => None,
Error::InvalidSharedSecret => None,
Error::InvalidRecoveryId => None,
Error::InvalidTweak => None,
Error::NotEnoughMemory => None,
Error::InvalidPublicKeySum => None,
Error::InvalidParityValue(error) => Some(error),
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;

match *self {
IncorrectSignature
| InvalidMessage
| InvalidPublicKey
| InvalidSignature
| InvalidSecretKey
| InvalidSharedSecret
| InvalidRecoveryId
| InvalidTweak
| NotEnoughMemory
| InvalidPublicKeySum => None,
InvalidParityValue(ref e) => Some(e),
}
}
}


/// The secp256k1 engine, used to execute all signature operations.
pub struct Secp256k1<C: Context> {
ctx: *mut ffi::Context,
Expand Down
6 changes: 5 additions & 1 deletion src/scalar.rs
Expand Up @@ -133,4 +133,8 @@ impl fmt::Display for OutOfRangeError {

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for OutOfRangeError {}
impl std::error::Error for OutOfRangeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
None
}
}

0 comments on commit 4fb4fa0

Please sign in to comment.