Skip to content

Commit

Permalink
Remove usage of impl_from_infallible in crates
Browse files Browse the repository at this point in the history
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the usage of the
`impl_from_infallible` macro in the bitcoin, units, and internals crates
and just write the code.
  • Loading branch information
shinghim committed Jan 5, 2025
1 parent b97be3d commit f94c718
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 115 deletions.
28 changes: 21 additions & 7 deletions base58/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Error code for the `base58` crate.
use core::fmt;
use core::convert::Infallible;

use internals::write_err;

Expand All @@ -20,8 +21,13 @@ pub(super) enum ErrorInner {
TooShort(TooShortError),
}

internals::impl_from_infallible!(Error);
internals::impl_from_infallible!(ErrorInner);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl From<Infallible> for ErrorInner {
fn from(never: Infallible) -> Self { match never {} }
}

impl Error {
/// Returns the invalid base58 ssscharacter, if encountered.
Expand Down Expand Up @@ -95,7 +101,9 @@ pub(super) struct IncorrectChecksumError {
pub(super) expected: u32,
}

internals::impl_from_infallible!(IncorrectChecksumError);
impl From<Infallible> for IncorrectChecksumError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for IncorrectChecksumError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -116,8 +124,9 @@ pub(super) struct TooShortError {
/// The length of the decoded data.
pub(super) length: usize,
}

internals::impl_from_infallible!(TooShortError);
impl From<Infallible> for TooShortError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for TooShortError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -141,8 +150,13 @@ pub(super) struct InvalidCharacterErrorInner {
pub(super) invalid: u8,
}

internals::impl_from_infallible!(InvalidCharacterError);
internals::impl_from_infallible!(InvalidCharacterErrorInner);
impl From<Infallible> for InvalidCharacterError {
fn from(never: Infallible) -> Self { match never {} }
}

impl From<Infallible> for InvalidCharacterErrorInner {
fn from(never: Infallible) -> Self { match never {} }
}

impl InvalidCharacterError {
pub(super) fn new(invalid: u8) -> Self { Self(InvalidCharacterErrorInner { invalid }) }
Expand Down
21 changes: 16 additions & 5 deletions bitcoin/src/address/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Error code for the address module.
use core::fmt;
use core::convert::Infallible;

use internals::write_err;

Expand All @@ -21,7 +22,9 @@ pub enum FromScriptError {
WitnessVersion(witness_version::TryFromError),
}

internals::impl_from_infallible!(FromScriptError);
impl From<Infallible> for FromScriptError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for FromScriptError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -84,7 +87,9 @@ pub enum ParseError {
NetworkValidation(NetworkValidationError),
}

internals::impl_from_infallible!(ParseError);
impl From<Infallible> for ParseError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -175,7 +180,9 @@ pub enum Bech32Error {
UnknownHrp(UnknownHrpError),
}

internals::impl_from_infallible!(Bech32Error);
impl From<Infallible> for Bech32Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Bech32Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -221,7 +228,9 @@ impl From<UnknownHrpError> for Bech32Error {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseBech32Error(pub(crate) bech32::segwit::DecodeError);

internals::impl_from_infallible!(ParseBech32Error);
impl From<Infallible> for ParseBech32Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for ParseBech32Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -248,7 +257,9 @@ pub enum Base58Error {
InvalidLegacyPrefix(InvalidLegacyPrefixError),
}

internals::impl_from_infallible!(Base58Error);
impl From<Infallible> for Base58Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Base58Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/bip152.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! Implementation of compact blocks data structure and algorithms.
use core::{convert, fmt, mem};
use core::convert::Infallible;
#[cfg(feature = "std")]
use std::error;

Expand All @@ -30,7 +31,9 @@ pub enum Error {
InvalidPrefill,
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use core::cmp::{self, Ordering};
use core::fmt;
use core::convert::Infallible;

use hashes::{sha256d, siphash24, HashEngine as _};
use internals::{write_err, ToU64 as _};
Expand Down Expand Up @@ -79,7 +80,9 @@ pub enum Error {
Io(io::Error),
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use core::ops::Index;
use core::str::FromStr;
use core::{fmt, slice};
use core::convert::Infallible;

use hashes::{hash160, hash_newtype, sha512, GeneralHash, HashEngine, Hmac, HmacEngine};
use internals::write_err;
Expand Down Expand Up @@ -519,7 +520,9 @@ pub enum Error {
InvalidBase58PayloadLength(InvalidBase58PayloadLengthError),
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
13 changes: 10 additions & 3 deletions bitcoin/src/blockdata/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! these blocks and the blockchain.
use core::fmt;
use core::convert::Infallible;

use hashes::{sha256d, HashEngine};
use internals::{compact_size, ToU64};
Expand Down Expand Up @@ -383,7 +384,9 @@ pub enum InvalidBlockError {
InvalidWitnessCommitment,
}

internals::impl_from_infallible!(InvalidBlockError);
impl From<Infallible> for InvalidBlockError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for InvalidBlockError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -413,7 +416,9 @@ pub enum Bip34Error {
NegativeHeight,
}

internals::impl_from_infallible!(Bip34Error);
impl From<Infallible> for Bip34Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Bip34Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -457,7 +462,9 @@ pub enum ValidationError {
BadTarget,
}

internals::impl_from_infallible!(ValidationError);
impl From<Infallible> for ValidationError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/blockdata/script/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub mod witness_program;
pub mod witness_version;

use core::fmt;
use core::convert::Infallible;

use io::{BufRead, Write};
use primitives::opcodes::all::*;
Expand Down Expand Up @@ -234,7 +235,9 @@ pub enum Error {
Serialization,
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/blockdata/script/witness_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! [BIP141]: <https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki>
use core::fmt;
use core::convert::Infallible;

use internals::array_vec::ArrayVec;
use secp256k1::{Secp256k1, Verification};
Expand Down Expand Up @@ -139,7 +140,9 @@ pub enum Error {
InvalidSegwitV0Length(usize),
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
9 changes: 7 additions & 2 deletions bitcoin/src/blockdata/script/witness_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use core::fmt;
use core::str::FromStr;
use core::convert::Infallible;

use internals::write_err;
use units::parse::{self, ParseIntError};
Expand Down Expand Up @@ -159,7 +160,9 @@ pub enum FromStrError {
Invalid(TryFromError),
}

internals::impl_from_infallible!(FromStrError);
impl From<Infallible> for FromStrError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for FromStrError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -202,7 +205,9 @@ pub enum TryFromInstructionError {
DataPush,
}

internals::impl_from_infallible!(TryFromInstructionError);
impl From<Infallible> for TryFromInstructionError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for TryFromInstructionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
17 changes: 13 additions & 4 deletions bitcoin/src/consensus/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Consensus encoding errors.
use core::fmt;
use core::convert::Infallible;

use hex::error::{InvalidCharError, OddLengthStringError};
use hex::DisplayHex as _;
Expand All @@ -21,7 +22,9 @@ pub enum DeserializeError {
Unconsumed,
}

internals::impl_from_infallible!(DeserializeError);
impl From<Infallible> for DeserializeError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for DeserializeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -64,7 +67,9 @@ pub enum DecodeError<E> {
Other(E), // Yielded by the inner iterator.
}

internals::impl_from_infallible!(DecodeError<E>);
impl<E> From<Infallible> for DecodeError<E> {
fn from(never: Infallible) -> Self { match never {} }
}

impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -101,7 +106,9 @@ pub enum Error {
Parse(ParseError),
}

internals::impl_from_infallible!(Error);
impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -169,7 +176,9 @@ pub enum ParseError {
UnsupportedSegwitFlag(u8),
}

internals::impl_from_infallible!(ParseError);
impl From<Infallible> for ParseError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
5 changes: 4 additions & 1 deletion bitcoin/src/consensus_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! Relies on the `bitcoinconsensus` crate that uses Bitcoin Core libconsensus to perform validation.
use core::fmt;
use core::convert::Infallible;

use internals::write_err;

Expand Down Expand Up @@ -237,7 +238,9 @@ pub enum TxVerifyError {
UnknownSpentOutput(OutPoint),
}

internals::impl_from_infallible!(TxVerifyError);
impl From<Infallible> for TxVerifyError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for TxVerifyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
9 changes: 7 additions & 2 deletions bitcoin/src/crypto/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use core::str::FromStr;
use core::{fmt, iter};
use core::convert::Infallible;

#[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured};
Expand Down Expand Up @@ -213,7 +214,9 @@ pub enum DecodeError {
Secp256k1(secp256k1::Error),
}

internals::impl_from_infallible!(DecodeError);
impl From<Infallible> for DecodeError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down Expand Up @@ -258,7 +261,9 @@ pub enum ParseSignatureError {
Decode(DecodeError),
}

internals::impl_from_infallible!(ParseSignatureError);
impl From<Infallible> for ParseSignatureError {
fn from(never: Infallible) -> Self { match never {} }
}

impl fmt::Display for ParseSignatureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
Loading

0 comments on commit f94c718

Please sign in to comment.