From 86218ad240db4dcd4a88ed104e994dee14c8a62b Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Wed, 27 Jul 2022 20:00:18 +0200 Subject: [PATCH] Use macro to implement `std::erorr::Error` There was a bunch of manual implemntations that can be converted to macro call. This commit replaces them except for enums because those are currently not supported by the macro and we want to protect against forgetting to handle newly added variants. --- src/network/message.rs | 8 +------- src/util/sighash.rs | 17 +++-------------- src/util/uint.rs | 8 +------- 3 files changed, 5 insertions(+), 28 deletions(-) diff --git a/src/network/message.rs b/src/network/message.rs index 4524fecf..ba1718d1 100644 --- a/src/network/message.rs +++ b/src/network/message.rs @@ -156,13 +156,7 @@ impl fmt::Display for CommandStringError { } } -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] -impl std::error::Error for CommandStringError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - None - } -} +crate::error::impl_std_error!(CommandStringError); /// A Network message #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/src/util/sighash.rs b/src/util/sighash.rs index a5ff8af6..ba09250c 100644 --- a/src/util/sighash.rs +++ b/src/util/sighash.rs @@ -16,6 +16,7 @@ use crate::{io, Script, Transaction, TxIn, TxOut, Sequence, Sighash}; use crate::blockdata::transaction::EncodeSigningDataResult; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Encodable}; +use crate::error::impl_std_error; use crate::util::endian; use crate::hashes::{sha256, sha256d, Hash}; use crate::internal_macros::serde_string_impl; @@ -488,13 +489,7 @@ impl fmt::Display for NonStandardSighashType { } } -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] -impl std::error::Error for NonStandardSighashType { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - None - } -} +impl_std_error!(NonStandardSighashType); /// Error returned for failure during parsing one of the sighash types. /// @@ -511,13 +506,7 @@ impl fmt::Display for SighashTypeParseError { } } -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] -impl std::error::Error for SighashTypeParseError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - None - } -} +impl_std_error!(SighashTypeParseError); impl> SighashCache { /// Constructs a new `SighashCache` from an unsigned transaction. diff --git a/src/util/uint.rs b/src/util/uint.rs index b64df3ea..5614bd50 100644 --- a/src/util/uint.rs +++ b/src/util/uint.rs @@ -521,13 +521,7 @@ impl core::fmt::Display for ParseLengthError { } } -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] -impl std::error::Error for ParseLengthError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - None - } -} +crate::error::impl_std_error!(ParseLengthError); impl Uint256 { /// Decay to a uint128