consenus_encoding: Make encoding functions use unsized
What changed, and why it matters
This commit is a routine Rust type-system relaxation. It changes three encoding helper functions so they can accept trait objects and other unsized types, not just fixed-size types. There is no bug fix, behavior change, or security patch here—only a flexibility improvement for callers.
No security action needed. Treat as a normal API ergonomics improvement.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The diff relaxes generic bounds on encode_to_hash_engine, encode_to_vec, and encode_to_writer by moving the Encodable bound into a where clause and adding ?Sized. In Rust, generic function arguments implicitly require Sized; adding ?Sized allows these functions to be called with &dyn Encodable and other dynamically-sized types. The implementation is unchanged, so behavior is identical for existing callers.
Changed components
consensus_encoding/src/encode/mod.rsInspect captured patch +14 / −6
diff --git a/consensus_encoding/src/encode/mod.rs b/consensus_encoding/src/encode/mod.rs
index 3f4cd6df..223006af 100644
--- a/consensus_encoding/src/encode/mod.rs
+++ b/consensus_encoding/src/encode/mod.rs
@@ -69,7 +69,11 @@ macro_rules! encoder_newtype{
///
/// Consumes and returns the hash engine to make it easier to call
/// [`hashes::HashEngine::finalize`] directly on the result.
-pub fn encode_to_hash_engine<T: Encodable, H: hashes::HashEngine>(object: &T, mut engine: H) -> H {
+pub fn encode_to_hash_engine<T, H>(object: &T, mut engine: H) -> H
+where
+ T: Encodable + ?Sized,
+ H: hashes::HashEngine,
+{
let mut encoder = object.encoder();
while let Some(sl) = encoder.current_chunk() {
engine.input(sl);
@@ -80,7 +84,10 @@ pub fn encode_to_hash_engine<T: Encodable, H: hashes::HashEngine>(object: &T, mu
/// Encodes an object into a vector.
#[cfg(feature = "alloc")]
-pub fn encode_to_vec<T: Encodable>(object: &T) -> Vec<u8> {
+pub fn encode_to_vec<T>(object: &T) -> Vec<u8>
+where
+ T: Encodable + ?Sized,
+{
let mut encoder = object.encoder();
let mut vec = Vec::new();
while let Some(chunk) = encoder.current_chunk() {
@@ -103,10 +110,11 @@ pub fn encode_to_vec<T: Encodable>(object: &T) -> Vec<u8> {
///
/// Returns any I/O error encountered while writing to the writer.
#[cfg(feature = "std")]
-pub fn encode_to_writer<T: Encodable, W: std::io::Write>(
- object: &T,
- mut writer: W,
-) -> Result<(), std::io::Error> {
+pub fn encode_to_writer<T, W>(object: &T, mut writer: W) -> Result<(), std::io::Error>
+where
+ T: Encodable + ?Sized,
+ W: std::io::Write,
+{
let mut encoder = object.encoder();
while let Some(chunk) = encoder.current_chunk() {
writer.write_all(chunk)?;
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.