Add new() constructor to CompactSizeDecoder
What changed, and why it matters
This commit is a routine code cleanup. It adds a `new()` constructor to a small helper type called `CompactSizeDecoder` and makes the existing `Default` implementation use that constructor. It does not change behavior, fix a bug, or address any security issue.
No security action needed. Treat as a normal refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch removes the derived Default impl from CompactSizeDecoder and replaces it with an explicit new() constructor plus a Default impl that delegates to new(). Existing call sites (VecDecoder::new() and a unit test) are updated from CompactSizeDecoder::default() to CompactSizeDecoder::new(). The initialized state (buf: ArrayVec::new()) is identical to the previous derived default, so there is no functional change.
Changed components
consensus_encoding/src/decode/decoders.rsInspect captured patch +12 / −3
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index 3dbc3378..1985f18d 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -126,7 +126,7 @@ impl<T: Decodable> VecDecoder<T> {
/// Constructs a new byte decoder.
pub fn new() -> Self {
Self {
- prefix_decoder: Some(CompactSizeDecoder::default()),
+ prefix_decoder: Some(CompactSizeDecoder::new()),
length: 0,
buffer: Vec::new(),
decoder: None,
@@ -602,11 +602,20 @@ where
/// Decodes a compact size encoded integer.
///
/// For more information about decoder see the documentation of the [`Decoder`] trait.
-#[derive(Default, Debug, Clone)]
+#[derive(Debug, Clone)]
pub struct CompactSizeDecoder {
buf: internals::array_vec::ArrayVec<u8, 9>,
}
+impl CompactSizeDecoder {
+ /// Constructs a new compact size decoder.
+ pub fn new() -> Self { Self { buf: internals::array_vec::ArrayVec::new() } }
+}
+
+impl Default for CompactSizeDecoder {
+ fn default() -> Self { Self::new() }
+}
+
impl Decoder for CompactSizeDecoder {
type Output = u64;
type Error = CompactSizeDecoderError;
@@ -934,7 +943,7 @@ mod tests {
let encoded = alloc::vec![0x00, 0xFF, 0xFF];
let mut slice = encoded.as_slice();
- let mut decoder = CompactSizeDecoder::default();
+ let mut decoder = CompactSizeDecoder::new();
assert!(!decoder.push_bytes(&mut slice).unwrap());
let got = decoder.end().unwrap();
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.