rmp::decode

Function read_str_from_slice

Source
pub fn read_str_from_slice<T: ?Sized + AsRef<[u8]>>(
    buf: &T,
) -> Result<(&str, &[u8]), DecodeStringError<'_, BytesReadError>>
Expand description

Attempts to read and decode a string value from the reader, returning a borrowed slice from it.

ยงExamples

use rmp::encode::write_str;
use rmp::decode::read_str_from_slice;

let mut buf = Vec::new();
write_str(&mut buf, "Unpacking").unwrap();
write_str(&mut buf, "multiple").unwrap();
write_str(&mut buf, "strings").unwrap();

let mut chunks = Vec::new();
let mut unparsed = &buf[..];
while let Ok((chunk, tail)) = read_str_from_slice(unparsed) {
    chunks.push(chunk);
    unparsed = tail;
}

assert_eq!(vec!["Unpacking", "multiple", "strings"], chunks);