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

Seeking from Current(0) return current stream position. #70577

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct BufReader<R> {
buf: Box<[u8]>,
pos: usize,
cap: usize,
inner_pos: u64,
}

impl<R: Read> BufReader<R> {
Expand Down Expand Up @@ -359,6 +360,10 @@ impl<R: Seek> Seek for BufReader<R> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
let result: u64;
if let SeekFrom::Current(n) = pos {
if n == 0 {
return Ok(self.inner_pos);
}

let remainder = (self.cap - self.pos) as i64;
// it should be safe to assume that remainder fits within an i64 as the alternative
// means we managed to allocate 8 exbibytes and that's absurd.
Expand All @@ -378,6 +383,7 @@ impl<R: Seek> Seek for BufReader<R> {
result = self.inner.seek(pos)?;
}
self.discard_buffer();
self.inner_pos = result;
Ok(result)
}
}
Expand Down