-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider an unsplit method for Bytes #503
Comments
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. https://gist.github.com/eloff/6e0c236ae6388744a7d5b76a5e0e9b6c |
adeschamps
added a commit
to adeschamps/bytes
that referenced
this issue
Mar 25, 2022
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()`. Closes tokio-rs#503
Looks like duplicate of #287 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: