How to deserialize data from data stream?

I currently rely on read_u8(16...) and read_exact to deserialize data from the data stream. Although this method also works, I think it is a bit primitive. Is there a better way?

I know serde_json can read data from read , but my data has to be peeked to determine the type of the next packet. Currently, the serialization and deserialization of data packets are implemented manually through Bytes. Is there a better way?

If you wrap your protocol in messages in the form of <length><bytes * length>, then you can easily buffer the data, and then use anything else to deserialize it.

bincode and msgpack can be used to read only as much as needed from a stream (they have lengths in their encoding).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.