properly handle EOF in BufReader::peek
previously this would cause an infinite loop due to it being unable to read `n` bytes.
This commit is contained in:
parent
17b322fa69
commit
dfdbf6343a
@ -99,7 +99,10 @@ pub fn with_capacity(capacity: usize, inner: R) -> BufReader<R> {
|
|||||||
impl<R: Read + ?Sized> BufReader<R> {
|
impl<R: Read + ?Sized> BufReader<R> {
|
||||||
/// Attempt to look ahead `n` bytes.
|
/// Attempt to look ahead `n` bytes.
|
||||||
///
|
///
|
||||||
/// `n` must be less than `capacity`.
|
/// `n` must be less than or equal to `capacity`.
|
||||||
|
///
|
||||||
|
/// the returned slice may be less than `n` bytes long if
|
||||||
|
/// end of file is reached.
|
||||||
///
|
///
|
||||||
/// ## Examples
|
/// ## Examples
|
||||||
///
|
///
|
||||||
@ -117,6 +120,7 @@ impl<R: Read + ?Sized> BufReader<R> {
|
|||||||
/// let mut s = String::new();
|
/// let mut s = String::new();
|
||||||
/// rdr.read_to_string(&mut s).unwrap();
|
/// rdr.read_to_string(&mut s).unwrap();
|
||||||
/// assert_eq!(&s, "hello");
|
/// assert_eq!(&s, "hello");
|
||||||
|
/// assert_eq!(rdr.peek(1).unwrap().len(), 0);
|
||||||
/// ```
|
/// ```
|
||||||
#[unstable(feature = "bufreader_peek", issue = "128405")]
|
#[unstable(feature = "bufreader_peek", issue = "128405")]
|
||||||
pub fn peek(&mut self, n: usize) -> io::Result<&[u8]> {
|
pub fn peek(&mut self, n: usize) -> io::Result<&[u8]> {
|
||||||
@ -125,7 +129,11 @@ pub fn peek(&mut self, n: usize) -> io::Result<&[u8]> {
|
|||||||
if self.buf.pos() > 0 {
|
if self.buf.pos() > 0 {
|
||||||
self.buf.backshift();
|
self.buf.backshift();
|
||||||
}
|
}
|
||||||
self.buf.read_more(&mut self.inner)?;
|
let new = self.buf.read_more(&mut self.inner)?;
|
||||||
|
if new == 0 {
|
||||||
|
// end of file, no more bytes to read
|
||||||
|
return Ok(&self.buf.buffer()[..]);
|
||||||
|
}
|
||||||
debug_assert_eq!(self.buf.pos(), 0);
|
debug_assert_eq!(self.buf.pos(), 0);
|
||||||
}
|
}
|
||||||
Ok(&self.buf.buffer()[..n])
|
Ok(&self.buf.buffer()[..n])
|
||||||
|
@ -98,7 +98,7 @@ pub fn unconsume(&mut self, amt: usize) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read more bytes into the buffer without discarding any of its contents
|
/// Read more bytes into the buffer without discarding any of its contents
|
||||||
pub fn read_more(&mut self, mut reader: impl Read) -> io::Result<()> {
|
pub fn read_more(&mut self, mut reader: impl Read) -> io::Result<usize> {
|
||||||
let mut buf = BorrowedBuf::from(&mut self.buf[self.pos..]);
|
let mut buf = BorrowedBuf::from(&mut self.buf[self.pos..]);
|
||||||
let old_init = self.initialized - self.pos;
|
let old_init = self.initialized - self.pos;
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -107,7 +107,7 @@ pub fn read_more(&mut self, mut reader: impl Read) -> io::Result<()> {
|
|||||||
reader.read_buf(buf.unfilled())?;
|
reader.read_buf(buf.unfilled())?;
|
||||||
self.filled += buf.len();
|
self.filled += buf.len();
|
||||||
self.initialized += buf.init_len() - old_init;
|
self.initialized += buf.init_len() - old_init;
|
||||||
Ok(())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove bytes that have already been read from the buffer.
|
/// Remove bytes that have already been read from the buffer.
|
||||||
|
Loading…
Reference in New Issue
Block a user