What changed, and why it matters
This commit is a routine code cleanup in the Rust Bitcoin library. It rewrites a few internal helper methods to use more idiomatic Rust syntax (Option methods and pattern matching) instead of longer if/else blocks. There is no change to what the code actually does, no security fix, and no bug being patched.
No security action needed. Treat as normal refactoring/code-quality improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff refactors three method implementations in the consensus_encoding module. In decoders.rs, ByteVecDecoder::read_limit and VecDecoder::read_limit are rewritten using Option::map_or and a match expression, respectively, preserving the exact same control flow and return values. In encode/mod.rs, Option<T>::advance is replaced with self.as_mut().is_some_and(Encoder::advance), which is semantically equivalent to the previous match. The changes are purely stylistic/idiomatic and do not alter behavior.
Changed components
consensus_encoding/src/decode/decoders.rsconsensus_encoding/src/encode/mod.rsInspect captured patch +14 / −24
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index 372e69cb..2f59f3d9 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -135,11 +135,9 @@ impl Decoder for ByteVecDecoder {
}
fn read_limit(&self) -> usize {
- if let Some(prefix_decoder) = &self.prefix_decoder {
- prefix_decoder.read_limit()
- } else {
- self.bytes_expected - self.bytes_written
- }
+ self.prefix_decoder
+ .as_ref()
+ .map_or(self.bytes_expected - self.bytes_written, CompactSizeDecoder::read_limit)
}
}
@@ -296,19 +294,16 @@ impl<T: Decode> Decoder for VecDecoder<T> {
}
fn read_limit(&self) -> usize {
- if let Some(prefix_decoder) = &self.prefix_decoder {
- prefix_decoder.read_limit()
- } else if let Some(decoder) = &self.decoder {
- decoder.read_limit()
- } else if self.buffer.len() == self.length {
- // Totally done.
- 0
- } else {
- let items_left_to_decode = self.length - self.buffer.len();
- let decoder = T::decoder();
- // This could be inaccurate (eg 1 for a `ByteVecDecoder`) but its the best we can do.
- let limit_per_decoder = decoder.read_limit();
- items_left_to_decode * limit_per_decoder
+ match (&self.prefix_decoder, &self.decoder) {
+ (Some(pd), _) => pd.read_limit(),
+ (None, Some(d)) => d.read_limit(),
+ (None, None) if self.buffer.len() == self.length => 0, // Totally done
+ (None, None) => {
+ let items_left_to_decode = self.length - self.buffer.len();
+ // This could be inaccurate (eg 1 for a `ByteVecDecoder`) but its the best we can do.
+ let limit_per_decoder = T::decoder().read_limit();
+ items_left_to_decode * limit_per_decoder
+ }
}
}
}
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index f70db839..5b30d5fd 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -355,10 +355,5 @@ impl<T: Encoder> Encoder for Option<T> {
}
}
- fn advance(&mut self) -> bool {
- match self {
- Some(encoder) => encoder.advance(),
- None => false,
- }
- }
+ fn advance(&mut self) -> bool { self.as_mut().is_some_and(Encoder::advance) }
}
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.