Skip to content
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

BufReader .consume() confuses the Reader seek point when the buffer is empty #23196

Closed
jorisgio opened this issue Mar 8, 2015 · 4 comments · Fixed by #23622
Closed

BufReader .consume() confuses the Reader seek point when the buffer is empty #23196

jorisgio opened this issue Mar 8, 2015 · 4 comments · Fixed by #23622

Comments

@jorisgio
Copy link

jorisgio commented Mar 8, 2015

The behavior of .consume when the buffer is empty is weird :

let mut f = BufReader::new(Cursor::new(vec![65, 255, 42]));
f.consume(1);
let x = &mut [0; 1];
println!("{}", f.read(x).unwrap()); // prints 0

while :

let mut f = BufReader::new(Cursor::new(vec![65, 255, 42]));
let x = &mut [0; 1];
f.read(x).unwrap();
f.consume(1);
println!("{}", f.read(x).unwrap()); // prints 1, x[0] is 42
@alexcrichton
Copy link
Member

Currently the contract of consume is that you can only pass a number >= the number of bytes you receive from fill_buf. In this case the method is never being called, causing the weird behavior.

I believe this is a documentation issue as the documentation for the consume method is pretty curt.

@jorisgio
Copy link
Author

jorisgio commented Mar 9, 2015

Maybe adding an assert_debug to check that could be useful too ?

@cskr
Copy link

cskr commented Mar 10, 2015

@alexcrichton What's the intended use of consume if it must always be passed a value >= the return value of fill_buf?

@alexcrichton
Copy link
Member

Oh sorry, I meant <=! The function is intended to tell the buffer how many bytes you've consumed from the return value of fill_buf

steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 23, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Mar 23, 2015
alexcrichton added a commit to alexcrichton/rust that referenced this issue Mar 23, 2015
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 a pull request may close this issue.

3 participants