Merge rust-bitcoin/rust-bitcoin#3859: Remove usage of impl_from_infallible in crates

f94c7185fd Remove usage of impl_from_infallible in crates (Shing Him Ng)

Pull request description:

  Fixes #3843

  tcharding Copied your commit message from the other `impl_from_infallible` commit 😄

ACKs for top commit:
  apoelstra:
    ACK f94c7185fdd62e1ed98ed4016486406146c4d4f3; successfully ran local tests; nice!
  tcharding:
    ACK f94c7185fd

Tree-SHA512: 8c58c2c87f6892855d74a3306e1027a37394961f0a26b7bd88cc1654a190dda37234e7dde51a419dcd2f1bd1dd1ccceec16bbbc6fbdd5418ad21f10531b402b3
This commit is contained in:
merge-script 2025-01-06 14:15:17 +00:00
commit 70a879279b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
22 changed files with 189 additions and 115 deletions

View File

@ -3,6 +3,7 @@
//! Error code for the `base58` crate. //! Error code for the `base58` crate.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::write_err; use internals::write_err;
@ -20,8 +21,13 @@ pub(super) enum ErrorInner {
TooShort(TooShortError), TooShort(TooShortError),
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
internals::impl_from_infallible!(ErrorInner); fn from(never: Infallible) -> Self { match never {} }
}
impl From<Infallible> for ErrorInner {
fn from(never: Infallible) -> Self { match never {} }
}
impl Error { impl Error {
/// Returns the invalid base58 ssscharacter, if encountered. /// Returns the invalid base58 ssscharacter, if encountered.
@ -95,7 +101,9 @@ pub(super) struct IncorrectChecksumError {
pub(super) expected: u32, 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 { impl fmt::Display for IncorrectChecksumError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -116,8 +124,9 @@ pub(super) struct TooShortError {
/// The length of the decoded data. /// The length of the decoded data.
pub(super) length: usize, pub(super) length: usize,
} }
impl From<Infallible> for TooShortError {
internals::impl_from_infallible!(TooShortError); fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TooShortError { impl fmt::Display for TooShortError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -141,8 +150,13 @@ pub(super) struct InvalidCharacterErrorInner {
pub(super) invalid: u8, pub(super) invalid: u8,
} }
internals::impl_from_infallible!(InvalidCharacterError); impl From<Infallible> for InvalidCharacterError {
internals::impl_from_infallible!(InvalidCharacterErrorInner); fn from(never: Infallible) -> Self { match never {} }
}
impl From<Infallible> for InvalidCharacterErrorInner {
fn from(never: Infallible) -> Self { match never {} }
}
impl InvalidCharacterError { impl InvalidCharacterError {
pub(super) fn new(invalid: u8) -> Self { Self(InvalidCharacterErrorInner { invalid }) } pub(super) fn new(invalid: u8) -> Self { Self(InvalidCharacterErrorInner { invalid }) }

View File

@ -1,6 +1,7 @@
//! Error code for the address module. //! Error code for the address module.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::write_err; use internals::write_err;
@ -21,7 +22,9 @@ pub enum FromScriptError {
WitnessVersion(witness_version::TryFromError), 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 { impl fmt::Display for FromScriptError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -84,7 +87,9 @@ pub enum ParseError {
NetworkValidation(NetworkValidationError), NetworkValidation(NetworkValidationError),
} }
internals::impl_from_infallible!(ParseError); impl From<Infallible> for ParseError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ParseError { impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -175,7 +180,9 @@ pub enum Bech32Error {
UnknownHrp(UnknownHrpError), UnknownHrp(UnknownHrpError),
} }
internals::impl_from_infallible!(Bech32Error); impl From<Infallible> for Bech32Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Bech32Error { impl fmt::Display for Bech32Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -221,7 +228,9 @@ impl From<UnknownHrpError> for Bech32Error {
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseBech32Error(pub(crate) bech32::segwit::DecodeError); 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 { impl fmt::Display for ParseBech32Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -248,7 +257,9 @@ pub enum Base58Error {
InvalidLegacyPrefix(InvalidLegacyPrefixError), InvalidLegacyPrefix(InvalidLegacyPrefixError),
} }
internals::impl_from_infallible!(Base58Error); impl From<Infallible> for Base58Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Base58Error { impl fmt::Display for Base58Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -5,6 +5,7 @@
//! Implementation of compact blocks data structure and algorithms. //! Implementation of compact blocks data structure and algorithms.
use core::{convert, fmt, mem}; use core::{convert, fmt, mem};
use core::convert::Infallible;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::error; use std::error;
@ -30,7 +31,9 @@ pub enum Error {
InvalidPrefill, InvalidPrefill,
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -39,6 +39,7 @@
use core::cmp::{self, Ordering}; use core::cmp::{self, Ordering};
use core::fmt; use core::fmt;
use core::convert::Infallible;
use hashes::{sha256d, siphash24, HashEngine as _}; use hashes::{sha256d, siphash24, HashEngine as _};
use internals::{write_err, ToU64 as _}; use internals::{write_err, ToU64 as _};
@ -79,7 +80,9 @@ pub enum Error {
Io(io::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 { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {

View File

@ -8,6 +8,7 @@
use core::ops::Index; use core::ops::Index;
use core::str::FromStr; use core::str::FromStr;
use core::{fmt, slice}; use core::{fmt, slice};
use core::convert::Infallible;
use hashes::{hash160, hash_newtype, sha512, GeneralHash, HashEngine, Hmac, HmacEngine}; use hashes::{hash160, hash_newtype, sha512, GeneralHash, HashEngine, Hmac, HmacEngine};
use internals::write_err; use internals::write_err;
@ -519,7 +520,9 @@ pub enum Error {
InvalidBase58PayloadLength(InvalidBase58PayloadLengthError), InvalidBase58PayloadLength(InvalidBase58PayloadLengthError),
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -8,6 +8,7 @@
//! these blocks and the blockchain. //! these blocks and the blockchain.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use hashes::{sha256d, HashEngine}; use hashes::{sha256d, HashEngine};
use internals::{compact_size, ToU64}; use internals::{compact_size, ToU64};
@ -383,7 +384,9 @@ pub enum InvalidBlockError {
InvalidWitnessCommitment, InvalidWitnessCommitment,
} }
internals::impl_from_infallible!(InvalidBlockError); impl From<Infallible> for InvalidBlockError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for InvalidBlockError { impl fmt::Display for InvalidBlockError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -413,7 +416,9 @@ pub enum Bip34Error {
NegativeHeight, NegativeHeight,
} }
internals::impl_from_infallible!(Bip34Error); impl From<Infallible> for Bip34Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Bip34Error { impl fmt::Display for Bip34Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -457,7 +462,9 @@ pub enum ValidationError {
BadTarget, BadTarget,
} }
internals::impl_from_infallible!(ValidationError); impl From<Infallible> for ValidationError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ValidationError { impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -58,6 +58,7 @@ pub mod witness_program;
pub mod witness_version; pub mod witness_version;
use core::fmt; use core::fmt;
use core::convert::Infallible;
use io::{BufRead, Write}; use io::{BufRead, Write};
use primitives::opcodes::all::*; use primitives::opcodes::all::*;
@ -234,7 +235,9 @@ pub enum Error {
Serialization, Serialization,
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -8,6 +8,7 @@
//! [BIP141]: <https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki> //! [BIP141]: <https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki>
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::array_vec::ArrayVec; use internals::array_vec::ArrayVec;
use secp256k1::{Secp256k1, Verification}; use secp256k1::{Secp256k1, Verification};
@ -139,7 +140,9 @@ pub enum Error {
InvalidSegwitV0Length(usize), InvalidSegwitV0Length(usize),
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -9,6 +9,7 @@
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
use core::convert::Infallible;
use internals::write_err; use internals::write_err;
use units::parse::{self, ParseIntError}; use units::parse::{self, ParseIntError};
@ -159,7 +160,9 @@ pub enum FromStrError {
Invalid(TryFromError), Invalid(TryFromError),
} }
internals::impl_from_infallible!(FromStrError); impl From<Infallible> for FromStrError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for FromStrError { impl fmt::Display for FromStrError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -202,7 +205,9 @@ pub enum TryFromInstructionError {
DataPush, DataPush,
} }
internals::impl_from_infallible!(TryFromInstructionError); impl From<Infallible> for TryFromInstructionError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TryFromInstructionError { impl fmt::Display for TryFromInstructionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -3,6 +3,7 @@
//! Consensus encoding errors. //! Consensus encoding errors.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use hex::error::{InvalidCharError, OddLengthStringError}; use hex::error::{InvalidCharError, OddLengthStringError};
use hex::DisplayHex as _; use hex::DisplayHex as _;
@ -21,7 +22,9 @@ pub enum DeserializeError {
Unconsumed, Unconsumed,
} }
internals::impl_from_infallible!(DeserializeError); impl From<Infallible> for DeserializeError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for DeserializeError { impl fmt::Display for DeserializeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -64,7 +67,9 @@ pub enum DecodeError<E> {
Other(E), // Yielded by the inner iterator. 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> { impl<E: fmt::Debug> fmt::Display for DecodeError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -101,7 +106,9 @@ pub enum Error {
Parse(ParseError), Parse(ParseError),
} }
internals::impl_from_infallible!(Error); impl From<Infallible> for Error {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for Error { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -169,7 +176,9 @@ pub enum ParseError {
UnsupportedSegwitFlag(u8), UnsupportedSegwitFlag(u8),
} }
internals::impl_from_infallible!(ParseError); impl From<Infallible> for ParseError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ParseError { impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -5,6 +5,7 @@
//! Relies on the `bitcoinconsensus` crate that uses Bitcoin Core libconsensus to perform validation. //! Relies on the `bitcoinconsensus` crate that uses Bitcoin Core libconsensus to perform validation.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::write_err; use internals::write_err;
@ -237,7 +238,9 @@ pub enum TxVerifyError {
UnknownSpentOutput(OutPoint), UnknownSpentOutput(OutPoint),
} }
internals::impl_from_infallible!(TxVerifyError); impl From<Infallible> for TxVerifyError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TxVerifyError { impl fmt::Display for TxVerifyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -6,6 +6,7 @@
use core::str::FromStr; use core::str::FromStr;
use core::{fmt, iter}; use core::{fmt, iter};
use core::convert::Infallible;
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured}; use arbitrary::{Arbitrary, Unstructured};
@ -213,7 +214,9 @@ pub enum DecodeError {
Secp256k1(secp256k1::Error), 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 { impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -258,7 +261,9 @@ pub enum ParseSignatureError {
Decode(DecodeError), Decode(DecodeError),
} }
internals::impl_from_infallible!(ParseSignatureError); impl From<Infallible> for ParseSignatureError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ParseSignatureError { impl fmt::Display for ParseSignatureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -8,6 +8,7 @@
use core::fmt::{self, Write as _}; use core::fmt::{self, Write as _};
use core::ops; use core::ops;
use core::str::FromStr; use core::str::FromStr;
use core::convert::Infallible;
use hashes::hash160; use hashes::hash160;
use hex::{FromHex, HexToArrayError}; use hex::{FromHex, HexToArrayError};
@ -918,7 +919,9 @@ pub enum FromSliceError {
InvalidLength(usize), InvalidLength(usize),
} }
internals::impl_from_infallible!(FromSliceError); impl From<Infallible> for FromSliceError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for FromSliceError { impl fmt::Display for FromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -962,7 +965,9 @@ pub enum FromWifError {
Secp256k1(secp256k1::Error), Secp256k1(secp256k1::Error),
} }
internals::impl_from_infallible!(FromWifError); impl From<Infallible> for FromWifError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for FromWifError { impl fmt::Display for FromWifError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -1022,7 +1027,9 @@ pub enum ParsePublicKeyError {
InvalidHexLength(usize), InvalidHexLength(usize),
} }
internals::impl_from_infallible!(ParsePublicKeyError); impl From<Infallible> for ParsePublicKeyError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ParsePublicKeyError { impl fmt::Display for ParsePublicKeyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -1061,7 +1068,9 @@ pub enum ParseCompressedPublicKeyError {
Hex(hex::HexToArrayError), Hex(hex::HexToArrayError),
} }
internals::impl_from_infallible!(ParseCompressedPublicKeyError); impl From<Infallible> for ParseCompressedPublicKeyError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ParseCompressedPublicKeyError { impl fmt::Display for ParseCompressedPublicKeyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -12,6 +12,7 @@
//! [`SighashCache`] and calling its methods. //! [`SighashCache`] and calling its methods.
use core::{fmt, str}; use core::{fmt, str};
use core::convert::Infallible;
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured}; use arbitrary::{Arbitrary, Unstructured};
@ -302,7 +303,9 @@ pub enum PrevoutsIndexError {
InvalidAllIndex, InvalidAllIndex,
} }
internals::impl_from_infallible!(PrevoutsIndexError); impl From<Infallible> for PrevoutsIndexError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for PrevoutsIndexError { impl fmt::Display for PrevoutsIndexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1203,7 +1206,9 @@ pub enum TaprootError {
InvalidSighashType(u32), InvalidSighashType(u32),
} }
internals::impl_from_infallible!(TaprootError); impl From<Infallible> for TaprootError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TaprootError { impl fmt::Display for TaprootError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1262,7 +1267,9 @@ pub enum P2wpkhError {
NotP2wpkhScript, NotP2wpkhScript,
} }
internals::impl_from_infallible!(P2wpkhError); impl From<Infallible> for P2wpkhError {
fn from(never: Infallible) -> Self { match never {} }
}
impl From<transaction::InputsIndexError> for P2wpkhError { impl From<transaction::InputsIndexError> for P2wpkhError {
fn from(value: transaction::InputsIndexError) -> Self { P2wpkhError::Sighash(value) } fn from(value: transaction::InputsIndexError) -> Self { P2wpkhError::Sighash(value) }
@ -1327,7 +1334,9 @@ pub enum AnnexError {
IncorrectPrefix(u8), IncorrectPrefix(u8),
} }
internals::impl_from_infallible!(AnnexError); impl From<Infallible> for AnnexError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for AnnexError { impl fmt::Display for AnnexError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1438,7 +1447,9 @@ pub enum SigningDataError<E> {
Sighash(E), Sighash(E),
} }
internals::impl_from_infallible!(SigningDataError<E>); impl<E> From<Infallible> for SigningDataError<E> {
fn from(never: Infallible) -> Self { match never {} }
}
impl<E> SigningDataError<E> { impl<E> SigningDataError<E> {
/// Returns the sighash variant, panicking if it's IO. /// Returns the sighash variant, panicking if it's IO.

View File

@ -5,6 +5,7 @@
//! This module provides Taproot keys used in Bitcoin (including reexporting secp256k1 keys). //! This module provides Taproot keys used in Bitcoin (including reexporting secp256k1 keys).
use core::fmt; use core::fmt;
use core::convert::Infallible;
#[cfg(feature = "arbitrary")] #[cfg(feature = "arbitrary")]
use arbitrary::{Arbitrary, Unstructured}; use arbitrary::{Arbitrary, Unstructured};
@ -96,7 +97,9 @@ pub enum SigFromSliceError {
InvalidSignatureSize(usize), InvalidSignatureSize(usize),
} }
internals::impl_from_infallible!(SigFromSliceError); impl From<Infallible> for SigFromSliceError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for SigFromSliceError { impl fmt::Display for SigFromSliceError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -10,6 +10,7 @@
//! Support proofs that transaction(s) belong to a block. //! Support proofs that transaction(s) belong to a block.
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::ToU64 as _; use internals::ToU64 as _;
use io::{BufRead, Write}; use io::{BufRead, Write};
@ -473,7 +474,9 @@ pub enum MerkleBlockError {
IdenticalHashesFound, IdenticalHashesFound,
} }
internals::impl_from_infallible!(MerkleBlockError); impl From<Infallible> for MerkleBlockError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for MerkleBlockError { impl fmt::Display for MerkleBlockError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -1,6 +1,7 @@
// SPDX-License-Identifier: CC0-1.0 // SPDX-License-Identifier: CC0-1.0
use core::fmt; use core::fmt;
use core::convert::Infallible;
use internals::write_err; use internals::write_err;
@ -107,7 +108,9 @@ pub enum Error {
Io(io::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 { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -14,6 +14,7 @@ pub mod raw;
pub mod serialize; pub mod serialize;
use core::{cmp, fmt}; use core::{cmp, fmt};
use core::convert::Infallible;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
@ -857,7 +858,9 @@ pub enum GetKeyError {
NotSupported, NotSupported,
} }
internals::impl_from_infallible!(GetKeyError); impl From<Infallible> for GetKeyError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for GetKeyError { impl fmt::Display for GetKeyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -970,7 +973,9 @@ pub enum SignError {
Unsupported, Unsupported,
} }
internals::impl_from_infallible!(SignError); impl From<Infallible> for SignError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for SignError { impl fmt::Display for SignError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1059,7 +1064,9 @@ pub enum ExtractTxError {
}, },
} }
internals::impl_from_infallible!(ExtractTxError); impl From<Infallible> for ExtractTxError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for ExtractTxError { impl fmt::Display for ExtractTxError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1111,7 +1118,9 @@ pub enum IndexOutOfBoundsError {
}, },
} }
internals::impl_from_infallible!(IndexOutOfBoundsError); impl From<Infallible> for IndexOutOfBoundsError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for IndexOutOfBoundsError { impl fmt::Display for IndexOutOfBoundsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1147,6 +1156,7 @@ impl std::error::Error for IndexOutOfBoundsError {
mod display_from_str { mod display_from_str {
use core::fmt; use core::fmt;
use core::str::FromStr; use core::str::FromStr;
use core::convert::Infallible;
use base64::display::Base64Display; use base64::display::Base64Display;
use base64::prelude::{Engine as _, BASE64_STANDARD}; use base64::prelude::{Engine as _, BASE64_STANDARD};
@ -1164,7 +1174,9 @@ mod display_from_str {
Base64Encoding(::base64::DecodeError), Base64Encoding(::base64::DecodeError),
} }
internals::impl_from_infallible!(PsbtParseError); impl From<Infallible> for PsbtParseError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for PsbtParseError { impl fmt::Display for PsbtParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@ -22,6 +22,7 @@ pub const BITCOIN_SIGNED_MSG_PREFIX: &[u8] = b"\x18Bitcoin Signed Message:\n";
#[cfg(feature = "secp-recovery")] #[cfg(feature = "secp-recovery")]
mod message_signing { mod message_signing {
use core::fmt; use core::fmt;
use core::convert::Infallible;
use hashes::sha256d; use hashes::sha256d;
use internals::write_err; use internals::write_err;
@ -44,7 +45,9 @@ mod message_signing {
UnsupportedAddressType(AddressType), UnsupportedAddressType(AddressType),
} }
internals::impl_from_infallible!(MessageSignatureError); impl From<Infallible> for MessageSignatureError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for MessageSignatureError { impl fmt::Display for MessageSignatureError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

View File

@ -10,6 +10,7 @@ pub mod serialized_signature;
use core::cmp::{Ordering, Reverse}; use core::cmp::{Ordering, Reverse};
use core::fmt; use core::fmt;
use core::iter::FusedIterator; use core::iter::FusedIterator;
use core::convert::Infallible;
use hashes::{sha256t, HashEngine}; use hashes::{sha256t, HashEngine};
use internals::{impl_to_hex_from_lower_hex, write_err}; use internals::{impl_to_hex_from_lower_hex, write_err};
@ -585,7 +586,9 @@ pub enum IncompleteBuilderError {
HiddenParts(TaprootBuilder), HiddenParts(TaprootBuilder),
} }
internals::impl_from_infallible!(IncompleteBuilderError); impl From<Infallible> for IncompleteBuilderError {
fn from(never: Infallible) -> Self { match never {} }
}
impl IncompleteBuilderError { impl IncompleteBuilderError {
/// Converts error into the original incomplete [`TaprootBuilder`] instance. /// Converts error into the original incomplete [`TaprootBuilder`] instance.
@ -631,7 +634,9 @@ pub enum HiddenNodesError {
HiddenParts(NodeInfo), HiddenParts(NodeInfo),
} }
internals::impl_from_infallible!(HiddenNodesError); impl From<Infallible> for HiddenNodesError {
fn from(never: Infallible) -> Self { match never {} }
}
impl HiddenNodesError { impl HiddenNodesError {
/// Converts error into the original incomplete [`NodeInfo`] instance. /// Converts error into the original incomplete [`NodeInfo`] instance.
@ -1341,7 +1346,9 @@ pub enum TaprootBuilderError {
EmptyTree, EmptyTree,
} }
internals::impl_from_infallible!(TaprootBuilderError); impl From<Infallible> for TaprootBuilderError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TaprootBuilderError { impl fmt::Display for TaprootBuilderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1398,7 +1405,9 @@ pub enum TaprootError {
EmptyTree, EmptyTree,
} }
internals::impl_from_infallible!(TaprootError); impl From<Infallible> for TaprootError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for TaprootError { impl fmt::Display for TaprootError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1454,7 +1463,9 @@ impl InvalidMerkleBranchSizeError {
pub fn invalid_merkle_branch_size(&self) -> usize { self.0 } pub fn invalid_merkle_branch_size(&self) -> usize { self.0 }
} }
internals::impl_from_infallible!(InvalidMerkleBranchSizeError); impl From<Infallible> for InvalidMerkleBranchSizeError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for InvalidMerkleBranchSizeError { impl fmt::Display for InvalidMerkleBranchSizeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1478,7 +1489,9 @@ impl InvalidMerkleTreeDepthError {
pub fn invalid_merkle_tree_depth(&self) -> usize { self.0 } pub fn invalid_merkle_tree_depth(&self) -> usize { self.0 }
} }
internals::impl_from_infallible!(InvalidMerkleTreeDepthError); impl From<Infallible> for InvalidMerkleTreeDepthError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for InvalidMerkleTreeDepthError { impl fmt::Display for InvalidMerkleTreeDepthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1502,7 +1515,9 @@ impl InvalidTaprootLeafVersionError {
pub fn invalid_leaf_version(&self) -> u8 { self.0 } pub fn invalid_leaf_version(&self) -> u8 { self.0 }
} }
internals::impl_from_infallible!(InvalidTaprootLeafVersionError); impl From<Infallible> for InvalidTaprootLeafVersionError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for InvalidTaprootLeafVersionError { impl fmt::Display for InvalidTaprootLeafVersionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -1522,7 +1537,9 @@ impl InvalidControlBlockSizeError {
pub fn invalid_control_block_size(&self) -> usize { self.0 } pub fn invalid_control_block_size(&self) -> usize { self.0 }
} }
internals::impl_from_infallible!(InvalidControlBlockSizeError); impl From<Infallible> for InvalidControlBlockSizeError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for InvalidControlBlockSizeError { impl fmt::Display for InvalidControlBlockSizeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

View File

@ -15,65 +15,6 @@ macro_rules! const_assert {
} }
} }
/// Derives `From<core::convert::Infallible>` for the given type.
///
/// Supports types with arbitrary combinations of lifetimes and type parameters.
///
/// Note: Paths are not supported (for ex. impl_from_infallible!(Hello<D: std::fmt::Display>).
///
/// # Examples
///
/// ```rust
/// # #[allow(unused)]
/// # fn main() {
/// # use core::fmt::{Display, Debug};
/// use bitcoin_internals::impl_from_infallible;
///
/// enum AlphaEnum { Item }
/// impl_from_infallible!(AlphaEnum);
///
/// enum BetaEnum<'b> { Item(&'b usize) }
/// impl_from_infallible!(BetaEnum<'b>);
///
/// enum GammaEnum<T> { Item(T) }
/// impl_from_infallible!(GammaEnum<T>);
///
/// enum DeltaEnum<'b, 'a: 'static + 'b, T: 'a, D: Debug + Display + 'a> {
/// Item((&'b usize, &'a usize, T, D))
/// }
/// impl_from_infallible!(DeltaEnum<'b, 'a: 'static + 'b, T: 'a, D: Debug + Display + 'a>);
///
/// struct AlphaStruct;
/// impl_from_infallible!(AlphaStruct);
///
/// struct BetaStruct<'b>(&'b usize);
/// impl_from_infallible!(BetaStruct<'b>);
///
/// struct GammaStruct<T>(T);
/// impl_from_infallible!(GammaStruct<T>);
///
/// struct DeltaStruct<'b, 'a: 'static + 'b, T: 'a, D: Debug + Display + 'a> {
/// hello: &'a T,
/// what: &'b D,
/// }
/// impl_from_infallible!(DeltaStruct<'b, 'a: 'static + 'b, T: 'a, D: Debug + Display + 'a>);
/// # }
/// ```
///
/// See <https://stackoverflow.com/a/61189128> for more information about this macro.
#[macro_export]
macro_rules! impl_from_infallible {
( $name:ident $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)? ) => {
impl $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?
From<core::convert::Infallible>
for $name
$(< $( $lt ),+ >)?
{
fn from(never: core::convert::Infallible) -> Self { match never {} }
}
}
}
/// Adds an implementation of `pub fn to_hex(&self) -> String` if `alloc` feature is enabled. /// Adds an implementation of `pub fn to_hex(&self) -> String` if `alloc` feature is enabled.
/// ///
/// The added function allocates a `String` then calls through to [`core::fmt::LowerHex`]. /// The added function allocates a `String` then calls through to [`core::fmt::LowerHex`].

View File

@ -23,6 +23,7 @@
//! ``` //! ```
use core::fmt; use core::fmt;
use core::convert::Infallible;
pub mod as_sat_per_kwu { pub mod as_sat_per_kwu {
//! Serialize and deserialize [`FeeRate`] denominated in satoshis per 1000 weight units. //! Serialize and deserialize [`FeeRate`] denominated in satoshis per 1000 weight units.
@ -243,7 +244,9 @@ pub mod as_sat_per_vb_ceil {
#[non_exhaustive] #[non_exhaustive]
pub struct OverflowError; pub struct OverflowError;
internals::impl_from_infallible!(OverflowError); impl From<Infallible> for OverflowError {
fn from(never: Infallible) -> Self { match never {} }
}
impl fmt::Display for OverflowError { impl fmt::Display for OverflowError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {