Serializing a stream of bytes in futures

I was writing a RPC server during this event I encountered that if I have 'impl Stream<Item=Bytes>' there isn't any suitable or viable way for sending this data using serde::serialization if the endpoint demands one, here is one simple example

#[tarpc::Service]
pub trait MyService { 
      async fn stream_some_bytes() -> bytes::Bytes; // the return type must implement `serde::serialize`
}

Now Iam stuck at how can I stream bytes without allocating it on heap or somewhere additional since serde::Serialize is not designed to be very compatible with async code hence any ideas around it ?

The only way is to read all of the data, and then pass all of it to serde in one go. It's not possible to avoid keeping the entire object in memory.

Generally, services that send very large amounts of data will not send one large JSON object. Instead, they might send many JSON objects separated by newlines (or other similar schemes). That way, you can parse one JSON object at the time to avoid keeping the entire stream in memory at once.

For the case where the JSON objects are separated by newlines, you can use tokio_util::io::StreamReader to get something that implements AsyncBufRead, and then you can use the read_until method from the AsyncBufReadExt trait to read each chunk.

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.