Struct rlp::RlpStream

source ·
pub struct RlpStream { /* private fields */ }
Expand description

Appendable rlp encoder.

Implementations§

source§

impl RlpStream

source

pub fn new() -> Self

Initializes instance of empty Stream.

source

pub fn new_list(len: usize) -> Self

Initializes the Stream as a list.

source

pub fn new_with_buffer(buffer: BytesMut) -> Self

Initializes instance of empty Stream.

source

pub fn new_list_with_buffer(buffer: BytesMut, len: usize) -> Self

Initializes the Stream as a list.

source

pub fn append_empty_data(&mut self) -> &mut Self

Apends null to the end of stream, chainable.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(2);
stream.append_empty_data().append_empty_data();
let out = stream.out();
assert_eq!(out, vec![0xc2, 0x80, 0x80]);
source

pub fn append_raw(&mut self, bytes: &[u8], item_count: usize) -> &mut Self

Appends raw (pre-serialised) RLP data. Use with caution. Chainable.

source

pub fn append<E>(&mut self, value: &E) -> &mut Self
where E: Encodable,

Appends value to the end of stream, chainable.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(2);
stream.append(&"cat").append(&"dog");
let out = stream.out();
assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
source

pub fn append_iter<I>(&mut self, value: I) -> &mut Self
where I: IntoIterator<Item = u8>,

Appends iterator to the end of stream, chainable.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(2);
stream.append(&"cat").append_iter("dog".as_bytes().iter().cloned());
let out = stream.out();
assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
source

pub fn append_list<E, K>(&mut self, values: &[K]) -> &mut Self
where E: Encodable, K: Borrow<E>,

Appends list of values to the end of stream, chainable.

source

pub fn append_internal<E>(&mut self, value: &E) -> &mut Self
where E: Encodable,

Appends value to the end of stream, but do not count it as an appended item. It’s useful for wrapper types

source

pub fn begin_list(&mut self, len: usize) -> &mut RlpStream

Declare appending the list of given size, chainable.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(2);
stream.begin_list(2).append(&"cat").append(&"dog");
stream.append(&"");
let out = stream.out();
assert_eq!(out, vec![0xca, 0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g', 0x80]);
source

pub fn begin_unbounded_list(&mut self) -> &mut RlpStream

Declare appending the list of unknown size, chainable.

source

pub fn append_raw_checked( &mut self, bytes: &[u8], item_count: usize, max_size: usize, ) -> bool

Appends raw (pre-serialised) RLP data. Checks for size overflow.

source

pub fn estimate_size(&self, add: usize) -> usize

Calculate total RLP size for appended payload.

source

pub fn len(&self) -> usize

Returns current RLP size in bytes for the data pushed into the list.

source

pub fn is_empty(&self) -> bool

source

pub fn clear(&mut self)

Clear the output stream so far.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(3);
stream.append(&"cat");
stream.clear();
stream.append(&"dog");
let out = stream.out();
assert_eq!(out, vec![0x83, b'd', b'o', b'g']);
source

pub fn is_finished(&self) -> bool

Returns true if stream doesnt expect any more items.

use rlp::RlpStream;
let mut stream = RlpStream::new_list(2);
stream.append(&"cat");
assert_eq!(stream.is_finished(), false);
stream.append(&"dog");
assert_eq!(stream.is_finished(), true);
let out = stream.out();
assert_eq!(out, vec![0xc8, 0x83, b'c', b'a', b't', 0x83, b'd', b'o', b'g']);
source

pub fn as_raw(&self) -> &[u8]

Get raw encoded bytes

source

pub fn out(self) -> BytesMut

Streams out encoded bytes.

panic! if stream is not finished.

source

pub fn encoder(&mut self) -> BasicEncoder<'_>

source

pub fn finalize_unbounded_list(&mut self)

Finalize current unbounded list. Panics if no unbounded list has been opened.

Trait Implementations§

source§

impl Default for RlpStream

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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>,

§

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>,

§

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.