BiagioFesta / wtransport

Async-friendly WebTransport implementation in Rust
Apache License 2.0
346 stars 19 forks source link

Make receive_datagram return BytesMut #179

Open TheButlah opened 1 month ago

TheButlah commented 1 month ago

Some use cases, such as using datagrams with serde via tokio_serde::Framed, require that messages received from the peer are BytesMut instead of Bytes. Currently, Connection::receive_datagram() returns a Datagram which itself can only give you Bytes, so the user of the API first needs to clone the data to get exclusive access to a buffer.

It would be nice if receive_datagrams instead returned a BytesMut, providing more performance for use cases like mine where I actually need exclusive access to the buffer.

BiagioFesta commented 1 month ago

It there a way to get a BytesMut from Bytes without copying it?

I am on my phone atm, but considering the underlying QUIC library returns Bytes I am wondering if that's trivially feasible

TheButlah commented 1 month ago

AFAIK, no, there isn't a way. There is a is_unique function, but I don't think it can be used for the purpose of converting to a BytesMut. I could be wrong though.

BiagioFesta commented 1 month ago

I am afraid so. Considering the QUIC connection returns a datagram as Bytes here, the only possibility is to copy the data (even if the Bytes is unique), I guess

pablosichert commented 1 month ago

There's an adjacent issue for this in the Quinn repo: https://github.com/quinn-rs/quinn/issues/1173