Skip to content

Add BufRead::skip_until #63

Closed
Closed
@WilliamVenner

Description

@WilliamVenner

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())

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions