bytes
bytes copied to clipboard
Need for BytesMut::reserve_exact
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
let _ = buf.split_off(buf.capacity());
can be used to emulate this, but it involves creating a temporary tail BytesMut
and always going atomic.
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.