bytes_mut.reserve(..) makes the guarantee that the underlying allocation will be reclaimed when bytes_mut is the only remaining handle to it. However, when it isn't the only remaining handle, new space is allocated and the handle to the old allocation is discarded.
BytesMut should offer a method to request that the underlying allocation be reclaimed without discarding the old allocation when the attempt fails. Something along the lines of the following.
impl BytesMut {
/// Attempts to reclaim the underlying allocation.
///
/// The attempt will succeed iff this is the last remaining handle
/// to the allocation.
///
/// When the attempt succeeds, an empty `BytesMut` with the full
/// capacity of the allocation is returned. Otherwise, the original
/// handle is returned.
pub fn try_reclaim(self) -> Result<Self, Self>;
}
I'd be happy to implement this proposal and file a PR for it.
bytes_mut.reserve(..)
makes the guarantee that the underlying allocation will be reclaimed whenbytes_mut
is the only remaining handle to it. However, when it isn't the only remaining handle, new space is allocated and the handle to the old allocation is discarded.BytesMut
should offer a method to request that the underlying allocation be reclaimed without discarding the old allocation when the attempt fails. Something along the lines of the following.I'd be happy to implement this proposal and file a PR for it.