Call `*_to_vec` instead of manually looping
What changed, and why it matters
This is a small code cleanup in test files only. It replaces hand-written loops with existing helper functions that do the same thing. There is no security issue and no change to production code.
No action needed. This is a non-security test refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit modifies consensus_encoding/tests/composition.rs to use encode_to_vec and drain_to_vec helper functions instead of manually looping over encoder chunks. The change is purely a test refactor, gated behind #[cfg(feature = “alloc”)]. No production logic, API behavior, or cryptographic operations are changed.
Changed components
consensus_encoding/tests/composition.rsInspect captured patch +6 / −16
diff --git a/consensus_encoding/tests/composition.rs b/consensus_encoding/tests/composition.rs
index 6231f3bb..5bdbfa48 100644
--- a/consensus_encoding/tests/composition.rs
+++ b/consensus_encoding/tests/composition.rs
@@ -6,6 +6,8 @@ use bitcoin_consensus_encoding::{
ArrayDecoder, ArrayEncoder, BytesEncoder, Decode, Decoder, Decoder2, Decoder2Error, Decoder6,
Encode, Encoder, Encoder2, Encoder3, Encoder6, UnexpectedEofError,
};
+#[cfg(feature = "alloc")]
+use bitcoin_consensus_encoding::{drain_to_vec, encode_to_vec};
const EMPTY: &[u8] = &[];
@@ -75,18 +77,11 @@ impl Decode for CompositeData {
type Decoder = CompositeDataDecoder;
}
+#[cfg(feature = "alloc")]
#[test]
fn composition_chain() {
let original = CompositeData { first: [0x01, 0x02, 0x03, 0x04], second: [0x05, 0x06] };
- // Encode using the pull encoder.
- let mut encoder = original.encoder();
- let mut encoded_bytes = Vec::new();
- loop {
- encoded_bytes.extend_from_slice(encoder.current_chunk());
- if !encoder.advance() {
- break;
- }
- }
+ let encoded_bytes = encode_to_vec(&original);
// Decode using the push decoder.
let mut decoder = CompositeData::decoder();
let mut bytes = &encoded_bytes[..];
@@ -97,6 +92,7 @@ fn composition_chain() {
assert_eq!(original, decoded);
}
+#[cfg(feature = "alloc")]
#[test]
fn composition_nested() {
let data = b"abcdef";
@@ -109,13 +105,7 @@ fn composition_nested() {
ArrayEncoder::without_length_prefix([data[5]]),
);
- let mut encoded_bytes = Vec::new();
- loop {
- encoded_bytes.extend_from_slice(encoder6.current_chunk());
- if !encoder6.advance() {
- break;
- }
- }
+ let encoded_bytes = drain_to_vec(&mut encoder6);
assert_eq!(encoded_bytes, data);
let mut decoder6: Decoder6<_, _, _, _, _, _> = Decoder6::new(
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.