Skip to content

Commit 816cd96

Browse files
committed
consenus_encoding: Make encoding functions use unsized
`Self` in `Encodable` is not sized (because the `Self` type within traits is not sized by default). And also generic types as function arguments are sized by default so the args to our encoding functions `(e.g., `encode_to_vec`) are sized but they do not need to be. Add explicit `?Sized` to the encoding functions.
1 parent 9d5ff8a commit 816cd96

File tree

1 file changed

+14
-6
lines changed
  • consensus_encoding/src/encode

1 file changed

+14
-6
lines changed

consensus_encoding/src/encode/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ macro_rules! encoder_newtype{
6969
///
7070
/// Consumes and returns the hash engine to make it easier to call
7171
/// [`hashes::HashEngine::finalize`] directly on the result.
72-
pub fn encode_to_hash_engine<T: Encodable, H: hashes::HashEngine>(object: &T, mut engine: H) -> H {
72+
pub fn encode_to_hash_engine<T, H>(object: &T, mut engine: H) -> H
73+
where
74+
T: Encodable + ?Sized,
75+
H: hashes::HashEngine,
76+
{
7377
let mut encoder = object.encoder();
7478
while let Some(sl) = encoder.current_chunk() {
7579
engine.input(sl);
@@ -80,7 +84,10 @@ pub fn encode_to_hash_engine<T: Encodable, H: hashes::HashEngine>(object: &T, mu
8084

8185
/// Encodes an object into a vector.
8286
#[cfg(feature = "alloc")]
83-
pub fn encode_to_vec<T: Encodable>(object: &T) -> Vec<u8> {
87+
pub fn encode_to_vec<T>(object: &T) -> Vec<u8>
88+
where
89+
T: Encodable + ?Sized,
90+
{
8491
let mut encoder = object.encoder();
8592
let mut vec = Vec::new();
8693
while let Some(chunk) = encoder.current_chunk() {
@@ -103,10 +110,11 @@ pub fn encode_to_vec<T: Encodable>(object: &T) -> Vec<u8> {
103110
///
104111
/// Returns any I/O error encountered while writing to the writer.
105112
#[cfg(feature = "std")]
106-
pub fn encode_to_writer<T: Encodable, W: std::io::Write>(
107-
object: &T,
108-
mut writer: W,
109-
) -> Result<(), std::io::Error> {
113+
pub fn encode_to_writer<T, W>(object: &T, mut writer: W) -> Result<(), std::io::Error>
114+
where
115+
T: Encodable + ?Sized,
116+
W: std::io::Write,
117+
{
110118
let mut encoder = object.encoder();
111119
while let Some(chunk) = encoder.current_chunk() {
112120
writer.write_all(chunk)?;

0 commit comments

Comments
 (0)