Skip to content
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

Add BufRead::skip_until #63

Closed
WilliamVenner opened this issue Jul 5, 2022 · 2 comments
Closed

Add BufRead::skip_until #63

WilliamVenner opened this issue Jul 5, 2022 · 2 comments
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api

Comments

@WilliamVenner
Copy link

Proposal

Problem statement

Rust provides a way to "read everything up to x" (BufRead::read_until), but overlooks that you may want to discard that data, ergo, ignore it.

Motivation, use-cases

When reading a binary file format, you may encounter NUL-terminated strings. These can be easily read using BufRead::read_until to read up to a 0 byte, but what if I don't care about that information? What if I want to skip over it? It is tempting to use BufRead::read_until(..., b'\0', &mut Vec::new()) but the optimiser does not optimise away the unused Vec and resulting allocations in this case.

Solution sketches

Solution 1

Make the buf argument of BufRead::read_until an Option<&mut Vec<u8>>

  • Introduces a breaking change
  • Results in slower code for both Some and None cases
  • Harshly non-self-documenting, needs explanation for its behaviour, isn't immediately obvious

Solution 2

Copy + paste the BufRead::read_until implementation and remove the buf argument and its resulting extend_from_slice calls.

  • Specialized for this use case
  • In my benchmarks is roughly x2 faster than BufRead::read_until(..., b'\0', &mut Vec::new())
@WilliamVenner WilliamVenner added api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api labels Jul 5, 2022
@joshtriplett joshtriplett added the ACP-accepted API Change Proposal is accepted (seconded with no objections) label May 17, 2023
@joshtriplett
Copy link
Member

joshtriplett commented May 17, 2023

Discussed in today's libs meetup. 👍 for solution 2: add a skip_until function that skips over the data.

Please do submit a PR.

Sorry for the long latency.

@WilliamVenner
Copy link
Author

PR rust-lang/rust#98943 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ACP-accepted API Change Proposal is accepted (seconded with no objections) api-change-proposal A proposal to add or alter unstable APIs in the standard libraries T-libs-api
Projects
None yet
Development

No branches or pull requests

2 participants