Skip to content

Commit

Permalink
Merge #1075: Remove Uninhabited
Browse files Browse the repository at this point in the history
a8e62f2 Remove Uninhabited (Tobin C. Harding)

Pull request description:

  Last release, before we had access to `non_exhaustive` we added some fancy types to enable conditionally having a `bitcoinconsensus::Error`.

  Now that we have bumped the MSRV and have already added `non_exhaustive` to the `Error` type in question, we can use a compiler attribute to conditionally include the `bitcoinconsensus` error.

  Remove `Uninhabited` and its usage.

  For more context see the [original attempt ](#1025 using `Infallible` (last comment in discussion thread).

ACKs for top commit:
  Kixunil:
    ACK a8e62f2
  dunxen:
    ACK a8e62f2
  apoelstra:
    ACK a8e62f2

Tree-SHA512: 8c03c44d7533af1a9a1185b7f9e0fa2c52369eaac8a45f0e2199e7e1bbd08ba2bfa23d829d2c2abf7f45fe8cc26ccad02f2e1a6d408d2c0fbca23140cafe3801
  • Loading branch information
apoelstra committed Jun 29, 2022
2 parents f401cdc + a8e62f2 commit 05f7545
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/blockdata/script.rs
Expand Up @@ -161,38 +161,22 @@ pub enum Error {
/// Tried to read an array off the stack as a number when it was more than 4 bytes
NumericOverflow,
/// Error validating the script with bitcoinconsensus library
BitcoinConsensus(BitcoinConsensusError),
#[cfg(feature = "bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
BitcoinConsensus(bitcoinconsensus::Error),
/// Can not find the spent output
UnknownSpentOutput(OutPoint),
/// Can not serialize the spending transaction
SerializationError
}

/// A [`bitcoinconsensus::Error`] alias. Exists to enable the compiler to ensure `bitcoinconsensus`
/// feature gating is correct.
#[cfg(feature = "bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub type BitcoinConsensusError = bitcoinconsensus::Error;

/// Dummy error type used when `bitcoinconsensus` feature is not enabled.
#[cfg(not(feature = "bitcoinconsensus"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "bitcoinconsensus"))))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct BitcoinConsensusError {
_uninhabited: Uninhabited,
}

#[cfg(not(feature = "bitcoinconsensus"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "bitcoinconsensus"))))]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
enum Uninhabited {}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self {
Error::NonMinimalPush => "non-minimal datapush",
Error::EarlyEndOfScript => "unexpected end of script",
Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)",
#[cfg(feature = "bitcoinconsensus")]
Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed",
Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()",
Error::SerializationError => "can not serialize the spending transaction in Transaction::verify()",
Expand All @@ -211,9 +195,10 @@ impl std::error::Error for Error {
NonMinimalPush
| EarlyEndOfScript
| NumericOverflow
| BitcoinConsensus(_)
| UnknownSpentOutput(_)
| SerializationError => None,
#[cfg(feature = "bitcoinconsensus")]
BitcoinConsensus(_) => None,
}
}
}
Expand Down

0 comments on commit 05f7545

Please sign in to comment.