Merge rust-bitcoin/rust-bitcoin#881: Remove feature gated enum variants

6ad2902814 Remove feature gated enum variants (Tobin Harding)

Pull request description:

  This is the updated version of #874 (which I closed, force pushed, and then was unable to re-open - my bad).

  Feature gating enum variants makes code that uses the library brittle while we do not have `non_exhaustive`, we should avoid doing so. Instead we can add a dummy type that is available when the feature is not turned on. Doing so enables the compiler to enforce that we do not create the error type that is feature gated when the feature is not enabled.

  Remove the feature gating around `bitcoinconsensus` error enum variants.

  Closes: #645

ACKs for top commit:
  sanket1729:
    tACK 6ad2902814. This is an improvment.
  dr-orlovsky:
    ACK 6ad2902814

Tree-SHA512: 07d8c6b500d2d5b92e367b89e296b86bec046bab4fe9f624eb087d52ea24a900d7f7a41a98065949c67b307a1f374a7f4cf1b77cb93b6cf19e3d779c27fd7f1d
This commit is contained in:
Dr. Maxim Orlovsky 2022-03-24 14:49:10 +02:00
commit d263c0c31e
No known key found for this signature in database
GPG Key ID: AF91255DEA466640
1 changed files with 23 additions and 15 deletions

View File

@ -38,7 +38,7 @@ use hashes::{Hash, hex};
use policy::DUST_RELAY_TX_FEE; use policy::DUST_RELAY_TX_FEE;
#[cfg(feature="bitcoinconsensus")] use bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] use bitcoinconsensus;
#[cfg(feature="bitcoinconsensus")] use core::convert::From; #[cfg(feature="bitcoinconsensus")] use core::convert::From;
#[cfg(feature="bitcoinconsensus")] use OutPoint; use OutPoint;
use util::key::PublicKey; use util::key::PublicKey;
use util::address::WitnessVersion; use util::address::WitnessVersion;
@ -146,30 +146,40 @@ pub enum Error {
/// Tried to read an array off the stack as a number when it was more than 4 bytes /// Tried to read an array off the stack as a number when it was more than 4 bytes
NumericOverflow, NumericOverflow,
/// Error validating the script with bitcoinconsensus library /// Error validating the script with bitcoinconsensus library
#[cfg(feature = "bitcoinconsensus")] BitcoinConsensus(BitcoinConsensusError),
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
BitcoinConsensus(bitcoinconsensus::Error),
/// Can not find the spent output /// Can not find the spent output
#[cfg(feature = "bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
UnknownSpentOutput(OutPoint), UnknownSpentOutput(OutPoint),
/// Can not serialize the spending transaction /// Can not serialize the spending transaction
#[cfg(feature = "bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
SerializationError 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 { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let str = match *self { let str = match *self {
Error::NonMinimalPush => "non-minimal datapush", Error::NonMinimalPush => "non-minimal datapush",
Error::EarlyEndOfScript => "unexpected end of script", Error::EarlyEndOfScript => "unexpected end of script",
Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)", Error::NumericOverflow => "numeric overflow (number on stack larger than 4 bytes)",
#[cfg(feature = "bitcoinconsensus")]
Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed", Error::BitcoinConsensus(ref _n) => "bitcoinconsensus verification failed",
#[cfg(feature = "bitcoinconsensus")]
Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()", Error::UnknownSpentOutput(ref _point) => "unknown spent output Transaction::verify()",
#[cfg(feature = "bitcoinconsensus")]
Error::SerializationError => "can not serialize the spending transaction in Transaction::verify()", Error::SerializationError => "can not serialize the spending transaction in Transaction::verify()",
}; };
f.write_str(str) f.write_str(str)
@ -196,13 +206,11 @@ impl From<UintError> for Error {
} }
} }
#[cfg(feature="bitcoinconsensus")] #[cfg(feature = "bitcoinconsensus")]
#[doc(hidden)] #[doc(hidden)]
impl From<bitcoinconsensus::Error> for Error { impl From<bitcoinconsensus::Error> for Error {
fn from(err: bitcoinconsensus::Error) -> Error { fn from(err: bitcoinconsensus::Error) -> Error {
match err { Error::BitcoinConsensus(err)
_ => Error::BitcoinConsensus(err)
}
} }
} }
/// Helper to encode an integer in script format /// Helper to encode an integer in script format