From 9c1d34253a509cd01e820fc3f714b9162e5bbe6b Mon Sep 17 00:00:00 2001 From: Grant Wuerker Date: Sat, 20 Oct 2018 15:15:58 -0500 Subject: [PATCH] refactoring of impl_decodable_for_uint --- beacon_chain/utils/ssz/src/impl_decode.rs | 14 +++++++------- beacon_chain/utils/ssz/src/impl_encode.rs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/beacon_chain/utils/ssz/src/impl_decode.rs b/beacon_chain/utils/ssz/src/impl_decode.rs index b86af1365..a6b8843bd 100644 --- a/beacon_chain/utils/ssz/src/impl_decode.rs +++ b/beacon_chain/utils/ssz/src/impl_decode.rs @@ -8,20 +8,20 @@ use super::{ macro_rules! impl_decodable_for_uint { ($type: ident, $bit_size: expr) => { impl Decodable for $type { - fn ssz_decode(bytes: &[u8], index: usize) + fn ssz_decode(bytes: &[u8], start_bytes: usize) -> Result<(Self, usize), DecodeError> { assert!((0 < $bit_size) & ($bit_size <= 64) & ($bit_size % 8 == 0)); let max_bytes = $bit_size / 8; - if bytes.len() >= (index + max_bytes) { - let end_bytes = index + max_bytes; + if bytes.len() >= (start_bytes + max_bytes) { + let end_bytes = start_bytes + max_bytes; let mut result: $type = 0; - for i in index..end_bytes { - let offset = ((index + max_bytes) - i - 1) * 8; - result |= ($type::from(bytes[i])) << offset; - }; + for (index, byte) in bytes.iter().enumerate().take(end_bytes).skip(start_bytes) { + let offset = (end_bytes - index - 1) * 8; + result |= ($type::from(*byte)) << offset; + } Ok((result, end_bytes)) } else { Err(DecodeError::TooShort) diff --git a/beacon_chain/utils/ssz/src/impl_encode.rs b/beacon_chain/utils/ssz/src/impl_encode.rs index 392f8667e..36776e071 100644 --- a/beacon_chain/utils/ssz/src/impl_encode.rs +++ b/beacon_chain/utils/ssz/src/impl_encode.rs @@ -35,7 +35,7 @@ macro_rules! impl_encodable_for_uint { } // Append bytes to the SszStream - s.append_encoded_raw(&mut buf.to_vec()); + s.append_encoded_raw(&buf.to_vec()); } } }