tokio-rs / bytes

Utilities for working with bytes
MIT License
1.87k stars 278 forks source link

Know ahead of time whether `.reserve` will reallocate #602

Open yshui opened 1 year ago

yshui commented 1 year ago

.reserve employs some heuristics, which are subject to change, to determine whether it's going to move the data or reallocate. A user concerned with memory usage might want to do something, like flushing out the buffer to I/O, before BytesMut gives up and grows the allocation.

Right now there seems to be no way to achieve this. Maybe there could be a .compact method which is like .reserve minus reallocation, and returns whether it could expand the available space.