[−][src]Struct ssz::SszEncoder
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 main() { 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); }
Methods
impl<'a> SszEncoder<'a>
[src]
pub fn list(buf: &'a mut Vec<u8>, num_fixed_bytes: usize) -> Self
[src]
Instantiate a new encoder for encoding a SSZ list.
Identical to Self::container
.
pub fn container(buf: &'a mut Vec<u8>, num_fixed_bytes: usize) -> Self
[src]
Instantiate a new encoder for encoding a SSZ container.
pub fn append<T: Encode>(&mut self, item: &T)
[src]
Append some item
to the SSZ bytes.
pub fn finalize(&mut self) -> &mut Vec<u8>
[src]
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> Send for SszEncoder<'a>
impl<'a> Unpin for SszEncoder<'a>
impl<'a> Sync for SszEncoder<'a>
impl<'a> !UnwindSafe for SszEncoder<'a>
impl<'a> RefUnwindSafe for SszEncoder<'a>
Blanket Implementations
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,