Struct ssz::SszEncoder[][src]

pub struct SszEncoder<'a> { /* fields omitted */ }
Expand description

Allow for encoding an ordered series of distinct or indistinct objects as SSZ bytes.

You must call finalize(..) after the final append(..) call to ensure the bytes are written to buf.

Example

Use SszEncoder to produce identical output to foo.as_ssz_bytes():

use ssz_derive::{Encode, Decode};
use ssz::{Decode, Encode, SszEncoder};

#[derive(PartialEq, Debug, Encode, Decode)]
struct Foo {
    a: u64,
    b: Vec<u16>,
}

fn ssz_encode_example() {
    let foo = Foo {
        a: 42,
        b: vec![1, 3, 3, 7]
    };

    let mut buf: Vec<u8> = vec![];
    let offset = <u64 as Encode>::ssz_fixed_len() + <Vec<u16> as Encode>::ssz_fixed_len();

    let mut encoder = SszEncoder::container(&mut buf, offset);

    encoder.append(&foo.a);
    encoder.append(&foo.b);

    encoder.finalize();

    assert_eq!(foo.as_ssz_bytes(), buf);
}

Implementations

Instantiate a new encoder for encoding a SSZ container.

Append some item to the SSZ bytes.

Uses ssz_append to append the encoding of some item to the SSZ bytes.

Write the variable bytes to self.bytes.

This method must be called after the final append(..) call when serializing variable-length items.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.