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>
impl<'a> Encoder<'a>
Sourcepub fn new(buf: &'a mut dyn BufMut) -> Self
pub fn new(buf: &'a mut dyn BufMut) -> Self
Creates a new encoder that writes to the given buffer.
Sourcepub fn encode_field<T: RLPEncode>(self, value: &T) -> Self
pub fn encode_field<T: RLPEncode>(self, value: &T) -> Self
Stores a field to be encoded.
Sourcepub fn encode_optional_field<T: RLPEncode>(self, opt_value: &Option<T>) -> Self
pub fn encode_optional_field<T: RLPEncode>(self, opt_value: &Option<T>) -> Self
If Some, stores a field to be encoded, else does nothing.
Sourcepub fn encode_key_value_list<T: RLPEncode>(
self,
list: &Vec<(Bytes, Bytes)>,
) -> Self
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
Sourcepub fn encode_raw(self, value: &[u8]) -> Self
pub fn encode_raw(self, value: &[u8]) -> Self
Adds a raw value to the buffer without rlp-encoding it
Sourcepub fn encode_bytes(self, value: &[u8]) -> Self
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
Trait Implementations§
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> 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