Skip to main content

MessageLen

Struct MessageLen 

Source
pub struct MessageLen { /* private fields */ }
Expand description

Incremental MessagePack parser that can parse incomplete messages, and report their estimated total length.

Implementations§

Source§

impl MessageLen

Source

pub fn new() -> Self

New parser with default limits

If you have all MessagePack data in memory already, you can use MessageLen::len_of. If you’re reading data in a streaming fashion, you can feed chunks of data to MessageLen::incremental_len.

Source

pub fn with_limits(max_depth: usize, max_len: usize) -> Self

  • max_depth limits nesting of arrays and maps

  • max_len is maximum size of any string, byte string, map, or array. For maps and arrays this is the number of items, not bytes.

Messages can be both deep and wide, being max_depth * max_len in size. You should also limit the maximum byte size of the message (outside of this parser).

Source

pub fn len_of(complete_message: &[u8]) -> Result<usize, LenError>

Parse the entire message to find if it’s complete, and what is its serialized size in bytes.

If it returns Ok(len), then the first len bytes of the given slice parse as a single MessagePack object. The length may be shorter than the slice given (extra data is gracefully ignored).

Err(LenError::Truncated(len)) means that the the object is incomplete, the slice is truncated, and it would need at least this many bytes to parse. The len is always the lower bound, and never exceeds actual message length.

Err(LenError::ParseError) — the end of the message is unknown.

Don’t call this function in a loop. Use MessageLen::incremental_len instead.

Source

pub fn incremental_len( &mut self, next_message_fragment: &[u8], ) -> Result<usize, LenError>

Parse more bytes, and re-evaluate required message length.

This function is stateful and keeps “appending” the data to its evaluation.

  • Ok(len) — size of the whole MessagePack object, in bytes, starting at the beginning of all data given to this function, including previous calls (not just this slice). The object’s data may end before the end of this slice. In such case the extra bytes are gracefully ignored, and have not been parsed.

  • Err(LenError::Truncated(len)) — all bytes of this slice have been consumed, and that was still not enough. The object needs at least len bytes in total (counting from the start of all data given to this function, not just this slice). The len is always the lower bound, and never exceeds actual message length, so it’s safe to read the additional bytes without overshooting the end of the message.

  • Err(LenError::ParseError) — the end of the message cannot be determined, and this is a non-recoverable error. Any further calls to this function may return nonsense.

Source

pub fn reset(&mut self)

Forget all the state. The next call to incremental_len will assume it’s the start of a new message.

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

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.