Unable to try_reclaim(..) a BytesMut allocation

Open
#620 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
48/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
tooling

Research direction

Start at the BytesMut::reserve entry point and trace how allocation ownership is handled when the buffer has shared handles. The work is done when a try_reclaim API follows the proposed success and failure semantics, with behavior checked for both sole and shared handles.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Rust
Stars
2.3k
Forks
355
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from tokio-rs/bytes

All issues in tokio-rs/bytes

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.