Crate sszb

Crate sszb 

Source
Expand description

Provides encoding (serialization) and decoding (deserialization) in the SimpleSerialize (SSZ) format designed for use in Ethereum 2.0.

Adheres to the Ethereum 2.0 but endian is switched SSZ specification at v0.12.1.

§Example

use sszb_derive::{Encode, Decode};
use sszb::{Decode, Encode};

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

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

    let sszb_bytes: Vec<u8> = foo.as_sszb_bytes();

    let decoded_foo = Foo::from_sszb_bytes(&sszb_bytes).unwrap();

    assert_eq!(foo, decoded_foo);
}

See examples/ for manual implementations of the Encode and Decode traits.

Structs§

SszDecoder
Decodes some slices of SSZ into object instances. Should be instantiated using SszDecoderBuilder.
SszDecoderBuilder
Builds an SszDecoder.
SszEncoder
Allow for encoding an ordered series of distinct or indistinct objects as SSZ bytes.
UnionSelector
Provides the one-byte “selector” from the SSZ union specification:

Enums§

DecodeError
Returned when SSZ decoding fails.

Constants§

BYTES_PER_LENGTH_OFFSET
The number of bytes used to represent an offset.
BYTES_PER_UNION_SELECTOR
The number of bytes used to indicate the variant of a union.
MAX_LENGTH_VALUE
MAX_UNION_SELECTOR
The highest possible union selector value (higher values are reserved for backwards compatible extensions).

Traits§

Decode
Provides SSZ decoding (de-serialization) via the from_sszb_bytes(&bytes) method.
Encode
Provides SSZ encoding (serialization) via the as_sszb_bytes(&self) method.

Functions§

decode_list_of_variable_length_items
Decodes bytes as if it were a list of variable-length items.
encode_length
Encode len as a big-endian byte array of BYTES_PER_LENGTH_OFFSET length.
read_offset
Reads a BYTES_PER_LENGTH_OFFSET-byte length from bytes, where bytes.len() >= BYTES_PER_LENGTH_OFFSET.
split_union_bytes
Takes bytes, assuming it is the encoding for a SSZ union, and returns the union-selector and the body (trailing bytes).
sszb_encode
Convenience function to SSZ encode an object supporting sszb::Encode.