diff --git a/bitcoin/src/address/error.rs b/bitcoin/src/address/error.rs index 9c5fd46c..43d11a73 100644 --- a/bitcoin/src/address/error.rs +++ b/bitcoin/src/address/error.rs @@ -4,7 +4,6 @@ use internals::write_err; use crate::address::{Address, NetworkUnchecked}; use crate::blockdata::script::{witness_program, witness_version}; -use crate::error::impl_std_error; use crate::prelude::String; use crate::{base58, Network}; @@ -91,7 +90,10 @@ impl fmt::Display for UnknownAddressTypeError { } } -impl_std_error!(UnknownAddressTypeError); +#[cfg(feature = "std")] +impl std::error::Error for UnknownAddressTypeError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} /// Address parsing error. #[derive(Debug, PartialEq, Eq, Clone)] diff --git a/bitcoin/src/blockdata/script/push_bytes.rs b/bitcoin/src/blockdata/script/push_bytes.rs index c1c184a2..07cd6f28 100644 --- a/bitcoin/src/blockdata/script/push_bytes.rs +++ b/bitcoin/src/blockdata/script/push_bytes.rs @@ -413,4 +413,7 @@ mod error { } } -crate::error::impl_std_error!(PushBytesError); +#[cfg(feature = "std")] +impl std::error::Error for PushBytesError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs index 64384b4c..0e73c808 100644 --- a/bitcoin/src/crypto/sighash.rs +++ b/bitcoin/src/crypto/sighash.rs @@ -18,7 +18,6 @@ use hashes::{hash_newtype, sha256, sha256d, sha256t_hash_newtype, Hash}; use crate::blockdata::witness::Witness; use crate::consensus::{encode, Encodable}; -use crate::error::impl_std_error; use crate::prelude::*; use crate::taproot::{LeafVersion, TapLeafHash, TAPROOT_ANNEX_PREFIX}; use crate::{io, Amount, Script, ScriptBuf, Sequence, Transaction, TxIn, TxOut}; @@ -533,7 +532,10 @@ impl fmt::Display for InvalidSighashTypeError { } } -impl_std_error!(InvalidSighashTypeError); +#[cfg(feature = "std")] +impl std::error::Error for InvalidSighashTypeError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} /// This type is consensus valid but an input including it would prevent the transaction from /// being relayed on today's Bitcoin network. @@ -546,7 +548,10 @@ impl fmt::Display for NonStandardSighashTypeError { } } -impl_std_error!(NonStandardSighashTypeError); +#[cfg(feature = "std")] +impl std::error::Error for NonStandardSighashTypeError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} /// Error returned for failure during parsing one of the sighash types. /// @@ -563,7 +568,10 @@ impl fmt::Display for SighashTypeParseError { } } -impl_std_error!(SighashTypeParseError); +#[cfg(feature = "std")] +impl std::error::Error for SighashTypeParseError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} impl> SighashCache { /// Constructs a new `SighashCache` from an unsigned transaction. diff --git a/bitcoin/src/error.rs b/bitcoin/src/error.rs index ab296628..f5ea45c3 100644 --- a/bitcoin/src/error.rs +++ b/bitcoin/src/error.rs @@ -2,6 +2,4 @@ //! Contains error types and other error handling tools. -pub(crate) use internals::impl_std_error; - pub use crate::parse::ParseIntError; diff --git a/bitcoin/src/network.rs b/bitcoin/src/network.rs index 895a4d93..67807e96 100644 --- a/bitcoin/src/network.rs +++ b/bitcoin/src/network.rs @@ -28,7 +28,6 @@ use internals::write_err; use serde::{Deserialize, Serialize}; use crate::constants::ChainHash; -use crate::error::impl_std_error; use crate::p2p::Magic; use crate::prelude::{String, ToOwned}; @@ -202,7 +201,11 @@ impl fmt::Display for ParseNetworkError { write_err!(f, "failed to parse {} as network", self.0; self) } } -impl_std_error!(ParseNetworkError); + +#[cfg(feature = "std")] +impl std::error::Error for ParseNetworkError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} impl FromStr for Network { type Err = ParseNetworkError; @@ -246,7 +249,10 @@ impl Display for UnknownChainHashError { } } -impl_std_error!(UnknownChainHashError); +#[cfg(feature = "std")] +impl std::error::Error for UnknownChainHashError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} impl TryFrom for Network { type Error = UnknownChainHashError; diff --git a/bitcoin/src/p2p/message.rs b/bitcoin/src/p2p/message.rs index 8b76e80e..ee0c64b2 100644 --- a/bitcoin/src/p2p/message.rs +++ b/bitcoin/src/p2p/message.rs @@ -144,7 +144,10 @@ impl fmt::Display for CommandStringError { } } -crate::error::impl_std_error!(CommandStringError); +#[cfg(feature = "std")] +impl std::error::Error for CommandStringError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} /// A Network message #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/bitcoin/src/p2p/mod.rs b/bitcoin/src/p2p/mod.rs index 861506c8..0e32c620 100644 --- a/bitcoin/src/p2p/mod.rs +++ b/bitcoin/src/p2p/mod.rs @@ -30,7 +30,6 @@ use hex::FromHex; use internals::{debug_from_display, write_err}; use crate::consensus::encode::{self, Decodable, Encodable}; -use crate::error::impl_std_error; use crate::prelude::{Borrow, BorrowMut, String, ToOwned}; use crate::{io, Network}; @@ -339,7 +338,11 @@ impl fmt::Display for ParseMagicError { write_err!(f, "failed to parse {} as network magic", self.magic; self.error) } } -impl_std_error!(ParseMagicError, error); + +#[cfg(feature = "std")] +impl std::error::Error for ParseMagicError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.error) } +} /// Error in creating a Network from Magic bytes. #[derive(Debug, Clone, PartialEq, Eq)] @@ -350,7 +353,11 @@ impl fmt::Display for UnknownMagicError { write!(f, "unknown network magic {}", self.0) } } -impl_std_error!(UnknownMagicError); + +#[cfg(feature = "std")] +impl std::error::Error for UnknownMagicError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } +} #[cfg(test)] mod tests { diff --git a/bitcoin/src/parse.rs b/bitcoin/src/parse.rs index 8d5a9813..1826c5c3 100644 --- a/bitcoin/src/parse.rs +++ b/bitcoin/src/parse.rs @@ -6,7 +6,6 @@ use core::str::FromStr; use internals::write_err; -use crate::error::impl_std_error; use crate::prelude::*; /// Error with rich context returned when a string can't be parsed as an integer. @@ -43,7 +42,10 @@ impl fmt::Display for ParseIntError { } } -impl_std_error!(ParseIntError, source); +#[cfg(feature = "std")] +impl std::error::Error for ParseIntError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.source) } +} impl From for core::num::ParseIntError { fn from(value: ParseIntError) -> Self { value.source } diff --git a/internals/src/error.rs b/internals/src/error.rs index 31580fc7..6731ae13 100644 --- a/internals/src/error.rs +++ b/internals/src/error.rs @@ -31,21 +31,3 @@ macro_rules! write_err { } } } - -/// Impls std::error::Error for the specified type with appropriate attributes, possibly returning -/// source. -#[macro_export] -macro_rules! impl_std_error { - // No source available - ($type:ty) => { - #[cfg(feature = "std")] - impl std::error::Error for $type {} - }; - // Struct with $field as source - ($type:ty, $field:ident) => { - #[cfg(feature = "std")] - impl std::error::Error for $type { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.$field) } - } - }; -} diff --git a/internals/src/error/parse_error.rs b/internals/src/error/parse_error.rs index 15552fbd..7bcf8ceb 100644 --- a/internals/src/error/parse_error.rs +++ b/internals/src/error/parse_error.rs @@ -40,6 +40,9 @@ macro_rules! parse_error_type { } } - $crate::error::impl_std_error!($name, source); + #[cfg(feature = "std")] + impl std::error::Error for $name { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.source) } + } } }