What changed, and why it matters
This commit is purely a code-formatting cleanup. It re-indents lines, breaks long statements across multiple lines, and reorders imports to satisfy Rust style rules. No program logic, behavior, or security properties were changed.
No security action needed. Treat as normal code-style maintenance.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff shows only rustfmt-style changes: collapsing/expanding brace blocks, wrapping long function signatures and macro calls, and alphabetizing/reorganizing use statements. There are no semantic edits to encoding/decoding logic, no new error handling, and no API changes beyond import reordering. The EncoderByteIter symbol is added to a re-export list, but it was already defined in the module; this merely exposes it publicly as part of formatting the export block.
Changed components
consensus_encoding/src/compact_size.rsconsensus_encoding/src/decode/decoders.rsconsensus_encoding/src/decode/mod.rsconsensus_encoding/src/encode/mod.rsconsensus_encoding/src/lib.rsconsensus_encoding/tests/api.rsconsensus_encoding/tests/compact_size.rsconsensus_encoding/tests/composition.rsconsensus_encoding/tests/decode.rsconsensus_encoding/tests/encode.rsInspect captured patch +126 / −56
diff --git a/consensus_encoding/src/compact_size.rs b/consensus_encoding/src/compact_size.rs
index 4e44ea95..f76ecd00 100644
--- a/consensus_encoding/src/compact_size.rs
+++ b/consensus_encoding/src/compact_size.rs
@@ -116,9 +116,7 @@ impl Encoder for CompactSizeEncoder {
fn current_chunk(&self) -> &[u8] { &self.buf }
#[inline]
- fn advance(&mut self) -> EncoderStatus {
- EncoderStatus::Finished
- }
+ fn advance(&mut self) -> EncoderStatus { EncoderStatus::Finished }
}
impl ExactSizeEncoder for CompactSizeEncoder {
@@ -262,7 +260,11 @@ fn compact_size_push_bytes(buf: &mut ArrayVec<u8, 9>, bytes: &mut &[u8]) -> Deco
buf.extend_from_slice(&bytes[..to_copy]);
*bytes = &bytes[to_copy..];
- if buf.len() == len { DecoderStatus::Ready } else { DecoderStatus::NeedsMore }
+ if buf.len() == len {
+ DecoderStatus::Ready
+ } else {
+ DecoderStatus::NeedsMore
+ }
}
/// Returns the number of bytes the compact size decoder still needs to read.
diff --git a/consensus_encoding/src/decode/decoders.rs b/consensus_encoding/src/decode/decoders.rs
index 3cf6962e..a39d6264 100644
--- a/consensus_encoding/src/decode/decoders.rs
+++ b/consensus_encoding/src/decode/decoders.rs
@@ -91,7 +91,8 @@ 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(|e| E(Inner::LengthPrefixDecode(e)))?.needs_more()
+ {
self.prefix_decoder = Some(decoder);
return Ok(DecoderStatus::NeedsMore);
}
@@ -240,7 +241,8 @@ impl<T: Decode> Decoder for VecDecoder<T> {
use VecDecoderErrorInner 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(|e| E(Inner::LengthPrefixDecode(e)))?.needs_more()
+ {
self.prefix_decoder = Some(decoder);
return Ok(DecoderStatus::NeedsMore);
}
diff --git a/consensus_encoding/src/decode/mod.rs b/consensus_encoding/src/decode/mod.rs
index 685273d0..07007574 100644
--- a/consensus_encoding/src/decode/mod.rs
+++ b/consensus_encoding/src/decode/mod.rs
@@ -124,14 +124,10 @@ pub enum DecoderStatus {
impl DecoderStatus {
/// Returns `true` if the decoder needs more data to continue.
- pub fn needs_more(&self) -> bool {
- matches!(self, Self::NeedsMore)
- }
+ pub fn needs_more(&self) -> bool { matches!(self, Self::NeedsMore) }
/// Returns `true` if ready to produce decoded value with [`Decoder::end`].
- pub fn is_ready(&self) -> bool {
- matches!(self, Self::Ready)
- }
+ pub fn is_ready(&self) -> bool { matches!(self, Self::Ready) }
}
/// Decodes an object from a byte slice.
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 0fa2e1ca..2456b836 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -122,14 +122,10 @@ pub enum EncoderStatus {
impl EncoderStatus {
/// Returns `true` if `self` is `HasMore`, `false` otherwise.
- pub fn has_more(&self) -> bool {
- matches!(self, Self::HasMore)
- }
+ pub fn has_more(&self) -> bool { matches!(self, Self::HasMore) }
/// Returns `true` if `self` is `Finished`, `false` otherwise.
- pub fn has_finished(&self) -> bool {
- matches!(self, Self::Finished)
- }
+ pub fn has_finished(&self) -> bool { matches!(self, Self::Finished) }
}
/// Implements a newtype around an encoder.
@@ -423,9 +419,20 @@ pub fn check_encoder<T: Encoder + ?Sized>(encoder: &mut T, mut expected: &[u8])
loop {
let chunk = encoder.current_chunk();
- assert!(chunk.len() <= expected.len(), "encoder yielded more bytes ({}) than expected ({})", bytes_processed + chunk.len(), orig_expected_len);
- if let Some((i, _)) = chunk.iter().zip(&expected[..chunk.len()]).enumerate().find(|&(_, (a, b))| a != b) {
- panic!("encoder did not yield expected bytes - difference in chunk #{}, after {} bytes", chunk_number, bytes_processed + i);
+ assert!(
+ chunk.len() <= expected.len(),
+ "encoder yielded more bytes ({}) than expected ({})",
+ bytes_processed + chunk.len(),
+ orig_expected_len
+ );
+ if let Some((i, _)) =
+ chunk.iter().zip(&expected[..chunk.len()]).enumerate().find(|&(_, (a, b))| a != b)
+ {
+ panic!(
+ "encoder did not yield expected bytes - difference in chunk #{}, after {} bytes",
+ chunk_number,
+ bytes_processed + i
+ );
}
bytes_processed += chunk.len();
expected = &expected[chunk.len()..];
@@ -434,7 +441,11 @@ pub fn check_encoder<T: Encoder + ?Sized>(encoder: &mut T, mut expected: &[u8])
break;
}
}
- assert!(expected.is_empty(), "encoder did not yield enough bytes - {} more expected", expected.len());
+ assert!(
+ expected.is_empty(),
+ "encoder did not yield enough bytes - {} more expected",
+ expected.len()
+ );
}
impl<T: Encoder> Encoder for Option<T> {
diff --git a/consensus_encoding/src/lib.rs b/consensus_encoding/src/lib.rs
index 2dff465d..16493970 100644
--- a/consensus_encoding/src/lib.rs
+++ b/consensus_encoding/src/lib.rs
@@ -83,20 +83,24 @@ pub use self::decode::{
decode_from_read, decode_from_read_unbuffered, decode_from_read_unbuffered_with,
};
#[doc(inline)]
-pub use self::decode::{decode_from_slice, decode_from_slice_unbounded, Decode, Decoder, DecoderStatus};
+pub use self::decode::{
+ decode_from_slice, decode_from_slice_unbounded, Decode, Decoder, DecoderStatus,
+};
#[doc(inline)]
pub use self::encode::encoders::{
ArrayEncoder, ArrayRefEncoder, BytesEncoder, Encoder2, Encoder3, Encoder4, Encoder6,
SliceEncoder,
};
+#[doc(inline)]
+pub use self::encode::{
+ check_encode, check_encoder, Encode, Encoder, EncoderByteIter, EncoderStatus, ExactSizeEncoder,
+};
#[cfg(feature = "alloc")]
#[doc(inline)]
pub use self::encode::{drain_to_vec, encode_to_vec};
#[cfg(feature = "std")]
#[doc(inline)]
pub use self::encode::{drain_to_writer, encode_to_writer};
-#[doc(inline)]
-pub use self::encode::{check_encode, check_encoder, Encode, Encoder, EncoderStatus, EncoderByteIter, ExactSizeEncoder};
#[cfg(feature = "alloc")]
#[doc(no_inline)]
pub use self::error::LengthPrefixExceedsMaxError;
diff --git a/consensus_encoding/tests/api.rs b/consensus_encoding/tests/api.rs
index ce2cce6f..3fd580ff 100644
--- a/consensus_encoding/tests/api.rs
+++ b/consensus_encoding/tests/api.rs
@@ -82,7 +82,10 @@ impl Decoder for FooDecoder {
type Output = Foo;
type Error = UnexpectedEofError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes)
}
fn end(self) -> Result<Self::Output, Self::Error> { self.0.end().map(Foo) }
diff --git a/consensus_encoding/tests/compact_size.rs b/consensus_encoding/tests/compact_size.rs
index 65dbf149..4bb6d398 100644
--- a/consensus_encoding/tests/compact_size.rs
+++ b/consensus_encoding/tests/compact_size.rs
@@ -3,10 +3,9 @@
//! Round-trip integration tests for `CompactSize` codec.
use bitcoin_consensus_encoding::{
- check_encode, decode_from_slice, CompactSizeDecoderError, CompactSizeEncoder,
- CompactSizeU64Decoder, Decode, Encode, ExactSizeEncoder,
+ check_encode, decode_from_slice, CompactSizeDecoder, CompactSizeDecoderError,
+ CompactSizeEncoder, CompactSizeU64Decoder, Decode, Decoder, Encode, ExactSizeEncoder,
};
-use bitcoin_consensus_encoding::{CompactSizeDecoder, Decoder};
/// A `usize` value encoded and decoded as a compact size length prefix.
struct CompactSizeUsize(usize);
@@ -27,7 +26,10 @@ impl Decoder for CompactSizeUsizeDecoderWrapper {
type Output = CompactSizeUsize;
type Error = CompactSizeDecoderError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes)
}
@@ -59,7 +61,10 @@ impl Decoder for CompactSizeU64DecoderWrapper {
type Output = CompactSizeU64;
type Error = CompactSizeDecoderError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes)
}
@@ -102,7 +107,10 @@ fn round_trip_usize_three_byte_max() {
fn round_trip_usize_five_byte_min() {
// 0x10000 is the smallest value that requires the 0xFE prefix.
check_encode(&CompactSizeUsize(0x10000), &[0xFE, 0x00, 0x00, 0x01, 0x00]);
- assert_eq!(decode_from_slice::<CompactSizeUsize>(&[0xFE, 0x00, 0x00, 0x01, 0x00]).unwrap().0, 0x10000);
+ assert_eq!(
+ decode_from_slice::<CompactSizeUsize>(&[0xFE, 0x00, 0x00, 0x01, 0x00]).unwrap().0,
+ 0x10000
+ );
}
#[test]
@@ -135,25 +143,51 @@ fn round_trip_u64_three_byte_max() {
fn round_trip_u64_five_byte_min() {
// 0x10000 is the smallest value that requires the 0xFE prefix.
check_encode(&CompactSizeU64(0x10000), &[0xFE, 0x00, 0x00, 0x01, 0x00]);
- assert_eq!(decode_from_slice::<CompactSizeU64>(&[0xFE, 0x00, 0x00, 0x01, 0x00]).unwrap().0, 0x10000);
+ assert_eq!(
+ decode_from_slice::<CompactSizeU64>(&[0xFE, 0x00, 0x00, 0x01, 0x00]).unwrap().0,
+ 0x10000
+ );
}
#[test]
fn round_trip_u64_five_byte_max() {
check_encode(&CompactSizeU64(0xFFFF_FFFF), &[0xFE, 0xFF, 0xFF, 0xFF, 0xFF]);
- assert_eq!(decode_from_slice::<CompactSizeU64>(&[0xFE, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap().0, 0xFFFF_FFFF);
+ assert_eq!(
+ decode_from_slice::<CompactSizeU64>(&[0xFE, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap().0,
+ 0xFFFF_FFFF
+ );
}
#[test]
fn round_trip_u64_nine_byte_min() {
- check_encode(&CompactSizeU64(0x1_0000_0000), &[0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]);
- assert_eq!(decode_from_slice::<CompactSizeU64>(&[0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00]).unwrap().0, 0x1_0000_0000);
+ check_encode(
+ &CompactSizeU64(0x1_0000_0000),
+ &[0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00],
+ );
+ assert_eq!(
+ decode_from_slice::<CompactSizeU64>(&[
+ 0xFF, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
+ ])
+ .unwrap()
+ .0,
+ 0x1_0000_0000
+ );
}
#[test]
fn round_trip_u64_max() {
- check_encode(&CompactSizeU64(u64::MAX), &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]);
- assert_eq!(decode_from_slice::<CompactSizeU64>(&[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]).unwrap().0, u64::MAX);
+ check_encode(
+ &CompactSizeU64(u64::MAX),
+ &[0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF],
+ );
+ assert_eq!(
+ decode_from_slice::<CompactSizeU64>(&[
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+ ])
+ .unwrap()
+ .0,
+ u64::MAX
+ );
}
#[test]
@@ -312,16 +346,21 @@ fn decoder_compact_size_end_incomplete_nine_byte() {
fn encoder_compact_size_len_matches_encoded_bytes() {
// Test that CompactSizeEncoder::len() returns the correct length for various values
let test_cases = [
- (0usize, 1), // Single byte: 0x00
- (252usize, 1), // Single byte: 0xFC
- (253usize, 3), // Three bytes: 0xFD + 2 bytes
- (0xFFFF_usize, 3), // Three bytes: 0xFD + 2 bytes
- (0x1_0000_usize, 5), // Five bytes: 0xFE + 4 bytes
- (0xFFFF_FFFF_usize, 5), // Five bytes: 0xFE + 4 bytes
+ (0usize, 1), // Single byte: 0x00
+ (252usize, 1), // Single byte: 0xFC
+ (253usize, 3), // Three bytes: 0xFD + 2 bytes
+ (0xFFFF_usize, 3), // Three bytes: 0xFD + 2 bytes
+ (0x1_0000_usize, 5), // Five bytes: 0xFE + 4 bytes
+ (0xFFFF_FFFF_usize, 5), // Five bytes: 0xFE + 4 bytes
];
for (value, expected_len) in test_cases {
let encoder = CompactSizeEncoder::new(value);
- assert_eq!(encoder.len(), expected_len, "CompactSizeEncoder::len() mismatch for value {}", value);
+ assert_eq!(
+ encoder.len(),
+ expected_len,
+ "CompactSizeEncoder::len() mismatch for value {}",
+ value
+ );
}
}
diff --git a/consensus_encoding/tests/composition.rs b/consensus_encoding/tests/composition.rs
index 4a054e2e..9f970519 100644
--- a/consensus_encoding/tests/composition.rs
+++ b/consensus_encoding/tests/composition.rs
@@ -3,13 +3,12 @@
//! Test composition of encoders and decoders.
use bitcoin_consensus_encoding::{
- ArrayDecoder, BytesEncoder, check_encoder, Decoder, Decoder2, Decoder2Error, DecoderStatus, Decoder6,
- Encoder3, UnexpectedEofError,
+ check_encoder, ArrayDecoder, BytesEncoder, Decoder, Decoder2, Decoder2Error, Decoder6,
+ DecoderStatus, Encoder3, UnexpectedEofError,
};
#[cfg(feature = "alloc")]
use bitcoin_consensus_encoding::{
- ArrayEncoder, Decode, Encode, Encoder2,
- drain_to_vec, encode_to_vec, Encoder6,
+ drain_to_vec, encode_to_vec, ArrayEncoder, Decode, Encode, Encoder2, Encoder6,
};
#[cfg(feature = "alloc")]
@@ -271,7 +270,10 @@ fn composition_error_unification() {
type Output = [u8; 1];
type Error = NestedError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.inner.push_bytes(bytes).map_err(NestedError::from)
}
diff --git a/consensus_encoding/tests/decode.rs b/consensus_encoding/tests/decode.rs
index 119471a7..b0cf73cd 100644
--- a/consensus_encoding/tests/decode.rs
+++ b/consensus_encoding/tests/decode.rs
@@ -56,7 +56,10 @@ fn decode_array_insufficient_data_error() {
let mut data = &[0xAA, 0xBB][..];
let status = decoder.push_bytes(&mut data).unwrap();
- assert!(status.needs_more(), "ArrayDecoder should need more data after 2 bytes for 5-byte array");
+ assert!(
+ status.needs_more(),
+ "ArrayDecoder should need more data after 2 bytes for 5-byte array"
+ );
assert_eq!(data, EMPTY);
let err = decoder.end().unwrap_err();
@@ -244,7 +247,10 @@ impl Decoder for TestArrayDecoder {
type Output = TestArray;
type Error = UnexpectedEofError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.inner.push_bytes(bytes)
}
@@ -401,7 +407,10 @@ impl Decoder for InnerDecoder {
type Output = Inner;
type Error = UnexpectedEofError;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes)
}
@@ -431,7 +440,10 @@ impl Decoder for TestDecoder {
type Output = Test;
type Error = VecDecoderError<UnexpectedEofError>;
- fn push_bytes(&mut self, bytes: &mut &[u8]) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
+ fn push_bytes(
+ &mut self,
+ bytes: &mut &[u8],
+ ) -> Result<bitcoin_consensus_encoding::DecoderStatus, Self::Error> {
self.0.push_bytes(bytes)
}
diff --git a/consensus_encoding/tests/encode.rs b/consensus_encoding/tests/encode.rs
index 35800b1e..1a40d85c 100644
--- a/consensus_encoding/tests/encode.rs
+++ b/consensus_encoding/tests/encode.rs
@@ -7,8 +7,7 @@ use std::io::{Cursor, Write};
use bitcoin_consensus_encoding::{
check_encode, check_encoder, ArrayEncoder, ArrayRefEncoder, BytesEncoder, Encode, Encoder,
- Encoder2, Encoder3, Encoder4, Encoder6, EncoderByteIter, ExactSizeEncoder,
- SliceEncoder,
+ Encoder2, Encoder3, Encoder4, Encoder6, EncoderByteIter, ExactSizeEncoder, SliceEncoder,
};
struct TestBytes<'a>(&'a [u8]);
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.