Move From<Infallible> impls below type
What changed, and why it matters
This commit only moves existing code around. It reorders where certain `From<Infallible>` implementations and error trait implementations appear in the source files so they follow a consistent layout convention. No behavior, logic, or security properties of the code are changed.
No action required; this is a non-functional style refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff is a pure refactor: impl From<Infallible> blocks are relocated below their associated type definitions and below any inherent impl blocks for error types. In a few files, Display and std::error::Error impls are also reordered to match the documented convention. The bodies of every moved impl are identical to the originals, and no new impls or changes to signatures are introduced.
Changed components
base58/src/error.rsbitcoin/src/bip32.rsbitcoin/src/crypto/sighash.rsbitcoin/src/taproot/mod.rsunits/src/amount/error.rsunits/src/locktime/absolute/error.rsunits/src/result.rsInspect captured patch +76 / −75
diff --git a/base58/src/error.rs b/base58/src/error.rs
index 1b6e2892..8838fb73 100644
--- a/base58/src/error.rs
+++ b/base58/src/error.rs
@@ -21,14 +21,6 @@ pub(super) enum ErrorInner {
TooShort(TooShortError),
}
-impl From<Infallible> for Error {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl From<Infallible> for ErrorInner {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl Error {
/// Returns the invalid base58 character, if encountered.
pub fn invalid_character(&self) -> Option<u8> {
@@ -55,6 +47,14 @@ impl Error {
}
}
+impl From<Infallible> for Error {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
+impl From<Infallible> for ErrorInner {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ErrorInner::{Decode, IncorrectChecksum, TooShort};
@@ -124,6 +124,7 @@ pub(super) struct TooShortError {
/// The length of the decoded data.
pub(super) length: usize,
}
+
impl From<Infallible> for TooShortError {
fn from(never: Infallible) -> Self { match never {} }
}
@@ -150,6 +151,13 @@ pub(super) struct InvalidCharacterErrorInner {
pub(super) invalid: u8,
}
+impl InvalidCharacterError {
+ pub(super) fn new(invalid: u8) -> Self { Self(InvalidCharacterErrorInner { invalid }) }
+
+ /// Returns the invalid base58 character.
+ pub fn invalid_character(&self) -> u8 { self.0.invalid }
+}
+
impl From<Infallible> for InvalidCharacterError {
fn from(never: Infallible) -> Self { match never {} }
}
@@ -158,13 +166,6 @@ impl From<Infallible> for InvalidCharacterErrorInner {
fn from(never: Infallible) -> Self { match never {} }
}
-impl InvalidCharacterError {
- pub(super) fn new(invalid: u8) -> Self { Self(InvalidCharacterErrorInner { invalid }) }
-
- /// Returns the invalid base58 character.
- pub fn invalid_character(&self) -> u8 { self.0.invalid }
-}
-
impl fmt::Display for InvalidCharacterError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid base58 character {:#x}", self.0.invalid)
diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs
index bd73e714..1c6f31e4 100644
--- a/bitcoin/src/bip32.rs
+++ b/bitcoin/src/bip32.rs
@@ -665,9 +665,6 @@ pub struct IndexOutOfRangeError {
pub index: u32,
}
-#[cfg(feature = "std")]
-impl std::error::Error for IndexOutOfRangeError {}
-
impl From<Infallible> for IndexOutOfRangeError {
fn from(never: Infallible) -> Self { match never {} }
}
@@ -678,6 +675,9 @@ impl fmt::Display for IndexOutOfRangeError {
}
}
+#[cfg(feature = "std")]
+impl std::error::Error for IndexOutOfRangeError {}
+
/// Error parsing a child number.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseChildNumberError {
@@ -691,21 +691,21 @@ impl From<Infallible> for ParseChildNumberError {
fn from(never: Infallible) -> Self { match never {} }
}
-#[cfg(feature = "std")]
-impl std::error::Error for ParseChildNumberError {
- fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+impl fmt::Display for ParseChildNumberError {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
- Self::IndexOutOfRange(ref e) => Some(e),
- Self::ParseInt(ref e) => Some(e),
+ Self::IndexOutOfRange(ref e) => e.fmt(f),
+ Self::ParseInt(ref e) => e.fmt(f),
}
}
}
-impl fmt::Display for ParseChildNumberError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+#[cfg(feature = "std")]
+impl std::error::Error for ParseChildNumberError {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
- Self::IndexOutOfRange(ref e) => e.fmt(f),
- Self::ParseInt(ref e) => e.fmt(f),
+ Self::IndexOutOfRange(ref e) => Some(e),
+ Self::ParseInt(ref e) => Some(e),
}
}
}
diff --git a/bitcoin/src/crypto/sighash.rs b/bitcoin/src/crypto/sighash.rs
index 0c3ed66e..d47a69de 100644
--- a/bitcoin/src/crypto/sighash.rs
+++ b/bitcoin/src/crypto/sighash.rs
@@ -1293,10 +1293,6 @@ impl From<Infallible> for P2wpkhError {
fn from(never: Infallible) -> Self { match never {} }
}
-impl From<transaction::InputsIndexError> for P2wpkhError {
- fn from(value: transaction::InputsIndexError) -> Self { Self::Sighash(value) }
-}
-
impl fmt::Display for P2wpkhError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -1316,6 +1312,10 @@ impl std::error::Error for P2wpkhError {
}
}
+impl From<transaction::InputsIndexError> for P2wpkhError {
+ fn from(value: transaction::InputsIndexError) -> Self { Self::Sighash(value) }
+}
+
/// Using `SIGHASH_SINGLE` requires an output at the same index as the input.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
@@ -1460,10 +1460,6 @@ pub enum SigningDataError<E> {
Sighash(E),
}
-impl<E> From<Infallible> for SigningDataError<E> {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl<E> SigningDataError<E> {
/// Returns the sighash variant, panicking if it's I/O.
///
@@ -1478,10 +1474,8 @@ impl<E> SigningDataError<E> {
fn sighash<E2: Into<E>>(error: E2) -> Self { Self::Sighash(error.into()) }
}
-// We cannot simultaneously impl `From<E>`. it was determined that this alternative requires less
-// manual `map_err` calls.
-impl<E> From<io::Error> for SigningDataError<E> {
- fn from(value: io::Error) -> Self { Self::Io(value) }
+impl<E> From<Infallible> for SigningDataError<E> {
+ fn from(never: Infallible) -> Self { match never {} }
}
impl<E: fmt::Display> fmt::Display for SigningDataError<E> {
@@ -1503,6 +1497,12 @@ impl<E: std::error::Error + 'static> std::error::Error for SigningDataError<E> {
}
}
+// We cannot simultaneously impl `From<E>`. it was determined that this alternative requires less
+// manual `map_err` calls.
+impl<E> From<io::Error> for SigningDataError<E> {
+ fn from(value: io::Error) -> Self { Self::Io(value) }
+}
+
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for EcdsaSighashType {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
diff --git a/bitcoin/src/taproot/mod.rs b/bitcoin/src/taproot/mod.rs
index 7b0b646d..c7aeb88a 100644
--- a/bitcoin/src/taproot/mod.rs
+++ b/bitcoin/src/taproot/mod.rs
@@ -657,10 +657,6 @@ pub enum IncompleteBuilderError {
HiddenParts(TaprootBuilder),
}
-impl From<Infallible> for IncompleteBuilderError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl IncompleteBuilderError {
/// Converts error into the original incomplete [`TaprootBuilder`] instance.
pub fn into_builder(self) -> TaprootBuilder {
@@ -670,6 +666,10 @@ impl IncompleteBuilderError {
}
}
+impl From<Infallible> for IncompleteBuilderError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl core::fmt::Display for IncompleteBuilderError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(match self {
diff --git a/units/src/amount/error.rs b/units/src/amount/error.rs
index 96830c24..7f098334 100644
--- a/units/src/amount/error.rs
+++ b/units/src/amount/error.rs
@@ -133,10 +133,6 @@ pub struct OutOfRangeError {
pub(super) is_greater_than_max: bool,
}
-impl From<Infallible> for OutOfRangeError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl OutOfRangeError {
/// Returns the minimum and maximum allowed values for the type that was parsed.
///
@@ -175,6 +171,10 @@ impl OutOfRangeError {
}
}
+impl From<Infallible> for OutOfRangeError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for OutOfRangeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_greater_than_max {
diff --git a/units/src/locktime/absolute/error.rs b/units/src/locktime/absolute/error.rs
index 8fe6d581..05837ff5 100644
--- a/units/src/locktime/absolute/error.rs
+++ b/units/src/locktime/absolute/error.rs
@@ -43,10 +43,6 @@ pub struct IncompatibleHeightError {
pub(super) incompatible: Height,
}
-impl From<Infallible> for IncompatibleHeightError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl IncompatibleHeightError {
/// Returns the value of the lock-by-time lock.
pub fn lock(&self) -> MedianTimePast { self.lock }
@@ -55,6 +51,10 @@ impl IncompatibleHeightError {
pub fn incompatible(&self) -> Height { self.incompatible }
}
+impl From<Infallible> for IncompatibleHeightError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for IncompatibleHeightError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -78,10 +78,6 @@ pub struct IncompatibleTimeError {
pub(super) incompatible: MedianTimePast,
}
-impl From<Infallible> for IncompatibleTimeError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl IncompatibleTimeError {
/// Returns the value of the lock-by-height lock.
pub fn lock(&self) -> Height { self.lock }
@@ -90,6 +86,10 @@ impl IncompatibleTimeError {
pub fn incompatible(&self) -> MedianTimePast { self.incompatible }
}
+impl From<Infallible> for IncompatibleTimeError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for IncompatibleTimeError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -108,6 +108,10 @@ impl std::error::Error for IncompatibleTimeError {}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParseHeightError(ParseError);
+impl From<Infallible> for ParseHeightError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for ParseHeightError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.display(f, "block height", 0, LOCK_TIME_THRESHOLD - 1)
@@ -128,6 +132,10 @@ impl From<ParseError> for ParseHeightError {
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ParseTimeError(ParseError);
+impl From<Infallible> for ParseTimeError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for ParseTimeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.display(f, "block time", LOCK_TIME_THRESHOLD, u32::MAX)
@@ -158,18 +166,6 @@ pub(super) enum ParseError {
Conversion(i64),
}
-impl From<Infallible> for ParseError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl From<Infallible> for ParseHeightError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
-impl From<Infallible> for ParseTimeError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl ParseError {
pub(super) fn invalid_int<S: Into<InputString>>(
s: S,
@@ -252,6 +248,10 @@ impl ParseError {
}
}
+impl From<Infallible> for ParseError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl From<ConversionError> for ParseError {
fn from(value: ConversionError) -> Self { Self::Conversion(value.input.into()) }
}
@@ -274,10 +274,6 @@ pub struct ConversionError {
input: u32,
}
-impl From<Infallible> for ConversionError {
- fn from(never: Infallible) -> Self { match never {} }
-}
-
impl ConversionError {
/// Constructs a new `ConversionError` from an invalid `n` when expecting a height value.
pub(super) const fn invalid_height(n: u32) -> Self {
@@ -290,6 +286,10 @@ impl ConversionError {
}
}
+impl From<Infallible> for ConversionError {
+ fn from(never: Infallible) -> Self { match never {} }
+}
+
impl fmt::Display for ConversionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "invalid lock time value {}, {}", self.input, self.unit)
diff --git a/units/src/result.rs b/units/src/result.rs
index 6f6971f7..42f293b9 100644
--- a/units/src/result.rs
+++ b/units/src/result.rs
@@ -400,10 +400,6 @@ pub mod error {
#[non_exhaustive]
pub struct NumOpError(pub(super) MathOp);
- impl From<Infallible> for NumOpError {
- fn from(never: Infallible) -> Self { match never {} }
- }
-
impl NumOpError {
/// Constructs a [`NumOpError`] caused by `op`.
pub(crate) const fn while_doing(op: MathOp) -> Self { Self(op) }
@@ -418,6 +414,10 @@ pub mod error {
pub fn operation(self) -> MathOp { self.0 }
}
+ impl From<Infallible> for NumOpError {
+ fn from(never: Infallible) -> Self { match never {} }
+ }
+
impl fmt::Display for NumOpError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "math operation '{}' gave an invalid numeric result", self.operation())
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.