Skip to content

Introduce Cursor and CursorMut traits#825

Closed
mhebant wants to merge 1 commit intotokio-rs:masterfrom
mhebant:master
Closed

Introduce Cursor and CursorMut traits#825
mhebant wants to merge 1 commit intotokio-rs:masterfrom
mhebant:master

Conversation

@mhebant
Copy link
Copy Markdown

@mhebant mhebant commented Mar 29, 2026

Has many others before, I've been looking for a way to read or write ahead, or seek into a Buf or a BufMut (see #823, #658, #382 to name a few)

While this PR might not solve everyone's problems, I has the benefit of not braking the API (it only adds 2 new traits) and simple (by relying as much as possible on the existing Buf and BufMut).

The simple solution to read ahead in a Buf I've seen proposed (like here #382 (comment)) is to clone the Buf. But making T: Buf + Clone your interface wont work in practice: it's fine if your Buf is a &[u8] but not if it's a Vec<u8>.

So the Idea is to add a trait (named Cursor for now) whit an associated type that is a cheap version of our Buf.
For example the Cursor of a Vec<u8> is a &[u8], for a Chain<Vec<u8>, Vec<u8>> it's a Chain<&[u8], &[u8]> etc...

The Cursor trait allows you to get a cheap copy of you Buf that you can use to read ahead without mutating the original Buf.

This should also work for writing ahead to a BufMut before possibly committing what have been written by calling advance.

This PR is incomplete, lacks some implementation and tests, but I'm looking for feedback before finishing it.

Also I understand adding new features is complicated because any it's impossible to make breaking change. It should be possible to put this in it's own create depending on bytes. But again looking for some feedback.

Thanks !

@Darksonn
Copy link
Copy Markdown
Member

Darksonn commented Apr 4, 2026

I've spent some time thinking about this. Unfortunately I think the desire to keep bytes simple outweighs this feature request, so I'm going to close it.

@Darksonn Darksonn closed this Apr 4, 2026
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.

2 participants