Merge rust-bitcoin/rust-bitcoin#6867: consensus_encoding: Flatten remaining error constructors
What changed, and why it matters
This commit is a pure code-style refactor. It rewrites how error values are constructed in the consensus encoding/decoding code, replacing direct nested constructor calls with chained `map_err` calls. The pull request explicitly states the goal is readability, and the behavior and public APIs are preserved. There is no security-relevant change.
No action required. This is a routine refactoring commit with no security implications.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change flattens 14 nested error constructor expressions into Err(...).map_err(...) chains across compact_size.rs, decode/decoders.rs, and decode/mod.rs. This is a non-functional refactor: error types, variants, conversion logic, and public API surfaces remain identical. No bounds checks, validation logic, or control flow were altered.
Changed components
consensus_encoding/src/compact_size.rsconsensus_encoding/src/decode/decoders.rsconsensus_encoding/src/decode/mod.rsInspect captured patch +18 / −19
### consensus_encoding/src/compact_size.rs
@@ -175,10 +175,9 @@ impl Decoder for CompactSizeDecoder {
match usize::try_from(dec_value) {
Ok(nsize) if nsize <= self.limit => Ok(nsize),
- _ => Err(CompactSizeDecoderError(E::ValueExceedsLimit(LengthPrefixExceedsMaxError {
- limit: self.limit,
- value: dec_value,
- }))),
+ _ => Err(LengthPrefixExceedsMaxError { limit: self.limit, value: dec_value })
+ .map_err(E::ValueExceedsLimit)
+ .map_err(CompactSizeDecoderError),
}
}
@@ -293,23 +292,23 @@ fn compact_size_decode_u64(buf: &ArrayVec<u8, 9>) -> Result<u64, CompactSizeDeco
PREFIX_U64 => {
let x = u64::from_le_bytes(arr(payload)?);
if x < 0x100_000_000 {
- Err(CompactSizeDecoderError(E::NonMinimal { value: x }))
+ Err(E::NonMinimal { value: x }).map_err(CompactSizeDecoderError)
} else {
Ok(x)
}
}
PREFIX_U32 => {
let x = u32::from_le_bytes(arr(payload)?);
if x < 0x10000 {
- Err(CompactSizeDecoderError(E::NonMinimal { value: x.into() }))
+ Err(E::NonMinimal { value: x.into() }).map_err(CompactSizeDecoderError)
} else {
Ok(x.into())
}
}
PREFIX_U16 => {
let x = u16::from_le_bytes(arr(payload)?);
if x < 0xFD {
- Err(CompactSizeDecoderError(E::NonMinimal { value: x.into() }))
+ Err(E::NonMinimal { value: x.into() }).map_err(CompactSizeDecoderError)
} else {
Ok(x.into())
}
### consensus_encoding/src/decode/decoders.rs
@@ -91,12 +91,12 @@ impl Decoder for ByteVecDecoder {
use ByteVecDecoderErrorInner as Inner;
if let Some(mut decoder) = self.prefix_decoder.take() {
- if decoder.push_bytes(bytes).map_err(|e| E(Inner::LengthPrefixDecode(e)))?.needs_more()
+ if decoder.push_bytes(bytes).map_err(Inner::LengthPrefixDecode).map_err(E)?.needs_more()
{
self.prefix_decoder = Some(decoder);
return Ok(DecoderStatus::NeedsMore);
}
- self.bytes_expected = decoder.end().map_err(|e| E(Inner::LengthPrefixDecode(e)))?;
+ self.bytes_expected = decoder.end().map_err(Inner::LengthPrefixDecode).map_err(E)?;
self.prefix_decoder = None;
// For DoS prevention, let's not allocate all memory upfront.
@@ -131,7 +131,7 @@ impl Decoder for ByteVecDecoder {
return Ok(self.buffer);
};
- Err(E(Inner::UnexpectedEof(UnexpectedEofError { missing })))
+ Err(UnexpectedEofError { missing }).map_err(Inner::UnexpectedEof).map_err(E)
}
fn read_limit(&self) -> usize {
@@ -222,11 +222,11 @@ impl<D: Decoder + Default> Decoder for ExactVecDecoderWith<D> {
self.reserve();
let mut decoder = self.decoder.take().unwrap_or_default();
- if decoder.push_bytes(bytes).map_err(|e| E(Inner::Item(e)))?.needs_more() {
+ if decoder.push_bytes(bytes).map_err(Inner::Item).map_err(E)?.needs_more() {
self.decoder = Some(decoder);
return Ok(DecoderStatus::NeedsMore);
}
- let item = decoder.end().map_err(|e| E(Inner::Item(e)))?;
+ let item = decoder.end().map_err(Inner::Item).map_err(E)?;
self.buffer.push(item);
if self.buffer.len() == self.length {
@@ -250,7 +250,7 @@ impl<D: Decoder + Default> Decoder for ExactVecDecoderWith<D> {
}
let missing = self.length - len;
- Err(VecDecoderError(E::UnexpectedEof(UnexpectedEofError { missing })))
+ Err(UnexpectedEofError { missing }).map_err(E::UnexpectedEof).map_err(VecDecoderError)
}
fn read_limit(&self) -> usize {
@@ -327,11 +327,11 @@ impl<D: Decoder + Default> Decoder for VecDecoderWith<D> {
use VecDecoderErrorInner as Inner;
if let Some(mut pd) = self.prefix_decoder.take() {
- if pd.push_bytes(bytes).map_err(|e| E(Inner::LengthPrefixDecode(e)))?.needs_more() {
+ if pd.push_bytes(bytes).map_err(Inner::LengthPrefixDecode).map_err(E)?.needs_more() {
self.prefix_decoder = Some(pd);
return Ok(DecoderStatus::NeedsMore);
}
- let count = pd.end().map_err(|e| E(Inner::LengthPrefixDecode(e)))?;
+ let count = pd.end().map_err(Inner::LengthPrefixDecode).map_err(E)?;
self.items = ExactVecDecoderWith::new(count);
}
@@ -342,9 +342,9 @@ impl<D: Decoder + Default> Decoder for VecDecoderWith<D> {
use VecDecoderErrorInner as Inner;
if let Some(pd) = self.prefix_decoder {
- return Err(VecDecoderError(Inner::UnexpectedEof(UnexpectedEofError {
- missing: pd.read_limit(),
- })));
+ return Err(UnexpectedEofError { missing: pd.read_limit() })
+ .map_err(Inner::UnexpectedEof)
+ .map_err(VecDecoderError);
}
self.items.end()
### consensus_encoding/src/decode/mod.rs
@@ -272,7 +272,7 @@ fn decode_from_slice_internal<D: Decoder>(
if remaining.is_empty() {
Ok(data)
} else {
- Err(DecodeError::Unconsumed(UnconsumedError()))
+ Err(UnconsumedError()).map_err(DecodeError::Unconsumed)
}
}
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.