refactoring of impl_decodable_for_uint

This commit is contained in:
Grant Wuerker 2018-10-20 15:15:58 -05:00
parent ba65bdfc6a
commit 9c1d34253a
2 changed files with 8 additions and 8 deletions

View File

@ -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)

View File

@ -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());
}
}
}