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>
impl<'a> Decoder<'a>
pub fn new(buf: &'a [u8]) -> Result<Self, RLPDecodeError>
pub fn decode_field<T: RLPDecode>( self, name: &str, ) -> Result<(T, Self), RLPDecodeError>
Sourcepub fn get_encoded_item(self) -> Result<(Vec<u8>, Self), RLPDecodeError>
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.
Sourcepub fn get_encoded_item_ref(self) -> Result<(&'a [u8], Self), RLPDecodeError>
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.
Sourcepub fn decode_optional_field<T: RLPDecode>(self) -> (Option<T>, Self)
pub fn decode_optional_field<T: RLPDecode>(self) -> (Option<T>, Self)
Returns Some(field) if there’s some field to decode, otherwise returns None
Sourcepub const fn finish(self) -> Result<&'a [u8], RLPDecodeError>
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.
Sourcepub const fn is_done(&self) -> bool
pub const fn is_done(&self) -> bool
Returns true if the decoder has finished decoding the given input
Sourcepub const fn finish_unchecked(self) -> &'a [u8]
pub const fn finish_unchecked(self) -> &'a [u8]
Same as finish, but discards the item’s remaining payload
instead of failing.
pub const fn get_payload_len(&self) -> usize
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more