Refactor Encoder and ExactSizeIterator Option impls
What changed, and why it matters
This commit is a straightforward code cleanup in the Rust Bitcoin library. It replaces a few verbose 'match' statements with shorter, more idiomatic Option helper methods. There is no functional change and no security relevance.
No action required. This is a non-functional readability refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors three trait implementations (Encoder and ExactSizeEncoder for Option
Changed components
consensus_encoding/src/encode/mod.rsInspect captured patch +3 / −16
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 79b9f93e..c859ce47 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -474,26 +474,13 @@ pub fn check_encoder<T: Encoder + ?Sized>(encoder: &mut T, mut expected: &[u8])
}
impl<T: Encoder> Encoder for Option<T> {
- fn current_chunk(&self) -> &[u8] {
- match self {
- Some(encoder) => encoder.current_chunk(),
- None => &[],
- }
- }
+ fn current_chunk(&self) -> &[u8] { self.as_ref().map_or(&[], Encoder::current_chunk) }
fn advance(&mut self) -> EncoderStatus {
- match self {
- Some(encoder) => encoder.advance(),
- None => EncoderStatus::Finished,
- }
+ self.as_mut().map_or(EncoderStatus::Finished, Encoder::advance)
}
}
impl<T: ExactSizeEncoder> ExactSizeEncoder for Option<T> {
- fn len(&self) -> usize {
- match self {
- Some(encoder) => encoder.len(),
- None => 0,
- }
- }
+ fn len(&self) -> usize { self.as_ref().map_or(0, T::len) }
}
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.