3Hren / 3Hren/msgpack-rust

How to read a string

Open
#168 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
1.4k
Forks
162
PR merge metrics
No merged PRs in 30d

Description

I have difficulties to read a string. Here is what I do :
let mut buf = [0;64];
rmp::decode::read_str(&mut reader, &mut buf);

This way is good as long as my string is not longer than 64 bytes. But I want to be able to read a string that I don't know the length so I would like to read the length then read the string.
let len = rmp::decode::read_str_len(&mut reader);
*****Here, how can I read the string ? The function read_str_data is not public. If I use read_str, it will read the length again and I don't want that.********************

For now, what I am doing is reading a u8 vector with that function :
fn msgpack_decode_binary(rd: &mut R, len: usize) -> Result> {
let mut buf: Vec = Vec::new();
let mut i = 0;
while i < len {
let byte: u8 = rmp::decode::read_data_u8(rd).expect("39");
buf.push(byte);
i += 1;
}

return Ok(buf);
}

So I can read all u8 character, then build a string with that with String::from_utf8(&buf);

Does it exist a better way to do that ?
Thanks

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.