From 2af764e8594e99f3cbebc70cfcf058fe4a24d8bc Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 1 Mar 2024 16:22:05 +1100 Subject: [PATCH 1/3] hashes: Fix leaf error type Leaf error types should typically have private fields, provide accessor functions, and not use `non_exhaustive`. --- hashes/src/lib.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/hashes/src/lib.rs b/hashes/src/lib.rs index d2e7b850..e1381919 100644 --- a/hashes/src/lib.rs +++ b/hashes/src/lib.rs @@ -234,12 +234,19 @@ pub trait Hash: /// Attempted to create a hash from an invalid length slice. #[derive(Debug, Clone, PartialEq, Eq)] -#[non_exhaustive] pub struct FromSliceError { expected: usize, got: usize, } +impl FromSliceError { + /// Returns the expected slice length. + pub fn expected_length(&self) -> usize { self.expected } + + /// Returns the invalid slice length. + pub fn invalid_length(&self) -> usize { self.got } +} + impl fmt::Display for FromSliceError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "invalid slice length {} (expected {})", self.got, self.expected) @@ -247,9 +254,7 @@ impl fmt::Display for FromSliceError { } #[cfg(feature = "std")] -impl std::error::Error for FromSliceError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } -} +impl std::error::Error for FromSliceError {} #[cfg(test)] mod tests { From 43c5eb765c9692f03c8f401908fedd1ac2d67fef Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 1 Mar 2024 17:04:10 +1100 Subject: [PATCH 2/3] Fix witness_version leaf error type Leaf error types should typically have private fields, provide accessor functions, and not use `non_exhaustive`. --- bitcoin/src/blockdata/script/witness_version.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bitcoin/src/blockdata/script/witness_version.rs b/bitcoin/src/blockdata/script/witness_version.rs index 05ee2846..943569fc 100644 --- a/bitcoin/src/blockdata/script/witness_version.rs +++ b/bitcoin/src/blockdata/script/witness_version.rs @@ -245,10 +245,14 @@ impl From for TryFromInstructionError { /// Error attempting to create a [`WitnessVersion`] from an integer. #[derive(Clone, Debug, PartialEq, Eq)] -#[non_exhaustive] pub struct TryFromError { /// The invalid non-witness version integer. - pub invalid: u8, + invalid: u8, +} + +impl TryFromError { + /// Returns the invalid non-witness version integer. + pub fn invalid_version(&self) -> u8 { self.invalid } } impl fmt::Display for TryFromError { @@ -258,6 +262,4 @@ impl fmt::Display for TryFromError { } #[cfg(feature = "std")] -impl std::error::Error for TryFromError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } -} +impl std::error::Error for TryFromError {} From f8de7954b24fa8f020ce2e82e2579f406f8c3eda Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 1 Mar 2024 17:12:52 +1100 Subject: [PATCH 3/3] Remove unused pow::TryFromError type --- bitcoin/src/pow.rs | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/bitcoin/src/pow.rs b/bitcoin/src/pow.rs index e6870677..c8c1eea6 100644 --- a/bitcoin/src/pow.rs +++ b/bitcoin/src/pow.rs @@ -723,22 +723,6 @@ impl> From for U256 { fn from(x: T) -> Self { U256(0, x.into()) } } -/// Error from `TryFrom` implementations, occurs when input is negative. -#[derive(Debug, Clone, PartialEq, Eq)] -#[non_exhaustive] -pub struct TryFromError(i128); - -impl fmt::Display for TryFromError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "attempt to create unsigned integer type from negative number: {}", self.0) - } -} - -#[cfg(feature = "std")] -impl std::error::Error for TryFromError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None } -} - impl Add for U256 { type Output = Self; fn add(self, rhs: Self) -> Self {