units: Implement constructor for decoders
What changed, and why it matters
This commit adds public constructors and Default implementations for several data decoder types in the rust-bitcoin 'units' crate. It is a straightforward API usability improvement for downstream users and does not change any decoding logic, parsing rules, or security behavior.
No security action needed. Review as normal API addition if desired.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds new() constructors and Default trait implementations to AmountDecoder, BlockHeightDecoder, LockTimeDecoder, SequenceDecoder, and BlockTimeDecoder. These are thin wrappers around encoding::ArrayDecoder<N>. No logic is modified; the change only exposes construction APIs that were previously inaccessible.
Changed components
units/src/amount/unsigned.rsunits/src/block.rsunits/src/locktime/absolute/mod.rsunits/src/sequence.rsunits/src/time.rsInspect captured patch +55 / −0
diff --git a/units/src/amount/unsigned.rs b/units/src/amount/unsigned.rs
index fa47d158..301ee8ee 100644
--- a/units/src/amount/unsigned.rs
+++ b/units/src/amount/unsigned.rs
@@ -582,6 +582,17 @@ impl encoding::Encodable for Amount {
#[cfg(feature = "encoding")]
pub struct AmountDecoder(encoding::ArrayDecoder<8>);
+#[cfg(feature = "encoding")]
+impl AmountDecoder {
+ /// Constructs a new [`Amount`] decoder.
+ pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+}
+
+#[cfg(feature = "encoding")]
+impl Default for AmountDecoder {
+ fn default() -> Self { Self::new() }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Decoder for AmountDecoder {
type Output = Amount;
diff --git a/units/src/block.rs b/units/src/block.rs
index a6030276..c7ff5fb0 100644
--- a/units/src/block.rs
+++ b/units/src/block.rs
@@ -165,6 +165,17 @@ impl encoding::Encodable for BlockHeight {
#[cfg(feature = "encoding")]
pub struct BlockHeightDecoder(encoding::ArrayDecoder<4>);
+#[cfg(feature = "encoding")]
+impl Default for BlockHeightDecoder {
+ fn default() -> Self { Self::new() }
+}
+
+#[cfg(feature = "encoding")]
+impl BlockHeightDecoder {
+ /// Constructs a new [`BlockHeight`] decoder.
+ pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Decoder for BlockHeightDecoder {
type Output = BlockHeight;
diff --git a/units/src/locktime/absolute/mod.rs b/units/src/locktime/absolute/mod.rs
index a8708a95..b626726a 100644
--- a/units/src/locktime/absolute/mod.rs
+++ b/units/src/locktime/absolute/mod.rs
@@ -423,6 +423,17 @@ impl encoding::Encodable for LockTime {
#[cfg(feature = "encoding")]
pub struct LockTimeDecoder(encoding::ArrayDecoder<4>);
+#[cfg(feature = "encoding")]
+impl LockTimeDecoder {
+ /// Constructs a new [`LockTime`] decoder.
+ pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+}
+
+#[cfg(feature = "encoding")]
+impl Default for LockTimeDecoder {
+ fn default() -> Self { Self::new() }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Decoder for LockTimeDecoder {
type Output = LockTime;
diff --git a/units/src/sequence.rs b/units/src/sequence.rs
index 9085ad1b..938502f2 100644
--- a/units/src/sequence.rs
+++ b/units/src/sequence.rs
@@ -288,6 +288,17 @@ impl encoding::Encodable for Sequence {
#[cfg(feature = "encoding")]
pub struct SequenceDecoder(encoding::ArrayDecoder<4>);
+#[cfg(feature = "encoding")]
+impl Default for SequenceDecoder {
+ fn default() -> Self { Self::new() }
+}
+
+#[cfg(feature = "encoding")]
+impl SequenceDecoder {
+ /// Constructs a new [`Sequence`] decoder.
+ pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Decoder for SequenceDecoder {
type Output = Sequence;
diff --git a/units/src/time.rs b/units/src/time.rs
index fb6921ce..8e6954ed 100644
--- a/units/src/time.rs
+++ b/units/src/time.rs
@@ -98,6 +98,17 @@ impl encoding::Encodable for BlockTime {
#[cfg(feature = "encoding")]
pub struct BlockTimeDecoder(encoding::ArrayDecoder<4>);
+#[cfg(feature = "encoding")]
+impl Default for BlockTimeDecoder {
+ fn default() -> Self { Self::new() }
+}
+
+#[cfg(feature = "encoding")]
+impl BlockTimeDecoder {
+ /// Constructs a new [`BlockTime`] decoder.
+ pub fn new() -> Self { Self(encoding::ArrayDecoder::new()) }
+}
+
#[cfg(feature = "encoding")]
impl encoding::Decoder for BlockTimeDecoder {
type Output = BlockTime;
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.