Skip to main content

Encoder

Struct Encoder 

Source
pub struct Encoder<'a> { /* private fields */ }
Expand description

§Struct encoding helper

Used to encode a struct into RLP format. The struct’s fields must implement RLPEncode. The struct is encoded as a list, with its values being the fields in the order they are passed to Encoder::encode_field.

§Examples

#[derive(Debug, PartialEq, Eq)]
struct Simple {
    pub a: u8,
    pub b: u16,
}

impl RLPEncode for Simple {
    fn encode(&self, buf: &mut dyn BufMut) {
        // The fields are encoded in the order given here
        Encoder::new(buf)
            .encode_field(&self.a)
            .encode_field(&self.b)
            .finish();
    }
}

let mut buf = vec![];
Simple { a: 61, b: 75 }.encode(&mut buf);

assert_eq!(&buf, &[0xc2, 61, 75]);

Implementations§

Source§

impl<'a> Encoder<'a>

Source

pub fn new(buf: &'a mut dyn BufMut) -> Self

Creates a new encoder that writes to the given buffer.

Source

pub fn encode_field<T: RLPEncode>(self, value: &T) -> Self

Stores a field to be encoded.

Source

pub fn encode_optional_field<T: RLPEncode>(self, opt_value: &Option<T>) -> Self

If Some, stores a field to be encoded, else does nothing.

Source

pub fn encode_key_value_list<T: RLPEncode>( self, list: &Vec<(Bytes, Bytes)>, ) -> Self

Stores a (key, value) list where the values are already encoded (i.e. value = RLP prefix || payload) but the keys are not encoded

Source

pub fn finish(self)

Finishes encoding the struct and writes the result to the buffer.

Source

pub fn encode_raw(self, value: &[u8]) -> Self

Adds a raw value to the buffer without rlp-encoding it

Source

pub fn encode_bytes(self, value: &[u8]) -> Self

Stores a field to be encoded as bytes This method is used to bypass the conflicting implementations between Vec and Vec

Trait Implementations§

Source§

impl Debug for Encoder<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Encoder<'a>

§

impl<'a> !Send for Encoder<'a>

§

impl<'a> !Sync for Encoder<'a>

§

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

§

impl<'a> Freeze for Encoder<'a>

§

impl<'a> Unpin for Encoder<'a>

§

impl<'a> UnsafeUnpin for Encoder<'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, 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.