Skip to content

Add Reader::skip_exact method #19025

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

canndrew
Copy link
Contributor

This implements the fix for #19022

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

@nodakai
Copy link
Contributor

nodakai commented Nov 17, 2014

Oops, please have a look at my half-baked and stale PR #18137

I didn't have progress in it because I was thinking too much about #13989 (comment)

My current plan is to make skip() a "pure virtual (C++ terminology)" and add an auxiliary method skip_by_read() for its default implementation. Pros: each implementation of the Read trait can choose the most efficient implementation for its skip(). If repeated read() folowed by discarding a buffer is the only way to do so, we can simply implement skip() on top of skip_by_read(). Cons: because Rust trait doesn't have the notion of private/protected methods, exposing an auxiliary method skip_by_read() is a bit ugly.

@nikomatsakis
Copy link
Contributor

I'm not familiar enough with the direction that this API is taking to judge this particular pull request. @alexcrichton or @aturon, would one of you take this up please?

@aturon
Copy link
Member

aturon commented Nov 21, 2014

cc @sfackler @erickt

@canndrew
Copy link
Contributor Author

Here's a thought. A good optimisation for the default implementation might be

fn skip_exact(&mut self, n: uint) -> Result<()> {
    if n < THRESHOLD {
        let buf: [u8, ..THRESHOLD] = unsafe { mem::uninitialized() };
        try!(self.read_at_least(n, buf[..n]));
    }
    else {
        try!(self.read_exact(n));
    }
    Ok(())
}

ie. read to the stack if we're only reading a small amount (I suspect people will often only be skipping a few bytes here or there). Not sure how to pick a good value for THRESHOLD though.

@nodakai
Copy link
Contributor

nodakai commented Nov 21, 2014

All you need is read_at_least() into an on-stack array in a loop: nodakai@9fd9953

@aturon
Copy link
Member

aturon commented Dec 4, 2014

@canndrew I'm sorry for the long delay getting back to you on this. My basic feeling is that this is a relatively rare thing to need to do, and looping with read_at_least with an on-stack array is a simple enough work around, that we don't want to expand the API at this time. (We are also in the process of a complete overhaul of std::io, so I'd like to wait for expansions at least until after that's complete).

@alexcrichton
Copy link
Member

To echo @aturon and my similar comment in @nodakai's PR, I'm going to close this for now in expectation of the upcoming std::io RFC.

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.

6 participants