bytes icon indicating copy to clipboard operation
bytes copied to clipboard

Need for BytesMut::reserve_exact

Open mzabaluev opened this issue 4 years ago • 2 comments

Some applications may want the capacity of BytesMut to be reported exactly as previously requested (unless more is already reserved). BytesMut::reserve allows for allocation excess and over-reserving in expectation for further expansion, which is probably good for the general case. Hence the proposal for another method.

For an example where this could save on external bookkeeping, see https://github.com/mzabaluev/chunked-bytes/issues/1

mzabaluev avatar Jun 05 '20 13:06 mzabaluev

let _ = buf.split_off(buf.capacity()); can be used to emulate this, but it involves creating a temporary tail BytesMut and always going atomic.

mzabaluev avatar Jun 05 '20 13:06 mzabaluev

As I understand it, the stumbling block here is the Vec representation: Vec::reserve_exact does not actually guarantee the exact capacity, and the cap member of BytesMut is tied to the virtual Vec and so can't be fudged without breaking safety for Vec::from_raw_parts. So extra bookkeeping would be needed to implement this.

mzabaluev avatar Jun 06 '20 07:06 mzabaluev