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
impl MessageLen
Sourcepub fn new() -> Self
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.
Sourcepub fn with_limits(max_depth: usize, max_len: usize) -> Self
pub fn with_limits(max_depth: usize, max_len: usize) -> Self
-
max_depthlimits nesting of arrays and maps -
max_lenis 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).
Sourcepub fn len_of(complete_message: &[u8]) -> Result<usize, LenError>
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.
Sourcepub fn incremental_len(
&mut self,
next_message_fragment: &[u8],
) -> Result<usize, LenError>
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 leastlenbytes in total (counting from the start of all data given to this function, not just this slice). Thelenis 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.