Add working decoder example
This commit is contained in:
parent
0c0edb0653
commit
7096d56749
@ -1,5 +1,6 @@
|
|||||||
use ssz2::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszStream};
|
use ssz2::{Decodable, DecodeError, Encodable, SszDecoderBuilder, SszStream};
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Foo {
|
pub struct Foo {
|
||||||
a: u16,
|
a: u16,
|
||||||
b: Vec<u8>,
|
b: Vec<u8>,
|
||||||
@ -51,8 +52,11 @@ fn main() {
|
|||||||
c: 11,
|
c: 11,
|
||||||
};
|
};
|
||||||
|
|
||||||
assert_eq!(
|
let bytes = vec![42, 0, 8, 0, 0, 0, 11, 0, 0, 1, 2, 3];
|
||||||
foo.as_ssz_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);
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ impl<'a> SszDecoderBuilder<'a> {
|
|||||||
let mut insertions = 0;
|
let mut insertions = 0;
|
||||||
let mut running_offset = self.offsets[0].offset;
|
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() {
|
let (slice_option, position) = if i == self.offsets.len() {
|
||||||
(self.bytes.get(running_offset..), self.offsets.len())
|
(self.bytes.get(running_offset..), self.offsets.len())
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user