You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was looking for a way to join two contiguous Bytes into one, but there isn't such a thing.
It's common in parsing code to read multiple logical messages into a single BytesMut, split them off into individual Bytes for processing, but then it if they need to be written somewhere it can be advantageous to recombine them, if possible, so there are fewer syscalls to write. It's also possible to just use writev, but that typically involves an allocation for the iovec array and not all platforms support writev, and it's not an option if you're using a TLS wrapper around the stream. Using writev also requires much more code.
I needed this in more than one place as it turns out, so I implemented it using unsafe. Feel free to use any of this code in the bytes library, and relicense as required.
This is based largely on the example of `BytesMut::unsplit`. If two
`Bytes` are contiguous and point to the same allocation, then they are
cheaply merged. Otherwise, a new `BytesMut` is allocated, copies data
from `self` and `other`, and `self` is replaced with `new.freeze()`.
Closestokio-rs#503
I was looking for a way to join two contiguous Bytes into one, but there isn't such a thing.
It's common in parsing code to read multiple logical messages into a single BytesMut, split them off into individual Bytes for processing, but then it if they need to be written somewhere it can be advantageous to recombine them, if possible, so there are fewer syscalls to write. It's also possible to just use writev, but that typically involves an allocation for the iovec array and not all platforms support writev, and it's not an option if you're using a TLS wrapper around the stream. Using writev also requires much more code.
I think it is possible to do this safely:
Does this seem like a worthwhile addition to the library?
The text was updated successfully, but these errors were encountered: