Add working decoder example

This commit is contained in:
Paul Hauner 2019-05-03 17:50:29 +10:00
parent 0c0edb0653
commit 7096d56749
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 9 additions and 5 deletions

View File

@ -1,5 +1,6 @@
use ssz2::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszStream};
#[derive(Debug, PartialEq)]
pub struct Foo {
a: u16,
b: Vec<u8>,
@ -51,8 +52,11 @@ fn main() {
c: 11,
};
assert_eq!(
foo.as_ssz_bytes(),
vec![42, 0, 8, 0, 0, 0, 11, 0, 0, 1, 2, 3]
);
let bytes = vec![42, 0, 8, 0, 0, 0, 11, 0, 0, 1, 2, 3];
assert_eq!(foo.as_ssz_bytes(), bytes);
let decoded_foo = Foo::from_ssz_bytes(&bytes).unwrap();
assert_eq!(foo, decoded_foo);
}

View File

@ -103,7 +103,7 @@ impl<'a> SszDecoderBuilder<'a> {
let mut insertions = 0;
let mut running_offset = self.offsets[0].offset;
for i in 1..self.offsets.len() {
for i in 1..=self.offsets.len() {
let (slice_option, position) = if i == self.offsets.len() {
(self.bytes.get(running_offset..), self.offsets.len())
} else {