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 SSZ specification at v0.12.1.
§Example
use ssz_derive::{Encode, Decode};
use ssz::{Decode, Encode};
#[derive(PartialEq, Debug, Encode, Decode)]
struct Foo {
a: u64,
b: Vec<u16>,
}
fn ssz_encode_decode_example() {
let foo = Foo {
a: 42,
b: vec![1, 3, 3, 7]
};
let ssz_bytes: Vec<u8> = foo.as_ssz_bytes();
let decoded_foo = Foo::from_ssz_bytes(&ssz_bytes).unwrap();
assert_eq!(foo, decoded_foo);
}
See examples/
for manual implementations of the Encode
and Decode
traits.
Modules§
- legacy
- Provides a “legacy” version of SSZ encoding for
Option<T> where T: Encode + Decode
.
Macros§
Structs§
- SszDecoder
- Decodes some slices of SSZ into object instances. Should be instantiated using
SszDecoderBuilder
. - SszDecoder
Builder - Builds an
SszDecoder
. - SszEncoder
- Allow for encoding an ordered series of distinct or indistinct objects as SSZ bytes.
- Union
Selector - Provides the one-byte “selector” from the SSZ union specification:
Enums§
- Decode
Error - 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_ssz_bytes(&bytes)
method. - Encode
- Provides SSZ encoding (serialization) via the
as_ssz_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 little-endian byte array ofBYTES_PER_LENGTH_OFFSET
length. - read_
offset - Reads a
BYTES_PER_LENGTH_OFFSET
-byte length frombytes
, wherebytes.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). - ssz_
encode - Convenience function to SSZ encode an object supporting ssz::Encode.