Improve From<Infallible> impls to the hex_codec errors
What changed, and why it matters
This is a routine code-quality change in a Rust Bitcoin library. It adds and reorganizes implementations of `From<Infallible>` for certain hex-decoding error types. `Infallible` is a Rust type that can never actually be produced, so these conversions are only useful for making the type system happy and have no security impact.
No action required. This is a non-security code-quality/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds From<Infallible> trait implementations for ParseHeaderError, ParseTransactionError, and the generic ParsePrimitiveError<T>. It also moves an existing From<core::convert::Infallible> impl to a conventional location and imports Infallible. These are idiomatic Rust changes to satisfy the crate’s policy that all error types implement From<Infallible>. Since Infallible has no values, the match never {} bodies are unreachable and cannot introduce runtime behavior changes or security vulnerabilities.
Changed components
primitives/src/block.rsprimitives/src/hex_codec.rsprimitives/src/transaction.rsInspect captured patch +16 / −5
diff --git a/primitives/src/block.rs b/primitives/src/block.rs
index cc59e377..3e326106 100644
--- a/primitives/src/block.rs
+++ b/primitives/src/block.rs
@@ -547,6 +547,11 @@ impl fmt::Debug for Header {
#[cfg(feature = "hex")]
pub struct ParseHeaderError(ParsePrimitiveError<Header>);
+#[cfg(feature = "hex")]
+impl From<Infallible> for ParseHeaderError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
#[cfg(feature = "hex")]
impl fmt::Debug for ParseHeaderError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f) }
diff --git a/primitives/src/hex_codec.rs b/primitives/src/hex_codec.rs
index b4edf8d2..b2a75771 100644
--- a/primitives/src/hex_codec.rs
+++ b/primitives/src/hex_codec.rs
@@ -8,6 +8,7 @@
//! associated errors for encoding and decoding `Encodable` types
//! within the primitives crate.
+use core::convert::Infallible;
use core::fmt;
use core::fmt::Write as _;
@@ -160,6 +161,10 @@ pub(crate) enum ParsePrimitiveError<T: Decodable> {
Decode(<T::Decoder as Decoder>::Error),
}
+impl<T: Decodable> From<Infallible> for ParsePrimitiveError<T> {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl<T: Decodable> fmt::Debug for ParsePrimitiveError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -182,10 +187,6 @@ impl<T: Decodable> From<hex_unstable::InvalidCharError> for ParsePrimitiveError<
fn from(err: hex_unstable::InvalidCharError) -> Self { Self::InvalidChar(err) }
}
-impl<T: Decodable> From<core::convert::Infallible> for ParsePrimitiveError<T> {
- fn from(never: core::convert::Infallible) -> Self { match never {} }
-}
-
#[cfg(feature = "std")]
impl<T: Decodable> std::error::Error for ParsePrimitiveError<T> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
@@ -249,4 +250,4 @@ mod tests {
assert_eq!((&hex).into_iter().next(), Some(0u8));
assert!(!format!("{hex:?}").is_empty());
}
-}
\ No newline at end of file
+}
diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs
index ffaa52e2..508c4eea 100644
--- a/primitives/src/transaction.rs
+++ b/primitives/src/transaction.rs
@@ -410,6 +410,11 @@ impl fmt::UpperHex for Transaction {
#[cfg(all(feature = "hex", feature = "alloc"))]
pub struct ParseTransactionError(ParsePrimitiveError<Transaction>);
+#[cfg(all(feature = "hex", feature = "alloc"))]
+impl From<Infallible> for ParseTransactionError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
#[cfg(all(feature = "hex", feature = "alloc"))]
impl fmt::Debug for ParseTransactionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&self.0, f) }
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.