consensus_encoding: Add flush_to_* functions
What changed, and why it matters
This commit adds two new helper functions, flush_to_vec and flush_to_writer, to the consensus_encoding crate. They let developers take an already-created Encoder object and write its contents out to a Vec or an I/O writer. The existing encode_to_vec and encode_to_writer functions are refactored to use these new helpers internally. There is no security fix or behavior change; it is a straightforward API convenience addition.
No security action required. Treat as a normal feature/API refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces public functions flush_to_vec and flush_to_writer in consensus_encoding/src/encode/mod.rs and re-exports them from lib.rs. encode_to_vec and encode_to_writer are simplified to create an encoder and delegate to the new flush functions. The logic (loop over current_chunk, extend/write, advance until false) is unchanged. API snapshot files are updated to list the new exports. No unsafe code, no cryptographic changes, no parsing/validation changes.
Changed components
consensus_encoding/src/encode/mod.rsconsensus_encoding/src/lib.rsapi/consensus_encoding/all-features.txtapi/consensus_encoding/alloc-only.txtInspect captured patch +31 / −3
diff --git a/api/consensus_encoding/all-features.txt b/api/consensus_encoding/all-features.txt
index 35d374b5..6b9e87a4 100644
--- a/api/consensus_encoding/all-features.txt
+++ b/api/consensus_encoding/all-features.txt
@@ -410,6 +410,8 @@ pub fn bitcoin_consensus_encoding::decode_from_read_unbuffered_with<T, R, const
pub fn bitcoin_consensus_encoding::decode_from_slice<T>(bytes: &[u8]) -> core::result::Result<T, <<T as bitcoin_consensus_encoding::Decodable>::Decoder as bitcoin_consensus_encoding::Decoder>::Error> where T: bitcoin_consensus_encoding::Decodable
pub fn bitcoin_consensus_encoding::encode_to_vec<T>(object: &T) -> alloc::vec::Vec<u8> where T: bitcoin_consensus_encoding::Encodable + ?core::marker::Sized
pub fn bitcoin_consensus_encoding::encode_to_writer<T, W>(object: &T, writer: W) -> core::result::Result<(), std::io::error::Error> where T: bitcoin_consensus_encoding::Encodable + ?core::marker::Sized, W: std::io::Write
+pub fn bitcoin_consensus_encoding::flush_to_vec<T>(encoder: &mut T) -> alloc::vec::Vec<u8> where T: bitcoin_consensus_encoding::Encoder + ?core::marker::Sized
+pub fn bitcoin_consensus_encoding::flush_to_writer<T, W>(encoder: &mut T, writer: W) -> core::result::Result<(), std::io::error::Error> where T: bitcoin_consensus_encoding::Encoder + ?core::marker::Sized, W: std::io::Write
pub fn core::option::Option<T>::advance(&mut self) -> bool
pub fn core::option::Option<T>::current_chunk(&self) -> &[u8]
pub macro bitcoin_consensus_encoding::encoder_newtype!
diff --git a/api/consensus_encoding/alloc-only.txt b/api/consensus_encoding/alloc-only.txt
index 1528e3bf..992b98e9 100644
--- a/api/consensus_encoding/alloc-only.txt
+++ b/api/consensus_encoding/alloc-only.txt
@@ -374,6 +374,7 @@ pub fn bitcoin_consensus_encoding::VecDecoderError<Err>::fmt(&self, f: &mut core
pub fn bitcoin_consensus_encoding::VecDecoderError<Err>::from(never: core::convert::Infallible) -> Self
pub fn bitcoin_consensus_encoding::decode_from_slice<T>(bytes: &[u8]) -> core::result::Result<T, <<T as bitcoin_consensus_encoding::Decodable>::Decoder as bitcoin_consensus_encoding::Decoder>::Error> where T: bitcoin_consensus_encoding::Decodable
pub fn bitcoin_consensus_encoding::encode_to_vec<T>(object: &T) -> alloc::vec::Vec<u8> where T: bitcoin_consensus_encoding::Encodable + ?core::marker::Sized
+pub fn bitcoin_consensus_encoding::flush_to_vec<T>(encoder: &mut T) -> alloc::vec::Vec<u8> where T: bitcoin_consensus_encoding::Encoder + ?core::marker::Sized
pub fn core::option::Option<T>::advance(&mut self) -> bool
pub fn core::option::Option<T>::current_chunk(&self) -> &[u8]
pub macro bitcoin_consensus_encoding::encoder_newtype!
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index e5c7f98b..0d067077 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -98,6 +98,15 @@ where
T: Encodable + ?Sized,
{
let mut encoder = object.encoder();
+ flush_to_vec(&mut encoder)
+}
+
+/// Flushes the output of an [`Encoder`] into a vector.
+#[cfg(feature = "alloc")]
+pub fn flush_to_vec<T>(encoder: &mut T) -> Vec<u8>
+where
+ T: Encoder + ?Sized,
+{
let mut vec = Vec::new();
loop {
vec.extend_from_slice(encoder.current_chunk());
@@ -121,12 +130,28 @@ where
///
/// Returns any I/O error encountered while writing to the writer.
#[cfg(feature = "std")]
-pub fn encode_to_writer<T, W>(object: &T, mut writer: W) -> Result<(), std::io::Error>
+pub fn encode_to_writer<T, W>(object: &T, writer: W) -> Result<(), std::io::Error>
where
T: Encodable + ?Sized,
W: std::io::Write,
{
let mut encoder = object.encoder();
+ flush_to_writer(&mut encoder, writer)
+}
+
+/// Flushes the output of an [`Encoder`] to a standard I/O writer.
+///
+/// See [`encode_to_writer`] for more information.
+///
+/// # Errors
+///
+/// Returns any I/O error encountered while writing to the writer.
+#[cfg(feature = "std")]
+pub fn flush_to_writer<T, W>(encoder: &mut T, mut writer: W) -> Result<(), std::io::Error>
+where
+ T: Encoder + ?Sized,
+ W: std::io::Write,
+{
loop {
writer.write_all(encoder.current_chunk())?;
if !encoder.advance() {
diff --git a/consensus_encoding/src/lib.rs b/consensus_encoding/src/lib.rs
index 2f5a315b..c45cc5ae 100644
--- a/consensus_encoding/src/lib.rs
+++ b/consensus_encoding/src/lib.rs
@@ -34,9 +34,9 @@ pub use self::decode::{
};
pub use self::decode::{decode_from_slice, Decodable, Decoder};
#[cfg(feature = "alloc")]
-pub use self::encode::encode_to_vec;
+pub use self::encode::{encode_to_vec, flush_to_vec};
#[cfg(feature = "std")]
-pub use self::encode::encode_to_writer;
+pub use self::encode::{encode_to_writer, flush_to_writer};
pub use self::encode::encoders::{
ArrayEncoder, BytesEncoder, CompactSizeEncoder, Encoder2, Encoder3, Encoder4, Encoder6,
SliceEncoder,
Why this scored 16/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.