diff --git a/library/std/src/io/copy.rs b/library/std/src/io/copy.rs index 60962d5afc5b..a75dbc76278d 100644 --- a/library/std/src/io/copy.rs +++ b/library/std/src/io/copy.rs @@ -83,14 +83,19 @@ impl BufferedCopySpec for BufWriter { } 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 BufferedCopySpec for BufWriter { 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;