bytes icon indicating copy to clipboard operation
bytes copied to clipboard

Unable to try_reclaim(..) a BytesMut allocation

Open benschulz opened this issue 2 years ago • 1 comments

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.

benschulz avatar Aug 04 '23 16:08 benschulz

I think this is a pretty decent idea.

bojo-byte avatar Aug 12 '23 09:08 bojo-byte