Skip to content

I'd like an efficient way to discard data from a Reader. #19022

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

Closed
canndrew opened this issue Nov 17, 2014 · 1 comment
Closed

I'd like an efficient way to discard data from a Reader. #19022

canndrew opened this issue Nov 17, 2014 · 1 comment

Comments

@canndrew
Copy link
Contributor

The problem.

Say I want to skip the next n bytes of input from a Reader. Currently, I can see two ways of doing this, neither of which are ideal.

  • Allocate a Vec<u8> of size n. Call read on it. Discard the Vec<u8>. This has the disadvantage of doing an unnecessary allocation and unnecessary copying. Note that calling read_exact is equivalent to this.
  • Call read_u8 n times. This has this the disadvantage of doing n function calls, which may even translate to n syscalls. For any significant n this is going to be worse than the first option.

The solution.

Have Reader expose a method skip_exact that skips the next n bytes. For some readers such as MemReader and BufferedReader this just means advancing the cursor by n. For some other readers this could be implemented using seek. For other readers again this method can be given a default implementation that just calls read_exact.

Note that I can't just call seek(n, SeekCur). Not all readers implement Seek (socket streams for example) but all readers can be advanced from their current position by - in the worst case - just reading and throwing away data.

@aturon
Copy link
Member

aturon commented Jan 24, 2015

Closing as a dup of #13989

@aturon aturon closed this as completed Jan 24, 2015
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