Rename and expose BytesMut::spare_capacity_mut
#572
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR renames
BytesMut::uninit_slice
tospare_capacity_mut
, to matchVec::spare_capacity_mut
, and makes it a public API.There are two reasons I'm proposing this change:
BufMut::chunk_mut
has a similar purpose, the fact thatBufMut
supports non-contiguous buffers makes unsafe code using it on aBytesMut
type somewhat harder to reason about. Thechunk_mut
docs explicitly mention the returned slice might be less thanbuf.capacity()
, which is untrue forBytesMut
(because it is always contiguous), but not very clear when reading code using it.<BytesMut as BufMut>::chunk_mut
has additional logic to check whether the buffer is currently at full capacity, and reserve 64 additional bytes if it is. This is a redundant check when callingbuf.reserve(n)
followed bybuf.chunk_mut()
, but unfortunately the optimization process doesn't seem to be able to eliminate the unreachable branch. I've seen this to have, admittedly small, negative impact on performance in certain scenarios.IMHO,
spare_capacity_mut
is a more descriptive name and I thought it would be helpful to match the std naming, which is why I renamed the method.I've also aped the docs from the std docs for
Vec
, but slightly reworded them to fitBytesMut
:)