about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--library/std/src/io/copy.rs9
1 files changed, 8 insertions, 1 deletions
diff --git a/library/std/src/io/copy.rs b/library/std/src/io/copy.rs
index 60962d5afc5..a75dbc76278 100644
--- a/library/std/src/io/copy.rs
+++ b/library/std/src/io/copy.rs
@@ -83,14 +83,19 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
         }
 
         let mut len = 0;
+        let mut init = 0;
 
         loop {
             let buf = writer.buffer_mut();
             let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
 
+            // SAFETY: init is either 0 or the initialized_len of the previous iteration
+            unsafe {
+                read_buf.assume_init(init);
+            }
+
             if read_buf.capacity() >= DEFAULT_BUF_SIZE {
                 match reader.read_buf(&mut read_buf) {
-                    //Ok(0) => return Ok(len), // EOF reached
                     Ok(()) => {
                         let bytes_read = read_buf.filled_len();
 
@@ -98,6 +103,8 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
                             return Ok(len);
                         }
 
+                        init = read_buf.initialized_len();
+
                         // SAFETY: ReadBuf guarantees all of its filled bytes are init
                         unsafe { buf.set_len(buf.len() + bytes_read) };
                         len += bytes_read as u64;