Skip to main content

Decoder

Struct Decoder 

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

§Struct decoding helper

Used to decode a struct from RLP format. The struct’s fields must implement RLPDecode. The struct is expected as a list, with its values being the fields in the order they are passed to Decoder::decode_field.

§Examples

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

impl RLPDecode for Simple {
    fn decode_unfinished(buf: &[u8]) -> Result<(Self, &[u8]), RLPDecodeError> {
        let decoder = Decoder::new(&buf).unwrap();
        // The fields are expected in the same order as given here
        let (a, decoder) = decoder.decode_field("a").unwrap();
        let (b, decoder) = decoder.decode_field("b").unwrap();
        let rest = decoder.finish().unwrap();
        Ok((Simple { a, b }, rest))
    }
}

let bytes = [0xc2, 61, 75];
let decoded = Simple::decode(&bytes).unwrap();

assert_eq!(decoded, Simple { a: 61, b: 75 });

Implementations§

Source§

impl<'a> Decoder<'a>

Source

pub fn new(buf: &'a [u8]) -> Result<Self, RLPDecodeError>

Source

pub fn decode_field<T: RLPDecode>( self, name: &str, ) -> Result<(T, Self), RLPDecodeError>

Source

pub fn get_encoded_item(self) -> Result<(Vec<u8>, Self), RLPDecodeError>

Returns the next field without decoding it, i.e. the payload bytes including its prefix.

Source

pub fn get_encoded_item_ref(self) -> Result<(&'a [u8], Self), RLPDecodeError>

Returns the next field without decoding it, i.e. the payload bytes including its prefix.

Source

pub fn decode_optional_field<T: RLPDecode>(self) -> (Option<T>, Self)

Returns Some(field) if there’s some field to decode, otherwise returns None

Source

pub const fn finish(self) -> Result<&'a [u8], RLPDecodeError>

Finishes encoding the struct and returns the remaining bytes after the item. If the item’s payload is not empty, returns an error.

Source

pub const fn is_done(&self) -> bool

Returns true if the decoder has finished decoding the given input

Source

pub const fn finish_unchecked(self) -> &'a [u8]

Same as finish, but discards the item’s remaining payload instead of failing.

Source

pub const fn get_payload_len(&self) -> usize

Trait Implementations§

Source§

impl<'a> Debug for Decoder<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Decoder<'a>

§

impl<'a> RefUnwindSafe for Decoder<'a>

§

impl<'a> Send for Decoder<'a>

§

impl<'a> Sync for Decoder<'a>

§

impl<'a> Unpin for Decoder<'a>

§

impl<'a> UnsafeUnpin for Decoder<'a>

§

impl<'a> UnwindSafe for Decoder<'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.