Add BufRead::skip_until
#63
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
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 a0
byte, but what if I don't care about that information? What if I want to skip over it? It is tempting to useBufRead::read_until(..., b'\0', &mut Vec::new())
but the optimiser does not optimise away the unusedVec
and resulting allocations in this case.Solution sketches
Solution 1
Make the
buf
argument ofBufRead::read_until
anOption<&mut Vec<u8>>
Solution 2
Copy + paste the
BufRead::read_until
implementation and remove thebuf
argument and its resultingextend_from_slice
calls.BufRead::read_until(..., b'\0', &mut Vec::new())
The text was updated successfully, but these errors were encountered: