Open aalekhpatel07 opened 1 year ago
No. The serde API can only return the final result once it's 100% complete.
You could use the lower-level rmp to read individual items as they come. OR you could use something else around msgpack messages to split them into chunks (it could be as simple as sending <length><data>
pieces over the stream).
Hi!
I am serializing a series of structs, and writing them individually to a binary file.
Could you please show which function in rmp::decode
should I use to get the packed struct size? I tried several, and rmp::decode::read_map_len
seems to be the most suitable choice, however, it returns 1
, which is clearly not the case.
And could you please elaborate on the 'using something else around msgpack messages'? Do you suggest just writing custom bytes after each struct, and then splitting the data by that divider?
Thanks in advance.
UPD: as a workaround, one can simply write the size of serialized struct before the actual data:
// encoding (pseudocode)
writer.write(size.to_bytes());
writer.write(serialized_bytes());
// decoding
while buf.len() > 0 {
// firstly we read the size of packed data
// here you might want to use big/little endian, not native one
let size = usize::from_ne_bytes(buf[0..8].try_into().unwrap());
// trim the buffer so that it starts with actual data
buf = &buf[8..];
// parse the serialized record
let record = rmp_serde::from_slice::<Record>(&buf[..size]);
// cut out the data
buf = &buf[size..];
}
I'm trying to use
rmp_serde
to send and receive entire messages (enum
s orstruct
s) via aBytesMut
buffer that gets populated and emptied by a different part of the system (in this case aTcpStream
).I don't have any custom framing setup so I'm wondering if I could use
rmp_serde
to tell me the exact size of the serialized representation of the data (i.e. the count of bytes it deserialized) immediately after it has successfully parsed a portion of the stream into the specified type.If there already exists an approach I apologize for having missed it. Please feel free to point me in the right direction.
I'm picturing an API like:
Here's an example usage: