From 084f82be2172aef3e97989ccd134cf20964550cb Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 22 May 2019 18:26:39 -0400 Subject: [PATCH] Swap a few more [d]encoders to slice emit/read functions --- src/consensus/encode.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/consensus/encode.rs b/src/consensus/encode.rs index 7101be20..3f14d07f 100644 --- a/src/consensus/encode.rs +++ b/src/consensus/encode.rs @@ -583,21 +583,25 @@ impl> Decodable for Vec { } } -impl> Encodable for Box<[T]> { +impl Encodable for Box<[u8]> { #[inline] - fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (&self[..]).consensus_encode(s) } + fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { + VarInt(self.len() as u64).consensus_encode(s)?; + s.emit_slice(&self) + } } -impl> Decodable for Box<[T]> { +impl Decodable for Box<[u8]> { #[inline] - fn consensus_decode(d: &mut D) -> Result, self::Error> { + fn consensus_decode(d: &mut D) -> Result, self::Error> { let len = VarInt::consensus_decode(d)?.0; let len = len as usize; if len > MAX_VEC_SIZE { return Err(self::Error::OversizedVectorAllocation { requested: len, max: MAX_VEC_SIZE }) } let mut ret = Vec::with_capacity(len); - for _ in 0..len { ret.push(Decodable::consensus_decode(d)?); } + ret.resize(len, 0); + d.read_slice(&mut ret)?; Ok(ret.into_boxed_slice()) } } @@ -615,11 +619,7 @@ impl Encodable for CheckedData { fn consensus_encode(&self, s: &mut S) -> Result<(), self::Error> { (self.0.len() as u32).consensus_encode(s)?; sha2_checksum(&self.0).consensus_encode(s)?; - // We can't just pass to the slice encoder since it'll insert a length - for ch in &self.0 { - ch.consensus_encode(s)?; - } - Ok(()) + s.emit_slice(&self.0) } } @@ -629,7 +629,8 @@ impl Decodable for CheckedData { let len: u32 = Decodable::consensus_decode(d)?; let checksum: [u8; 4] = Decodable::consensus_decode(d)?; let mut ret = Vec::with_capacity(len as usize); - for _ in 0..len { ret.push(Decodable::consensus_decode(d)?); } + ret.resize(len as usize, 0); + d.read_slice(&mut ret)?; let expected_checksum = sha2_checksum(&ret); if expected_checksum != checksum { Err(self::Error::InvalidChecksum {