Skip to content

Commit

Permalink
Merge pull request #2 from t-rapp/pr/seek-nop
Browse files Browse the repository at this point in the history
Avoid flushing buffers when seeking with SeekFrom::Start is a no-op
  • Loading branch information
gcarq authored Dec 5, 2018
2 parents 1f9b68f + 27aad3e commit cabe417
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ impl<R: Read + Seek> Seek for BufReader<R> {
}
}
SeekFrom::Start(n) => {
// Get difference between actual and requested position
let n_bytes = n.checked_sub(self.absolute_pos).unwrap_or(0);
// Check if number of bytes is within buffer range
match n_bytes > 0 && n_bytes < self.available() as u64 {
true => self.seek_forward(n_bytes as usize),
false => self.sync_and_flush(pos)
// Check difference between actual and requested position
match n.checked_sub(self.absolute_pos) {
Some(n_bytes) => self.seek_forward(n_bytes as usize),
None => self.sync_and_flush(pos)
}
}
_ => self.sync_and_flush(pos)
Expand Down

0 comments on commit cabe417

Please sign in to comment.