Skip to content

Commit f92241d

Browse files
committed
Don't reinitialize here
1 parent 5a97090 commit f92241d

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

library/std/src/io/copy.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,28 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
8383
}
8484

8585
let mut len = 0;
86+
let mut init = 0;
8687

8788
loop {
8889
let buf = writer.buffer_mut();
8990
let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
9091

92+
// SAFETY: init is either 0 or the initialized_len of the previous iteration
93+
unsafe {
94+
read_buf.assume_init(init);
95+
}
96+
9197
if read_buf.capacity() >= DEFAULT_BUF_SIZE {
9298
match reader.read_buf(&mut read_buf) {
93-
//Ok(0) => return Ok(len), // EOF reached
9499
Ok(()) => {
95100
let bytes_read = read_buf.filled_len();
96101

97102
if bytes_read == 0 {
98103
return Ok(len);
99104
}
100105

106+
init = read_buf.initialized_len();
107+
101108
// SAFETY: ReadBuf guarantees all of its filled bytes are init
102109
unsafe { buf.set_len(buf.len() + bytes_read) };
103110
len += bytes_read as u64;

0 commit comments

Comments
 (0)