Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to try_reclaim(..) a BytesMut allocation #620

Open
benschulz opened this issue Aug 4, 2023 · 1 comment
Open

Unable to try_reclaim(..) a BytesMut allocation #620

benschulz opened this issue Aug 4, 2023 · 1 comment

Comments

@benschulz
Copy link

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.

@bojo-byte
Copy link

I think this is a pretty decent idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants