Struct ssz::SszEncoder

source ·
pub struct SszEncoder<'a> { /* private fields */ }
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§

source§

impl<'a> SszEncoder<'a>

source

pub fn container(buf: &'a mut Vec<u8>, num_fixed_bytes: usize) -> Self

Instantiate a new encoder for encoding a SSZ container.

source

pub fn append<T: Encode>(&mut self, item: &T)

Append some item to the SSZ bytes.

source

pub fn append_parameterized<F>(&mut self, is_ssz_fixed_len: bool, ssz_append: F)
where F: Fn(&mut Vec<u8>),

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

source

pub fn finalize(&mut self) -> &mut Vec<u8>

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§

§

impl<'a> Freeze for SszEncoder<'a>

§

impl<'a> RefUnwindSafe for SszEncoder<'a>

§

impl<'a> Send for SszEncoder<'a>

§

impl<'a> Sync for SszEncoder<'a>

§

impl<'a> Unpin for SszEncoder<'a>

§

impl<'a> !UnwindSafe for SszEncoder<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.