From 4fb4fa09363e958da4bb4dc86c56b4afc58fa29d Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 10 Jun 2022 12:17:59 +1000 Subject: [PATCH] Implement std::error::Error for all error types Now that we have bumped the MSRV we can use `Error::source`. Implement `std::error::Error` for all error types codebase wide. --- src/key.rs | 6 +++++- src/lib.rs | 30 +++++++++++++++--------------- src/scalar.rs | 6 +++++- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/key.rs b/src/key.rs index 4f715d8c6..03e2a8dd5 100644 --- a/src/key.rs +++ b/src/key.rs @@ -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 for Error { fn from(error: InvalidParityValue) -> Self { diff --git a/src/lib.rs b/src/lib.rs index 902f656ef..e1694ffd0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 { ctx: *mut ffi::Context, diff --git a/src/scalar.rs b/src/scalar.rs index 03854f5e9..138990785 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -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 + } +}