Tidy ssz decoding code

This commit is contained in:
Paul Hauner 2019-05-11 22:52:24 +10:00
parent fc2a406edf
commit 02afc6ef24
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C

View File

@ -102,31 +102,27 @@ impl<'a> SszDecoderBuilder<'a> {
} }
fn apply_offsets(&mut self) -> Result<(), DecodeError> { fn apply_offsets(&mut self) -> Result<(), DecodeError> {
dbg!(&self.offsets);
dbg!(&self.items);
if !self.offsets.is_empty() { if !self.offsets.is_empty() {
// Check to ensure the first offset points to the byte immediately following the
let mut running_offset = self.offsets[0].offset; // fixed-length bytes.
if self.offsets[0].offset != self.items_index {
if running_offset != self.items_index { return Err(DecodeError::OutOfBoundsByte {
return Err(DecodeError::OutOfBoundsByte { i: running_offset }) i: self.offsets[0].offset,
});
} }
for i in 1..=self.offsets.len() { // Iterate through each pair of offsets, grabbing the slice between each of the offsets.
let slice_option = if i == self.offsets.len() { for pair in self.offsets.windows(2) {
self.bytes.get(running_offset..) let a = pair[0];
} else { let b = pair[1];
let offset = self.offsets[i];
let start = running_offset;
running_offset = offset.offset;
self.bytes.get(start..running_offset) self.items[a.position] = &self.bytes[a.offset..b.offset];
}; }
let slice = slice_option // Handle the last offset, pushing a slice from it's start through to the end of
.ok_or_else(|| DecodeError::OutOfBoundsByte { i: running_offset })?; // `self.bytes`.
if let Some(last) = self.offsets.last() {
self.items[self.offsets[i - 1].position] = slice; self.items[last.position] = &self.bytes[last.offset..]
} }
} }