From 42960b959f97c12772e7b936b355881fe5f5d612 Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Thu, 11 Jul 2019 14:56:37 +0000 Subject: [PATCH] eliminate type parameter from `Encodable` trait --- src/blockdata/script.rs | 11 +-- src/blockdata/transaction.rs | 47 ++++++++----- src/consensus/encode.rs | 115 ++++++++++++++++++------------- src/internal_macros.rs | 9 ++- src/network/address.rs | 16 +++-- src/network/message.rs | 38 ++++++---- src/network/message_blockdata.rs | 17 +++-- src/util/merkleblock.rs | 19 ++--- src/util/psbt/macros.rs | 12 +++- src/util/psbt/mod.rs | 21 +++--- src/util/psbt/raw.rs | 26 ++++--- src/util/uint.rs | 9 ++- 12 files changed, 209 insertions(+), 131 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index ec1ae8b1..116c2bdd 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -25,12 +25,12 @@ //! use std::default::Default; -use std::{error, fmt}; +use std::{error, fmt, io}; #[cfg(feature = "serde")] use serde; use blockdata::opcodes; -use consensus::{encode, Decodable, Encodable, ReadExt, WriteExt}; +use consensus::{encode, Decodable, Encodable, ReadExt}; use bitcoin_hashes::{hash160, sha256, Hash}; #[cfg(feature="bitcoinconsensus")] use bitcoinconsensus; #[cfg(feature="bitcoinconsensus")] use std::convert; @@ -725,9 +725,12 @@ impl serde::Serialize for Script { } // Network serialization -impl Encodable for Script { +impl Encodable for Script { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + s: S, + ) -> Result { self.0.consensus_encode(s) } } diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index e7c4675f..3d83d06c 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -25,7 +25,7 @@ use byteorder::{LittleEndian, WriteBytesExt}; use std::default::Default; -use std::fmt; +use std::{fmt, io}; #[cfg(feature="bitcoinconsensus")] use std::collections::HashMap; use bitcoin_hashes::{self, sha256d, Hash}; @@ -34,7 +34,7 @@ use bitcoin_hashes::hex::FromHex; use util::hash::BitcoinHash; #[cfg(feature="bitcoinconsensus")] use blockdata::script; use blockdata::script::Script; -use consensus::{encode, serialize, Decodable, Encodable, ReadExt, WriteExt}; +use consensus::{encode, serialize, Decodable, Encodable, ReadExt}; use VarInt; /// A reference to a transaction output @@ -439,9 +439,12 @@ impl BitcoinHash for Transaction { impl_consensus_encoding!(TxOut, value, script_pubkey); -impl Encodable for OutPoint { - fn consensus_encode(&self, s: &mut S) -> Result { - let len = self.txid.consensus_encode(s)?; +impl Encodable for OutPoint { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { + let len = self.txid.consensus_encode(&mut s)?; Ok(len + self.vout.consensus_encode(s)?) } } @@ -454,11 +457,14 @@ impl Decodable for OutPoint { } } -impl Encodable for TxIn { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for TxIn { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += self.previous_output.consensus_encode(s)?; - len += self.script_sig.consensus_encode(s)?; + len += self.previous_output.consensus_encode(&mut s)?; + len += self.script_sig.consensus_encode(&mut s)?; len += self.sequence.consensus_encode(s)?; Ok(len) } @@ -474,10 +480,13 @@ impl Decodable for TxIn { } } -impl Encodable for Transaction { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for Transaction { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += self.version.consensus_encode(s)?; + len += self.version.consensus_encode(&mut s)?; let mut have_witness = self.input.is_empty(); for input in &self.input { if !input.witness.is_empty() { @@ -486,15 +495,15 @@ impl Encodable for Transaction { } } if !have_witness { - len += self.input.consensus_encode(s)?; - len += self.output.consensus_encode(s)?; + len += self.input.consensus_encode(&mut s)?; + len += self.output.consensus_encode(&mut s)?; } else { - len += 0u8.consensus_encode(s)?; - len += 1u8.consensus_encode(s)?; - len += self.input.consensus_encode(s)?; - len += self.output.consensus_encode(s)?; + len += 0u8.consensus_encode(&mut s)?; + len += 1u8.consensus_encode(&mut s)?; + len += self.input.consensus_encode(&mut s)?; + len += self.output.consensus_encode(&mut s)?; for input in &self.input { - len += input.witness.consensus_encode(s)?; + len += input.witness.consensus_encode(&mut s)?; } } len += self.lock_time.consensus_encode(s)?; diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 02cf6af6..a1e88229 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -199,18 +199,14 @@ impl From for Error { } /// Encode an object into a vector -pub fn serialize(data: &T) -> Vec - where T: Encodable>>, -{ +pub fn serialize(data: &T) -> Vec { let mut encoder = Cursor::new(vec![]); data.consensus_encode(&mut encoder).unwrap(); encoder.into_inner() } /// Encode an object into a hex-encoded string -pub fn serialize_hex(data: &T) -> String - where T: Encodable>> -{ +pub fn serialize_hex(data: &T) -> String { hex_encode(serialize(data)) } @@ -370,11 +366,11 @@ impl ReadExt for R { pub const MAX_VEC_SIZE: usize = 32 * 1024 * 1024; /// Data which can be encoded in a consensus-consistent way -pub trait Encodable { +pub trait Encodable { /// Encode an object with a well-defined format, should only ever error if - /// the underlying WriteExt errors. Returns the number of bytes written on + /// the underlying `Write` errors. Returns the number of bytes written on /// success - fn consensus_encode(&self, e: &mut S) -> Result; + fn consensus_encode(&self, e: W) -> Result; } /// Data which can be encoded in a consensus-consistent way @@ -399,9 +395,12 @@ macro_rules! impl_int_encodable{ fn consensus_decode(d: &mut D) -> Result<$ty, self::Error> { d.$meth_dec().map($ty::from_le) } } - impl Encodable for $ty { + impl Encodable for $ty { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { s.$meth_enc(self.to_le())?; Ok(mem::size_of::<$ty>()) } @@ -433,14 +432,29 @@ impl VarInt { } } -impl Encodable for VarInt { +impl Encodable for VarInt { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { match self.0 { - 0...0xFC => { (self.0 as u8).consensus_encode(s)?; Ok(1) } - 0xFD...0xFFFF => { s.emit_u8(0xFD)?; (self.0 as u16).consensus_encode(s)?; Ok(3) } - 0x10000...0xFFFFFFFF => { s.emit_u8(0xFE)?; (self.0 as u32).consensus_encode(s)?; Ok(5) } - _ => { s.emit_u8(0xFF)?; (self.0 as u64).consensus_encode(s)?; Ok(9) } + 0...0xFC => { + (self.0 as u8).consensus_encode(s)?; + Ok(1) + }, + 0xFD...0xFFFF => { + s.emit_u8(0xFD)?; + (self.0 as u16).consensus_encode(s)?; + Ok(3) + }, + 0x10000...0xFFFFFFFF => { + s.emit_u8(0xFE)?; + (self.0 as u32).consensus_encode(s)?; + Ok(5) + }, + _ => { + s.emit_u8(0xFF)?; + (self.0 as u64).consensus_encode(s)?; + Ok(9) + }, } } } @@ -481,9 +495,9 @@ impl Decodable for VarInt { // Booleans -impl Encodable for bool { +impl Encodable for bool { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { s.emit_u8(if *self {1} else {0})?; Ok(1) } @@ -495,11 +509,11 @@ impl Decodable for bool { } // Strings -impl Encodable for String { +impl Encodable for String { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode(&self, mut s: S) -> Result { let b = self.as_bytes(); - let vi_len = VarInt(b.len() as u64).consensus_encode(s)?; + let vi_len = VarInt(b.len() as u64).consensus_encode(&mut s)?; s.emit_slice(&b)?; Ok(vi_len + b.len()) } @@ -517,9 +531,12 @@ impl Decodable for String { // Arrays macro_rules! impl_array { ( $size:expr ) => ( - impl Encodable for [u8; $size] { + impl Encodable for [u8; $size] { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { s.emit_slice(&self[..])?; Ok(self.len()) } @@ -555,10 +572,10 @@ impl Decodable for [u16; 8] { } } -impl Encodable for [u16; 8] { +impl Encodable for [u16; 8] { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { - for c in self.iter() { c.consensus_encode(s)?; } + fn consensus_encode(&self, mut s: S) -> Result { + for c in self.iter() { c.consensus_encode(&mut s)?; } Ok(16) } } @@ -566,13 +583,16 @@ impl Encodable for [u16; 8] { // Vectors macro_rules! impl_vec { ($type: ty) => { - impl Encodable for Vec<$type> { + impl Encodable for Vec<$type> { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += VarInt(self.len() as u64).consensus_encode(s)?; + len += VarInt(self.len() as u64).consensus_encode(&mut s)?; for c in self.iter() { - len += c.consensus_encode(s)?; + len += c.consensus_encode(&mut s)?; } Ok(len) } @@ -604,10 +624,10 @@ impl_vec!(Vec); impl_vec!((u32, Address)); impl_vec!(u64); -impl Encodable for Vec { +impl Encodable for Vec { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { - let vi_len = VarInt(self.len() as u64).consensus_encode(s)?; + fn consensus_encode(&self, mut s: S) -> Result { + let vi_len = VarInt(self.len() as u64).consensus_encode(&mut s)?; s.emit_slice(&self)?; Ok(vi_len + self.len()) } @@ -628,10 +648,10 @@ impl Decodable for Vec { } } -impl Encodable for Box<[u8]> { +impl Encodable for Box<[u8]> { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { - let vi_len = VarInt(self.len() as u64).consensus_encode(s)?; + fn consensus_encode(&self, mut s: S) -> Result { + let vi_len = VarInt(self.len() as u64).consensus_encode(&mut s)?; s.emit_slice(&self)?; Ok(vi_len + self.len()) } @@ -660,11 +680,11 @@ fn sha2_checksum(data: &[u8]) -> [u8; 4] { } // Checked data -impl Encodable for CheckedData { +impl Encodable for CheckedData { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { - (self.0.len() as u32).consensus_encode(s)?; - sha2_checksum(&self.0).consensus_encode(s)?; + fn consensus_encode(&self, mut s: S) -> Result { + (self.0.len() as u32).consensus_encode(&mut s)?; + sha2_checksum(&self.0).consensus_encode(&mut s)?; s.emit_slice(&self.0)?; Ok(8 + self.0.len()) } @@ -699,13 +719,16 @@ impl Decodable for CheckedData { // Tuples macro_rules! tuple_encode { ($($x:ident),*) => ( - impl ),*> Encodable for ($($x),*) { + impl <$($x: Encodable),*> Encodable for ($($x),*) { #[inline] #[allow(non_snake_case)] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let &($(ref $x),*) = self; let mut len = 0; - $(len += $x.consensus_encode(s)?;)* + $(len += $x.consensus_encode(&mut s)?;)* Ok(len) } } @@ -725,8 +748,8 @@ tuple_encode!(T0, T1, T2, T3); tuple_encode!(T0, T1, T2, T3, T4, T5); tuple_encode!(T0, T1, T2, T3, T4, T5, T6, T7); -impl Encodable for sha256d::Hash { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for sha256d::Hash { + fn consensus_encode(&self, s: S) -> Result { self.into_inner().consensus_encode(s) } } diff --git a/src/internal_macros.rs b/src/internal_macros.rs index 27cbb5af..30fe0bb1 100644 --- a/src/internal_macros.rs +++ b/src/internal_macros.rs @@ -18,11 +18,14 @@ macro_rules! impl_consensus_encoding { ($thing:ident, $($field:ident),+) => ( - impl ::consensus::Encodable for $thing { + impl ::consensus::Encodable for $thing { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - $(len += self.$field.consensus_encode(s)?;)+ + $(len += self.$field.consensus_encode(&mut s)?;)+ Ok(len) } } diff --git a/src/network/address.rs b/src/network/address.rs index 142ae41d..87a91564 100644 --- a/src/network/address.rs +++ b/src/network/address.rs @@ -22,7 +22,7 @@ use std::io; use std::fmt; use std::net::{SocketAddr, Ipv6Addr, SocketAddrV4, SocketAddrV6}; -use consensus::{encode, ReadExt, WriteExt}; +use consensus::{encode, ReadExt}; use consensus::encode::{Decodable, Encodable}; /// A message which can be sent on the Bitcoin network @@ -72,13 +72,15 @@ fn addr_to_be(addr: [u16; 8]) -> [u16; 8] { addr[4].to_be(), addr[5].to_be(), addr[6].to_be(), addr[7].to_be()] } -impl Encodable for Address { +impl Encodable for Address { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { - let mut len = 0; - len += self.services.consensus_encode(s)?; - len += addr_to_be(self.address).consensus_encode(s)?; - len += self.port.to_be().consensus_encode(s)?; + fn consensus_encode( + &self, + mut s: S, + ) -> Result { + let len = self.services.consensus_encode(&mut s)? + + addr_to_be(self.address).consensus_encode(&mut s)? + + self.port.to_be().consensus_encode(s)?; Ok(len) } } diff --git a/src/network/message.rs b/src/network/message.rs index d8dcfd08..de00d783 100644 --- a/src/network/message.rs +++ b/src/network/message.rs @@ -19,7 +19,7 @@ //! also defines (de)serialization routines for many primitives. //! -use std::{iter, mem}; +use std::{io, iter, mem}; use std::io::Cursor; use blockdata::block; @@ -30,16 +30,19 @@ use network::message_blockdata; use network::message_filter; use consensus::encode::{Decodable, Encodable}; use consensus::encode::{CheckedData, VarInt}; -use consensus::{encode, serialize, ReadExt, WriteExt}; +use consensus::{encode, serialize, ReadExt}; use consensus::encode::MAX_VEC_SIZE; /// Serializer for command string #[derive(PartialEq, Eq, Clone, Debug)] pub struct CommandString(pub String); -impl Encodable for CommandString { +impl Encodable for CommandString { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + s: S, + ) -> Result { let &CommandString(ref inner_str) = self; let mut rawbytes = [0u8; 12]; let strbytes = inner_str.as_bytes(); @@ -160,24 +163,31 @@ impl RawNetworkMessage { } struct HeaderSerializationWrapper<'a>(&'a Vec); -impl <'a, S: WriteExt> Encodable for HeaderSerializationWrapper<'a> { + +impl<'a> Encodable for HeaderSerializationWrapper<'a> { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += VarInt(self.0.len() as u64).consensus_encode(s)?; + len += VarInt(self.0.len() as u64).consensus_encode(&mut s)?; for header in self.0.iter() { - len += header.consensus_encode(s)?; - len += 0u8.consensus_encode(s)?; + len += header.consensus_encode(&mut s)?; + len += 0u8.consensus_encode(&mut s)?; } Ok(len) } } -impl Encodable for RawNetworkMessage { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for RawNetworkMessage { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += self.magic.consensus_encode(s)?; - len += CommandString(self.command()).consensus_encode(s)?; + len += self.magic.consensus_encode(&mut s)?; + len += CommandString(self.command()).consensus_encode(&mut s)?; len += CheckedData(match self.payload { NetworkMessage::Version(ref dat) => serialize(dat), NetworkMessage::Addr(ref dat) => serialize(dat), @@ -202,7 +212,7 @@ impl Encodable for RawNetworkMessage { | NetworkMessage::SendHeaders | NetworkMessage::MemPool | NetworkMessage::GetAddr => vec![], - }).consensus_encode(s)?; + }).consensus_encode(&mut s)?; Ok(len) } } diff --git a/src/network/message_blockdata.rs b/src/network/message_blockdata.rs index c74c0543..7d3a7701 100644 --- a/src/network/message_blockdata.rs +++ b/src/network/message_blockdata.rs @@ -20,9 +20,11 @@ use network::constants; use consensus::encode::{Decodable, Encodable}; -use consensus::{encode, ReadExt, WriteExt}; +use consensus::{encode, ReadExt}; use bitcoin_hashes::sha256d; +use std::io; + #[derive(PartialEq, Eq, Clone, Debug)] /// The type of an inventory object pub enum InvType { @@ -101,17 +103,20 @@ impl GetHeadersMessage { impl_consensus_encoding!(GetHeadersMessage, version, locator_hashes, stop_hash); -impl Encodable for Inventory { +impl Encodable for Inventory { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let inv_len = match self.inv_type { - InvType::Error => 0u32, + InvType::Error => 0u32, InvType::Transaction => 1, InvType::Block => 2, InvType::WitnessBlock => 0x40000002, InvType::WitnessTransaction => 0x40000001 - }.consensus_encode(s)?; - Ok(inv_len + self.hash.consensus_encode(s)?) + }.consensus_encode(&mut s)?; + Ok(inv_len + self.hash.consensus_encode(&mut s)?) } } diff --git a/src/util/merkleblock.rs b/src/util/merkleblock.rs index 9eb551e5..bd42d608 100644 --- a/src/util/merkleblock.rs +++ b/src/util/merkleblock.rs @@ -57,12 +57,13 @@ //! ``` use std::collections::HashSet; +use std::io; use bitcoin_hashes::{sha256d, Hash}; use blockdata::constants::{MAX_BLOCK_WEIGHT, MIN_TRANSACTION_WEIGHT}; use consensus::encode::{Encodable, Error}; -use consensus::{Decodable, ReadExt, WriteExt}; +use consensus::{Decodable, ReadExt}; use util::hash::BitcoinHash; use util::merkleblock::MerkleBlockError::*; use {Block, BlockHeader}; @@ -353,10 +354,10 @@ impl PartialMerkleTree { } } -impl Encodable for PartialMerkleTree { - fn consensus_encode(&self, s: &mut S) -> Result { - let ret = self.num_transactions.consensus_encode(s)? - + self.hashes.consensus_encode(s)?; +impl Encodable for PartialMerkleTree { + fn consensus_encode(&self, mut s: S) -> Result { + let ret = self.num_transactions.consensus_encode(&mut s)? + + self.hashes.consensus_encode(&mut s)?; let mut bytes: Vec = vec![0; (self.bits.len() + 7) / 8]; for p in 0..self.bits.len() { bytes[p / 8] |= (self.bits[p] as u8) << (p % 8) as u8; @@ -471,9 +472,11 @@ impl MerkleBlock { } } -impl Encodable for MerkleBlock { - fn consensus_encode(&self, s: &mut S) -> Result { - Ok(self.header.consensus_encode(s)? + self.txn.consensus_encode(s)?) +impl Encodable for MerkleBlock { + fn consensus_encode(&self, mut s: S) -> Result { + let len = self.header.consensus_encode(&mut s)? + + self.txn.consensus_encode(s)?; + Ok(len) } } diff --git a/src/util/psbt/macros.rs b/src/util/psbt/macros.rs index 5e652c54..3531da63 100644 --- a/src/util/psbt/macros.rs +++ b/src/util/psbt/macros.rs @@ -54,11 +54,17 @@ macro_rules! impl_psbt_serialize { macro_rules! impl_psbtmap_consensus_encoding { ($thing:ty) => { - impl ::consensus::Encodable for $thing { - fn consensus_encode(&self, s: &mut S) -> Result { + impl ::consensus::Encodable for $thing { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; for pair in ::util::psbt::Map::get_pairs(self)? { - len += ::consensus::Encodable::consensus_encode(&pair, s)?; + len += ::consensus::Encodable::consensus_encode( + &pair, + &mut s, + )?; } Ok(len + ::consensus::Encodable::consensus_encode(&0x00_u8, s)?) diff --git a/src/util/psbt/mod.rs b/src/util/psbt/mod.rs index 088192bc..342265c4 100644 --- a/src/util/psbt/mod.rs +++ b/src/util/psbt/mod.rs @@ -20,7 +20,9 @@ use blockdata::script::Script; use blockdata::transaction::Transaction; -use consensus::{encode, Encodable, Decodable, WriteExt, ReadExt}; +use consensus::{encode, Encodable, Decodable, ReadExt}; + +use std::io; mod error; pub use self::error::Error; @@ -88,21 +90,24 @@ impl PartiallySignedTransaction { } } -impl Encodable for PartiallySignedTransaction { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for PartiallySignedTransaction { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += b"psbt".consensus_encode(s)?; + len += b"psbt".consensus_encode(&mut s)?; - len += 0xff_u8.consensus_encode(s)?; + len += 0xff_u8.consensus_encode(&mut s)?; - len += self.global.consensus_encode(s)?; + len += self.global.consensus_encode(&mut s)?; for i in &self.inputs { - len += i.consensus_encode(s)?; + len += i.consensus_encode(&mut s)?; } for i in &self.outputs { - len += i.consensus_encode(s)?; + len += i.consensus_encode(&mut s)?; } Ok(len) diff --git a/src/util/psbt/raw.rs b/src/util/psbt/raw.rs index 880b437f..808f23bf 100644 --- a/src/util/psbt/raw.rs +++ b/src/util/psbt/raw.rs @@ -17,10 +17,10 @@ //! Raw PSBT key-value pairs as defined at //! https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki. -use std::fmt; +use std::{fmt, io}; use consensus::encode::{Decodable, Encodable, VarInt, MAX_VEC_SIZE}; -use consensus::{encode, ReadExt, WriteExt}; +use consensus::{encode, ReadExt}; use util::psbt::Error; /// A PSBT key in its raw byte form. @@ -80,24 +80,30 @@ impl Decodable for Key { } } -impl Encodable for Key { - fn consensus_encode(&self, s: &mut S) -> Result { +impl Encodable for Key { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let mut len = 0; - len += VarInt((self.key.len() + 1) as u64).consensus_encode(s)?; + len += VarInt((self.key.len() + 1) as u64).consensus_encode(&mut s)?; - len += self.type_value.consensus_encode(s)?; + len += self.type_value.consensus_encode(&mut s)?; for key in &self.key { - len += key.consensus_encode(s)? + len += key.consensus_encode(&mut s)? } Ok(len) } } -impl Encodable for Pair { - fn consensus_encode(&self, s: &mut S) -> Result { - let len = self.key.consensus_encode(s)?; +impl Encodable for Pair { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { + let len = self.key.consensus_encode(&mut s)?; Ok(len + self.value.consensus_encode(s)?) } } diff --git a/src/util/uint.rs b/src/util/uint.rs index de98411f..b1856a97 100644 --- a/src/util/uint.rs +++ b/src/util/uint.rs @@ -336,13 +336,16 @@ macro_rules! construct_uint { } } - impl ::consensus::Encodable for $name { + impl ::consensus::Encodable for $name { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result { + fn consensus_encode( + &self, + mut s: S, + ) -> Result { let &$name(ref data) = self; let mut len = 0; for word in data.iter() { - len += word.consensus_encode(s)?; + len += word.consensus_encode(&mut s)?; } Ok(len) }