Skip to content

Commit

Permalink
rand: Use fill() instead of read()
Browse files Browse the repository at this point in the history
It's possible for a reader to have a short read, and there's no reason the task
should fail in this scenario. By using fill(), this has a stronger guarantee
that the buffer will get filled with data.
  • Loading branch information
alexcrichton committed Mar 22, 2014
1 parent 5560383 commit 02dab5a
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/librand/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ impl<R: Reader> Rng for ReaderRng<R> {
}
fn fill_bytes(&mut self, v: &mut [u8]) {
if v.len() == 0 { return }
match self.reader.read(v) {
Ok(n) if n == v.len() => return,
Ok(n) => fail!("ReaderRng.fill_bytes could not fill buffer: \
read {} out of {} bytes.", n, v.len()),
match self.reader.fill(v) {
Ok(()) => {}
Err(e) => fail!("ReaderRng.fill_bytes error: {}", e)
}
}
Expand Down

1 comment on commit 02dab5a

@alexcrichton
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=huonw

Please sign in to comment.