Skip to content

Add fn skip(n: uint) with a default implementation to Reader #13989

Closed

Description

@carllerche

Reader should have an extra fn that allows skipping a number of bytes. The default implementation could look something like:

fn skip(n: uint) { let _ = self.read(Vec::from_elem(n, 0u8); }

However, BufReader could implement the fn by simply updating the internal cursor. This would allow more efficient skipping w/o having to require Seek.

Activity

  1. pongad commented on May 14, 2014

    @pongad
    Contributor

    This seems useful though reader does have a few methods on it already. I'm wondering what @alexcrichton thinks?
    Edit: I'm not sure if this is a worthwhile change anymore. We can easily to reader.read_exact(n) to achieve the same thing.

  2. alexcrichton commented on May 14, 2014

    @alexcrichton
    Member

    I'm worried about adding more methods to readers that downstream consumers need to consider to override. Having one flush() method is unfortunate, and I think time should be taken to consider whether this is justified to add it or not.

    I don't think that we can quite use read_exact for this because in theory skip doesn't actually have to read any data. It does sound quite similar to seek though...

  3. nox commented on Jul 11, 2014

    @nox
    Contributor

    Any news on this? I'm writing an IFF reader and I want users to be able to skip chunk bodies, requiring Seek for just this seems overkill.

  4. wycats commented on Jul 20, 2014

    @wycats
    Contributor

    The problem is that given today's (and likely future) coherence rules, you cannot create a trait that uses Seek if available but otherwise falls back to a less efficient Reader:

    impl<T: Seek> PossiblyInefficientSeek for T {
        // ...
    }
    
    impl<T: Reader> PossiblyInefficientSeek for T {
        // ...
    }

    This is actually a decent motivating use-case for allowing an implementation of a trait to refine an existing implementation defined on a superset of types.

  5. wycats commented on Jul 20, 2014

    @wycats
    Contributor

    @pongad I do not think read_exact is appropriate, since it requires allocating a buffer of n size even if the backend supports efficient skipping.

  6. nikomatsakis commented on Aug 6, 2014

    @nikomatsakis
    Contributor

    @alexcrichton it is true that having too many random convenience methods can be a problem, but I think that it makes sense to add methods for operations that can be implemented much more efficiently in some contexts than others. In other words, while skip is technically implementable as a read, there is a big efficiency difference between a call to fseek and reading tons of bytes.

  7. steveklabnik commented on Feb 14, 2015

    @steveklabnik
    Contributor

    @carllerche did this end up getting addressed in io reform?

  8. carllerche commented on Feb 14, 2015

    @carllerche
    ContributorAuthor

    @steveklabnik Not yet. I think (hope) it is coming pre 1.0 though. cc @aturon

  9. aturon commented on Feb 16, 2015

    @aturon
    Contributor

    Nominating for 1.0-beta P-high. (Should be trivial.)

  10. pnkfelix commented on Feb 19, 2015

    @pnkfelix
    Contributor

    P-high, I-needs-decision (not 1.0 beta blocker)

  11. added
    T-libs-api[DEPRECATED; DO NOT USE]
    and removed on Jan 8, 2016
  12. alexcrichton commented on Jan 21, 2016

    @alexcrichton
    Member

    In today's day and age this sort of addition (to a core trait like Read) will require an RFC, so I'm going to close this in favor of that course of action.

  13. youknowone commented on Apr 21, 2021

    @youknowone
    Contributor
  14. added a commit that references this issue on Feb 13, 2023
  15. added a commit that references this issue on Jan 28, 2025
    e692cd4
  16. added a commit that references this issue on Feb 13, 2025
    b41f54e
  17. added a commit that references this issue on Feb 17, 2025
    acc7e1c
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

    P-mediumMedium priorityT-libs-api[DEPRECATED; DO NOT USE]

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions