Skip to content

feat(bytesbuf): add BytesBuf::split_off_remaining#286

Merged
sandersaares merged 5 commits intomainfrom
feature/bytesbuf-split-off-remaining
Feb 26, 2026
Merged

feat(bytesbuf): add BytesBuf::split_off_remaining#286
sandersaares merged 5 commits intomainfrom
feature/bytesbuf-split-off-remaining

Conversation

@sandersaares
Copy link
Member

@sandersaares sandersaares commented Feb 25, 2026

Summary

Add split_off_remaining() and split_off_remaining_checked() methods to BytesBuf that detach a specified number of bytes of remaining (unfilled) capacity from the end of the buffer, returning a new empty BytesBuf that owns that capacity. The original buffer's filled data is unaffected.

This is a utility function that can be beneficial in test code and in specialized situations where cutting buffers to specific capacities is useful — for example, to pre-partition capacity across multiple buffers from a single reservation.

API

// Panics if count > remaining_capacity().
pub fn split_off_remaining(&mut self, count: usize) -> BytesBuf;

// Returns None if count > remaining_capacity().
pub fn split_off_remaining_checked(&mut self, count: usize) -> Option<BytesBuf>;

Follows the established crate pattern of panicking + checked variant pairs (like consume/consume_checked and begin_vectored_write/begin_vectored_write_checked). The naming follows the split_off convention from the standard library (Vec::split_off, String::split_off).

Changes

  • span_builder.rs: Added internal SpanBuilder::split_off_available() helper that splits off unfilled capacity from the end of a span builder into a new SpanBuilder sharing the same memory block.
  • buf.rs: Added split_off_remaining() and split_off_remaining_checked() on BytesBuf. The implementation drains complete empty span builders from the end and, if needed, splits a partially-used builder's available capacity.
  • Unit tests for all new methods covering: basic usage, multi-block buffers, writing to split-off buffers, zero-count edge case, full-capacity split, panic on overflow, and checked-returns-None.

Add split_off_remaining() and split_off_remaining_checked() methods to
BytesBuf that detach a specified number of bytes of remaining capacity
from the end of the buffer, returning a new empty BytesBuf that owns
that capacity.

This is a utility function that can be beneficial in test code and in
specialized situations where cutting buffers to specific capacities is
useful (e.g. to pre-partition capacity across multiple buffers from a
single reservation).

Also adds the internal SpanBuilder::split_off_available() helper that
enables splitting a single span builder's unfilled capacity.
@sandersaares sandersaares marked this pull request as ready for review February 25, 2026 14:43
@codecov
Copy link

codecov bot commented Feb 25, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.0%. Comparing base (496da35) to head (7c241c5).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #286   +/-   ##
=======================================
  Coverage   100.0%   100.0%           
=======================================
  Files         141      141           
  Lines        8664     8715   +51     
=======================================
+ Hits         8664     8715   +51     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@ZacWalk ZacWalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Might be good to add a test for split_off_remaining on an empty buffer with no capacity (Just a suggestion though)

… capacity

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sandersaares sandersaares enabled auto-merge (squash) February 26, 2026 09:16
@sandersaares sandersaares merged commit 04db046 into main Feb 26, 2026
27 checks passed
@sandersaares sandersaares deleted the feature/bytesbuf-split-off-remaining branch February 26, 2026 10:34
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

Successfully merging this pull request may close these issues.

4 participants