Skip to content

feat: Impl BufReader for std::io::Read#54

Closed
cpubot wants to merge 1 commit intomasterfrom
buf-reader-no-grow
Closed

feat: Impl BufReader for std::io::Read#54
cpubot wants to merge 1 commit intomasterfrom
buf-reader-no-grow

Conversation

@cpubot
Copy link
Contributor

@cpubot cpubot commented Dec 12, 2025

This PR adds a new Reader impl, BufReader which adds Reader support for any std::io::Read.

The implementation is generally what one would expect from a buffered reader implementation. The key nuance to pay attention to is the as_trusted_for implementation. Specifically, BufReader's Trusted implementation uses the following enum:

pub enum TrustedBufReader<'a, 'b, R: ?Sized> {
    WithinCapacity(TrustedSliceReader<'a, 'b>),
    ExceedsCapacity(BufReaderMut<'b, R>),
}

This type captures two possible scenarios when requesting a trusted window:

  1. The requested window is within the buffer capacity.
  2. The requested window is larger than the buffer capacity.

In the first case, we can provide a TrustedSliceReader, which achieves the desired trusted semantics where bounds checks are eliminated, and is very performant.
In the second case, we provide a BufReaderMut, which effectively acts as a typical non-trusted BufReader.

An alternative implementation could reallocate the buffer to accomodate the requested window size, but this route provides simplicity, predictability, and user-tuneability. In particular, rather than implicitly reallocating on behalf of the user, one may simply tune capacity for a specific use case.

See #53 for a longer discussion on these points.

Closes #50

@cpubot cpubot force-pushed the buf-reader-no-grow branch 4 times, most recently from 38d14f7 to 80aed37 Compare February 16, 2026 22:44
@cpubot cpubot changed the title RFC: Impl BufReader for std::io::Read (no grow) Impl BufReader for std::io::Read (no grow) Feb 16, 2026
@cpubot cpubot changed the title Impl BufReader for std::io::Read (no grow) feat: Impl BufReader for std::io::Read (no grow) Feb 16, 2026
@cpubot cpubot changed the title feat: Impl BufReader for std::io::Read (no grow) feat: Impl BufReader for std::io::Read Feb 16, 2026
@cpubot cpubot marked this pull request as ready for review February 16, 2026 23:00
@cpubot cpubot force-pushed the buf-reader-no-grow branch from 80aed37 to 56e1268 Compare February 17, 2026 17:52
@cpubot cpubot requested a review from kskalski February 17, 2026 17:52
@cpubot cpubot closed this Mar 24, 2026
@cpubot cpubot deleted the buf-reader-no-grow branch March 24, 2026 16:58
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

Successfully merging this pull request may close these issues.

Deserializing from BufReader?

1 participant