From e76929ff981cd50e6a4d4936bf1f89ed625b032a Mon Sep 17 00:00:00 2001 From: YuhanLiin Date: Sat, 29 May 2021 17:47:51 -0400 Subject: [PATCH 1/2] Add has_data_left() to BufRead --- library/std/src/io/mod.rs | 31 +++++++++++++++++++++++++++++++ library/std/src/io/tests.rs | 10 ++++++++++ 2 files changed, 41 insertions(+) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 4c154dbe01a5a..21cad51bf930d 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1953,6 +1953,37 @@ pub trait BufRead: Read { #[stable(feature = "rust1", since = "1.0.0")] fn consume(&mut self, amt: usize); + /// Check if the underlying `Read` has any data left to be read. + /// + /// This function may fill the buffer to check for data, + /// so this functions returns `Result`, not `bool`. + /// + /// Default implementation calls `fill_buf` and checks that + /// returned slice is empty (which means that there is no data left, + /// since EOF is reached). + /// + /// Examples + /// + /// ``` + /// #![feature(buf_read_has_data_left)] + /// use std::io; + /// use std::io::prelude::*; + /// + /// let stdin = io::stdin(); + /// let mut stdin = stdin.lock(); + /// + /// while stdin.has_data_left().unwrap() { + /// let mut line = String::new(); + /// stdin.read_line(&mut line).unwrap(); + /// // work with line + /// println!("{:?}", line); + /// } + /// ``` + #[unstable(feature = "buf_read_has_data_left", reason = "recently added", issue = "40745")] + fn has_data_left(&mut self) -> Result { + self.fill_buf().map(|b| !b.is_empty()) + } + /// Read all bytes into `buf` until the delimiter `byte` or EOF is reached. /// /// This function will read bytes from the underlying stream until the diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index 2b14e16150317..4206b79405864 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -71,6 +71,16 @@ fn lines() { assert!(s.next().is_none()); } +#[test] +fn buf_read_has_data_left() { + let mut buf = Cursor::new(&b"abcd"[..]); + assert!(buf.has_data_left().unwrap()); + buf.read_exact(&mut [0; 2]).unwrap(); + assert!(buf.has_data_left().unwrap()); + buf.read_exact(&mut [0; 2]).unwrap(); + assert!(!buf.has_data_left().unwrap()); +} + #[test] fn read_to_end() { let mut c = Cursor::new(&b""[..]); From 99939c44c3fabbd4bfcb87839072ba563ce12d9b Mon Sep 17 00:00:00 2001 From: YuhanLiin Date: Thu, 17 Jun 2021 23:17:16 -0400 Subject: [PATCH 2/2] Update tracking issue --- library/std/src/io/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 21cad51bf930d..6963f4132ac90 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -1979,7 +1979,7 @@ pub trait BufRead: Read { /// println!("{:?}", line); /// } /// ``` - #[unstable(feature = "buf_read_has_data_left", reason = "recently added", issue = "40745")] + #[unstable(feature = "buf_read_has_data_left", reason = "recently added", issue = "86423")] fn has_data_left(&mut self) -> Result { self.fill_buf().map(|b| !b.is_empty()) }