bytes icon indicating copy to clipboard operation
bytes copied to clipboard

Implement Bytes::unsplit

Open adeschamps opened this issue 2 years ago • 2 comments

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

adeschamps avatar Mar 25 '22 02:03 adeschamps

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

  1. merge two non-contiguous Bytes to have a convenient single view
  2. merge two contiguous Bytes that 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.

SephVelut avatar Mar 30 '23 11:03 SephVelut

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.

SephVelut avatar Mar 31 '23 18:03 SephVelut