Open adeschamps opened 2 years ago
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
Bytes
to have a convenient single viewBytes
that have previously been split offIn 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.
This is based largely on the example of
BytesMut::unsplit
. If twoBytes
are contiguous and point to the same allocation, then they are cheaply merged. Otherwise, a newBytesMut
is allocated, copies data fromself
andother
, andself
is replaced withnew.freeze()
.Closes https://github.com/tokio-rs/bytes/issues/503