From 7eece4771b49c4a199b0643f355b1ec6b63d95aa Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Fri, 19 Oct 2018 15:09:08 -0400 Subject: [PATCH 1/4] Add BufWriter::buffer method --- src/libstd/io/buffered.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index e26e6d391f8..5b19c0b81bd 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -525,6 +525,25 @@ pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() } #[stable(feature = "rust1", since = "1.0.0")] pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() } + /// Returns a reference to the internally buffered data. + /// + /// # Examples + /// + /// ```no_run + /// # #![feature(bufreader_buffer)] + /// use std::io::BufWriter; + /// use std::net::TcpStream; + /// + /// let mut buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); + /// + /// // See how many bytes are currently buffered + /// let bytes_buffered = buf_writer.buffer().len(); + /// ``` + #[unstable(feature = "bufreader_buffer", issue = "45323")] + pub fn buffer(&self) -> &[u8] { + &self.buf[..] + } + /// Unwraps this `BufWriter`, returning the underlying writer. /// /// The buffer is written out before returning the writer. From 07d966dbb8bee4ff193293555bd9ff2705cb1ece Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Sat, 27 Oct 2018 12:19:04 -0400 Subject: [PATCH 2/4] Add some tests --- src/libstd/io/buffered.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 5b19c0b81bd..f7b26e5888d 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -541,7 +541,7 @@ pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() } /// ``` #[unstable(feature = "bufreader_buffer", issue = "45323")] pub fn buffer(&self) -> &[u8] { - &self.buf[..] + &self.buf } /// Unwraps this `BufWriter`, returning the underlying writer. @@ -984,31 +984,31 @@ fn test_buffered_reader() { let mut buf = [0, 0, 0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 3); - let b: &[_] = &[5, 6, 7]; - assert_eq!(buf, b); + assert_eq!(buf, [5, 6, 7]); + assert_eq!(*reader.buffer(), []); let mut buf = [0, 0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 2); - let b: &[_] = &[0, 1]; - assert_eq!(buf, b); + assert_eq!(buf, [0, 1]); + assert_eq!(*reader.buffer(), []); let mut buf = [0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); - let b: &[_] = &[2]; - assert_eq!(buf, b); + assert_eq!(buf, [2]); + assert_eq!(*reader.buffer(), [3]); let mut buf = [0, 0, 0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); - let b: &[_] = &[3, 0, 0]; - assert_eq!(buf, b); + assert_eq!(buf, [3, 0, 0]); + assert_eq!(*reader.buffer(), []); let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); - let b: &[_] = &[4, 0, 0]; - assert_eq!(buf, b); + assert_eq!(buf, [4, 0, 0]); + assert_eq!(*reader.buffer(), []); assert_eq!(reader.read(&mut buf).unwrap(), 0); } @@ -1097,31 +1097,40 @@ fn test_buffered_writer() { let mut writer = BufWriter::with_capacity(2, inner); writer.write(&[0, 1]).unwrap(); + assert_eq!(*writer.buffer(), []); assert_eq!(*writer.get_ref(), [0, 1]); writer.write(&[2]).unwrap(); + assert_eq!(*writer.buffer(), [2]); assert_eq!(*writer.get_ref(), [0, 1]); writer.write(&[3]).unwrap(); + assert_eq!(*writer.buffer(), [2, 3]); assert_eq!(*writer.get_ref(), [0, 1]); writer.flush().unwrap(); + assert_eq!(*writer.buffer(), []); assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); writer.write(&[4]).unwrap(); writer.write(&[5]).unwrap(); + assert_eq!(*writer.buffer(), [4, 5]); assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); writer.write(&[6]).unwrap(); + assert_eq!(*writer.buffer(), [6]); assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]); writer.write(&[7, 8]).unwrap(); + assert_eq!(*writer.buffer(), []); assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); writer.write(&[9, 10, 11]).unwrap(); + assert_eq!(*writer.buffer(), []); assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); writer.flush().unwrap(); + assert_eq!(*writer.buffer(), []); assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); } From 40e41440e46a56b5d5fd66473c23efff6d44be3f Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Thu, 1 Nov 2018 13:29:47 -0400 Subject: [PATCH 3/4] Suggested edits --- src/libstd/io/buffered.rs | 48 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index f7b26e5888d..12d1afe6069 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -534,7 +534,7 @@ pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() } /// use std::io::BufWriter; /// use std::net::TcpStream; /// - /// let mut buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); + /// let buf_writer = BufWriter::new(TcpStream::connect("127.0.0.1:34254").unwrap()); /// /// // See how many bytes are currently buffered /// let bytes_buffered = buf_writer.buffer().len(); @@ -985,30 +985,30 @@ fn test_buffered_reader() { let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 3); assert_eq!(buf, [5, 6, 7]); - assert_eq!(*reader.buffer(), []); + assert_eq!(reader.buffer(), []); let mut buf = [0, 0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 2); assert_eq!(buf, [0, 1]); - assert_eq!(*reader.buffer(), []); + assert_eq!(reader.buffer(), []); let mut buf = [0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); assert_eq!(buf, [2]); - assert_eq!(*reader.buffer(), [3]); + assert_eq!(reader.buffer(), [3]); let mut buf = [0, 0, 0]; let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); assert_eq!(buf, [3, 0, 0]); - assert_eq!(*reader.buffer(), []); + assert_eq!(reader.buffer(), []); let nread = reader.read(&mut buf); assert_eq!(nread.unwrap(), 1); assert_eq!(buf, [4, 0, 0]); - assert_eq!(*reader.buffer(), []); + assert_eq!(reader.buffer(), []); assert_eq!(reader.read(&mut buf).unwrap(), 0); } @@ -1097,41 +1097,41 @@ fn test_buffered_writer() { let mut writer = BufWriter::with_capacity(2, inner); writer.write(&[0, 1]).unwrap(); - assert_eq!(*writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1]); + assert_eq!(writer.buffer(), []); + assert_eq!(writer.get_ref(), [0, 1]); writer.write(&[2]).unwrap(); - assert_eq!(*writer.buffer(), [2]); - assert_eq!(*writer.get_ref(), [0, 1]); + assert_eq!(writer.buffer(), [2]); + assert_eq!(writer.get_ref(), [0, 1]); writer.write(&[3]).unwrap(); - assert_eq!(*writer.buffer(), [2, 3]); - assert_eq!(*writer.get_ref(), [0, 1]); + assert_eq!(writer.buffer(), [2, 3]); + assert_eq!(writer.get_ref(), [0, 1]); writer.flush().unwrap(); - assert_eq!(*writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); + assert_eq!(writer.buffer(), []); + assert_eq!(writer.get_ref(), [0, 1, 2, 3]); writer.write(&[4]).unwrap(); writer.write(&[5]).unwrap(); - assert_eq!(*writer.buffer(), [4, 5]); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); + assert_eq!(writer.buffer(), [4, 5]); + assert_eq!(writer.get_ref(), [0, 1, 2, 3]); writer.write(&[6]).unwrap(); - assert_eq!(*writer.buffer(), [6]); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]); + assert_eq!(writer.buffer(), [6]); + assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5]); writer.write(&[7, 8]).unwrap(); - assert_eq!(*writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); + assert_eq!(writer.buffer(), []); + assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); writer.write(&[9, 10, 11]).unwrap(); - assert_eq!(*writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + assert_eq!(writer.buffer(), []); + assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); writer.flush().unwrap(); - assert_eq!(*writer.buffer(), []); - assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + assert_eq!(writer.buffer(), []); + assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); } #[test] From 59a030916b8f1173c99f874365533574fa6314a9 Mon Sep 17 00:00:00 2001 From: Jonathan Behrens Date: Thu, 1 Nov 2018 16:06:41 -0400 Subject: [PATCH 4/4] Fix compile errors in test --- src/libstd/io/buffered.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/libstd/io/buffered.rs b/src/libstd/io/buffered.rs index 12d1afe6069..476ee3f71ca 100644 --- a/src/libstd/io/buffered.rs +++ b/src/libstd/io/buffered.rs @@ -1098,40 +1098,40 @@ fn test_buffered_writer() { writer.write(&[0, 1]).unwrap(); assert_eq!(writer.buffer(), []); - assert_eq!(writer.get_ref(), [0, 1]); + assert_eq!(*writer.get_ref(), [0, 1]); writer.write(&[2]).unwrap(); assert_eq!(writer.buffer(), [2]); - assert_eq!(writer.get_ref(), [0, 1]); + assert_eq!(*writer.get_ref(), [0, 1]); writer.write(&[3]).unwrap(); assert_eq!(writer.buffer(), [2, 3]); - assert_eq!(writer.get_ref(), [0, 1]); + assert_eq!(*writer.get_ref(), [0, 1]); writer.flush().unwrap(); assert_eq!(writer.buffer(), []); - assert_eq!(writer.get_ref(), [0, 1, 2, 3]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); writer.write(&[4]).unwrap(); writer.write(&[5]).unwrap(); assert_eq!(writer.buffer(), [4, 5]); - assert_eq!(writer.get_ref(), [0, 1, 2, 3]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3]); writer.write(&[6]).unwrap(); assert_eq!(writer.buffer(), [6]); - assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5]); writer.write(&[7, 8]).unwrap(); assert_eq!(writer.buffer(), []); - assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8]); writer.write(&[9, 10, 11]).unwrap(); assert_eq!(writer.buffer(), []); - assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); writer.flush().unwrap(); assert_eq!(writer.buffer(), []); - assert_eq!(writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); + assert_eq!(*writer.get_ref(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]); } #[test]