bytes
bytes copied to clipboard
Implement Bytes::unsplit
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 https://github.com/tokio-rs/bytes/issues/503
try_unsplit only has one fail case that's side-effect free. It doesn't pose a danger to public users. Why not make it pub?
There's two use cases where I will want to merge Bytes
- merge two non-contiguous
Bytesto have a convenient single view - merge two contiguous
Bytesthat have previously been split off
In the first case, if the Bytes happen to be contiguous, then the O(1) operation is just a bonus. But in the second case, I expect that the Bytes must be contiguous. Otherwise it would be an error to merge them. unsplit hides this failure and is general over both cases. Maybe rename unsplit to merge and keep as is, but rename try_unsplit to unsplit and keep as is and expose both.
When using current unsplit, it's not immediately obvious to a pub user that the operation will soft-fail if the owning BytesMut reallocates because of insufficient capacity. In hindsight this is obvious, but this method gives off the impression that Bytes simply need to be contiguous. Emphasis on both contiguous and from the same allocation with a note in the doc that a reallocation will invalidate formerly contiguous blocks.