Remove impl_std_error macro
We would like the codebase to be optimized for readability not ease of development, as such code that is write-once-read-many should not use macros. Currently we use the `impl_std_error` macro to implement `std::error::Error` for struct error types. This makes the code harder to read at a glance because one has to think what the macro does. Remove the `impl_std_error` macro and write the code explicitly.
This commit is contained in:
parent
6933ca4fc2
commit
2512dbafc2
|
@ -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)]
|
||||
|
|
|
@ -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 }
|
||||
}
|
||||
|
|
|
@ -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<R: Borrow<Transaction>> SighashCache<R> {
|
||||
/// Constructs a new `SighashCache` from an unsigned transaction.
|
||||
|
|
|
@ -2,6 +2,4 @@
|
|||
|
||||
//! Contains error types and other error handling tools.
|
||||
|
||||
pub(crate) use internals::impl_std_error;
|
||||
|
||||
pub use crate::parse::ParseIntError;
|
||||
|
|
|
@ -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<ChainHash> for Network {
|
||||
type Error = UnknownChainHashError;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<ParseIntError> for core::num::ParseIntError {
|
||||
fn from(value: ParseIntError) -> Self { value.source }
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -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) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue