Introduce Cursor and CursorMut traits#825
Closed
mhebant wants to merge 1 commit intotokio-rs:masterfrom
Closed
Conversation
Member
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Has many others before, I've been looking for a way to read or write ahead, or seek into a
Bufor aBufMut(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
BufandBufMut).The simple solution to read ahead in a
BufI've seen proposed (like here #382 (comment)) is to clone theBuf. But makingT: Buf + Cloneyour interface wont work in practice: it's fine if yourBufis a&[u8]but not if it's aVec<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 aChain<Vec<u8>, Vec<u8>>it's aChain<&[u8], &[u8]>etc...The Cursor trait allows you to get a cheap copy of you
Bufthat you can use to read ahead without mutating the originalBuf.This should also work for writing ahead to a
BufMutbefore 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 !