Skip to content

provide a peeking API to std.io.BufferedReader #4501

Closed

Description

@andrewrk

The point of BufferedInStream is to call read() on the underlying stream in bursts, rather than separate calls. E.g. to make 1 syscall to read() reading 999 bytes, rather than 999 syscalls to read() reading 1 byte each.

The point of PeekStream is to be able to "put back" bytes, so that the next call to read() will get them again. In order to accomplish this, PeekStream needs a buffer. That makes PeekStream also a BufferedInStream.

All we need to unify these two APIs is for BufferedInStream to have a parameter called reserved_buffer_space or something like this, and then it's guaranteed to be able to "put back" this many elements.

Related:

Activity

  1. added
    breaking
    standard library
    proposal
    on Feb 18, 2020
  2. added this to the 0.7.0 milestone on Feb 18, 2020
  3. added
    accepted
    on Feb 19, 2020
  4. modified the milestones: 0.7.0, 0.8.0 on Oct 9, 2020
  5. modified the milestones: 0.8.0, 0.9.0 on May 19, 2021
  6. modified the milestones: 0.9.0, 0.10.0 on Nov 20, 2021
  7. modified the milestones: 0.10.0, 0.11.0 on Apr 16, 2022
  8. modified the milestones: 0.11.0, 0.12.0 on Apr 9, 2023
  9. modified the milestones: 0.13.0, 0.12.0 on Jun 29, 2023
  10. changed the title [-]merge std.io.PeekStream and std.io.BufferedInStream[/-] [+]provide a peeking API to std.io.BufferedReader[/+] on Mar 14, 2024
  11. modified the milestones: 0.13.0, 0.15.0 on Mar 14, 2024
  12. pierrec commented on Mar 14, 2024

    @pierrec
    Contributor

    Hello,

    Since I make use of io.PeekStream and it just got removed, I would like to contribute to this issue.
    The idea is to simply implement the old methods on BufferedReader.

    pub fn putBackByte(self: *Self, byte: u8) !void { ... }
    
    pub fn putBack(self: *Self, byte: []u8) !void { ... }
    

    Would this be acceptable?

  13. nektro commented on Mar 14, 2024

    @nektro
    Contributor

    that seems backwards. there should be a peek() function that returns br.buf[br.start] and read() should eat the byte

  14. archaistvolts commented on Mar 14, 2024

    @archaistvolts
    Contributor

    that seems backwards. there should be a peek()

    I agree that it would be nice to have a peek(). How about peek([]u8) !void and peekByte() !u8?

    But previously io.PeekStream() had putBackByte(u8) and putBack([]const u8). So maybe we could have both peek() and putBack()?

  15. septemhill commented on Oct 1, 2024

    @septemhill

    I have a question, how do we handle the data after peek ? use read to consume or add one more method discard to ignore the data (byte or slice) ?

    Edit: I didn't notice that the putBack maybe the solution of my question, sorry

  16. modified the milestones: upcoming, 0.15.0 on Aug 5, 2025
  17. andrewrk commented on Aug 5, 2025

    @andrewrk
    MemberAuthor

    Solved by #24329

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedbreakingproposalstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions