What changed, and why it matters
This commit is a pure code cleanup in the rust-bitcoin library's 'units' module. It reorders where encoder and decoder definitions appear in six source files so that the Encodable and Decodable trait implementations come before the macro-generated encoder/decoder structs. No behavior, logic, or security properties change; it only makes the layout consistent.
No security action needed. Treat as a normal refactoring/reordering commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff moves impl encoding::Encodable and impl encoding::Decodable blocks above the encoding::encoder_newtype_exact! and crate::decoder_newtype! macro invocations for Amount, BlockHeight, LockTime, CompactTarget, Sequence, and BlockTime. The generated code and trait implementations remain identical; only source-file ordering is changed. There are no functional modifications.
Changed components
units/src/amount/unsigned.rsunits/src/block.rsunits/src/locktime/absolute/mod.rsunits/src/pow.rsunits/src/sequence.rsunits/src/time.rsInspect captured patch +91 / −84
diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs
index 7b150fa3..2ebf85a5 100644
--- a/units/src/amount/unsigned.rs
+++ b/units/src/amount/unsigned.rs
@@ -613,16 +613,10 @@ impl TryFrom<SignedAmount> for Amount {
fn try_from(value: SignedAmount) -> Result<Self, Self::Error> { value.to_unsigned() }
}
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`Amount`] type.
- #[derive(Debug, Clone)]
- pub struct AmountEncoder<'e>(encoding::ArrayEncoder<8>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for Amount {
type Encoder<'e> = AmountEncoder<'e>;
+
#[inline]
fn encoder(&self) -> Self::Encoder<'_> {
AmountEncoder::new(encoding::ArrayEncoder::without_length_prefix(
@@ -631,6 +625,21 @@ impl encoding::Encodable for Amount {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for Amount {
+ type Decoder = AmountDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { AmountDecoder(encoding::ArrayDecoder::<8>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`Amount`] type.
+ #[derive(Debug, Clone)]
+ pub struct AmountEncoder<'e>(encoding::ArrayEncoder<8>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`Amount`] type.
@@ -651,13 +660,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for Amount {
- type Decoder = AmountDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { AmountDecoder(encoding::ArrayDecoder::<8>::new()) }
-}
-
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for Amount {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
diff --git a/units/src/block.rs b/units/src/block.rs
index 8978df6a..08657b40 100644
--- a/units/src/block.rs
+++ b/units/src/block.rs
@@ -204,13 +204,6 @@ impl TryFrom<BlockHeight> for absolute::Height {
fn try_from(h: BlockHeight) -> Result<Self, Self::Error> { Self::from_u32(h.to_u32()) }
}
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`BlockHeight`] type.
- #[derive(Debug, Clone)]
- pub struct BlockHeightEncoder<'e>(encoding::ArrayEncoder<4>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for BlockHeight {
type Encoder<'e> = BlockHeightEncoder<'e>;
@@ -222,6 +215,21 @@ impl encoding::Encodable for BlockHeight {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for BlockHeight {
+ type Decoder = BlockHeightDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { BlockHeightDecoder(encoding::ArrayDecoder::<4>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`BlockHeight`] type.
+ #[derive(Debug, Clone)]
+ pub struct BlockHeightEncoder<'e>(encoding::ArrayEncoder<4>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`BlockHeight`] type.
@@ -238,13 +246,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for BlockHeight {
- type Decoder = BlockHeightDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { BlockHeightDecoder(encoding::ArrayDecoder::<4>::new()) }
-}
-
impl_u32_wrapper! {
/// An unsigned block interval.
///
diff --git a/units/src/locktime/absolute/mod.rs b/units/src/locktime/absolute/mod.rs
index 99672a24..d2ace8bd 100644
--- a/units/src/locktime/absolute/mod.rs
+++ b/units/src/locktime/absolute/mod.rs
@@ -397,13 +397,6 @@ impl LockTime {
parse_int::impl_parse_str_from_int_infallible!(LockTime, u32, from_consensus);
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`LockTime`] type.
- #[derive(Debug, Clone)]
- pub struct LockTimeEncoder<'e>(encoding::ArrayEncoder<4>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for LockTime {
type Encoder<'e> = LockTimeEncoder<'e>;
@@ -415,6 +408,21 @@ impl encoding::Encodable for LockTime {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for LockTime {
+ type Decoder = LockTimeDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { LockTimeDecoder(encoding::ArrayDecoder::<4>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`LockTime`] type.
+ #[derive(Debug, Clone)]
+ pub struct LockTimeEncoder<'e>(encoding::ArrayEncoder<4>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`LockTime`] type.
@@ -431,13 +439,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for LockTime {
- type Decoder = LockTimeDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { LockTimeDecoder(encoding::ArrayDecoder::<4>::new()) }
-}
-
impl From<Height> for LockTime {
#[inline]
fn from(h: Height) -> Self { Self::Blocks(h) }
diff --git a/units/src/pow.rs b/units/src/pow.rs
index f5d0e1e5..4fb956f2 100644
--- a/units/src/pow.rs
+++ b/units/src/pow.rs
@@ -302,13 +302,6 @@ impl From<CompactTarget> for Target {
fn from(c: CompactTarget) -> Self { Self::from_compact(c) }
}
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`CompactTarget`] type.
- #[derive(Debug, Clone)]
- pub struct CompactTargetEncoder<'e>(encoding::ArrayEncoder<4>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for CompactTarget {
type Encoder<'e> = CompactTargetEncoder<'e>;
@@ -320,6 +313,21 @@ impl encoding::Encodable for CompactTarget {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for CompactTarget {
+ type Decoder = CompactTargetDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { CompactTargetDecoder(encoding::ArrayDecoder::<4>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`CompactTarget`] type.
+ #[derive(Debug, Clone)]
+ pub struct CompactTargetEncoder<'e>(encoding::ArrayEncoder<4>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`CompactTarget`] type.
@@ -336,13 +344,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for CompactTarget {
- type Decoder = CompactTargetDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { CompactTargetDecoder(encoding::ArrayDecoder::<4>::new()) }
-}
-
/// Error types for proof-of-work related integer types.
pub mod error {
use core::convert::Infallible;
diff --git a/units/src/sequence.rs b/units/src/sequence.rs
index 130a695d..83cf8597 100644
--- a/units/src/sequence.rs
+++ b/units/src/sequence.rs
@@ -258,13 +258,6 @@ impl fmt::Debug for Sequence {
parse_int::impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus);
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`Sequence`] type.
- #[derive(Debug, Clone)]
- pub struct SequenceEncoder<'e>(encoding::ArrayEncoder<4>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for Sequence {
type Encoder<'e> = SequenceEncoder<'e>;
@@ -276,6 +269,21 @@ impl encoding::Encodable for Sequence {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for Sequence {
+ type Decoder = SequenceDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { SequenceDecoder(encoding::ArrayDecoder::<4>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`Sequence`] type.
+ #[derive(Debug, Clone)]
+ pub struct SequenceEncoder<'e>(encoding::ArrayEncoder<4>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`Sequence`] type.
@@ -292,13 +300,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for Sequence {
- type Decoder = SequenceDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { SequenceDecoder(encoding::ArrayDecoder::<4>::new()) }
-}
-
/// Error types for input sequence numbers.
pub mod error {
#[cfg(feature = "encoding")]
diff --git a/units/src/time.rs b/units/src/time.rs
index 0305d936..94329a6d 100644
--- a/units/src/time.rs
+++ b/units/src/time.rs
@@ -117,13 +117,6 @@ impl<'de> Deserialize<'de> for BlockTime {
}
}
-#[cfg(feature = "encoding")]
-encoding::encoder_newtype_exact! {
- /// The encoder for the [`BlockTime`] type.
- #[derive(Debug, Clone)]
- pub struct BlockTimeEncoder<'e>(encoding::ArrayEncoder<4>);
-}
-
#[cfg(feature = "encoding")]
impl encoding::Encodable for BlockTime {
type Encoder<'e> = BlockTimeEncoder<'e>;
@@ -135,6 +128,21 @@ impl encoding::Encodable for BlockTime {
}
}
+#[cfg(feature = "encoding")]
+impl encoding::Decodable for BlockTime {
+ type Decoder = BlockTimeDecoder;
+
+ #[inline]
+ fn decoder() -> Self::Decoder { BlockTimeDecoder(encoding::ArrayDecoder::<4>::new()) }
+}
+
+#[cfg(feature = "encoding")]
+encoding::encoder_newtype_exact! {
+ /// The encoder for the [`BlockTime`] type.
+ #[derive(Debug, Clone)]
+ pub struct BlockTimeEncoder<'e>(encoding::ArrayEncoder<4>);
+}
+
#[cfg(feature = "encoding")]
crate::decoder_newtype! {
/// The decoder for the [`BlockTime`] type.
@@ -151,13 +159,6 @@ crate::decoder_newtype! {
}
}
-#[cfg(feature = "encoding")]
-impl encoding::Decodable for BlockTime {
- type Decoder = BlockTimeDecoder;
- #[inline]
- fn decoder() -> Self::Decoder { BlockTimeDecoder(encoding::ArrayDecoder::<4>::new()) }
-}
-
/// Error types for block times.
pub mod error {
#[cfg(feature = "encoding")]
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.