]> git.lizzy.rs Git - rust.git/blobdiff - library/std/src/io/readbuf.rs
Rollup merge of #103594 - maniwani:fix-issue-91417, r=thomcc
[rust.git] / library / std / src / io / readbuf.rs
index b1a84095f13fa7e9b1823935998794fa3713f481..4800eeda022bb7753ed60a1e051bb0393380086a 100644 (file)
@@ -3,10 +3,10 @@
 #[cfg(test)]
 mod tests;
 
-use crate::cmp;
 use crate::fmt::{self, Debug, Formatter};
 use crate::io::{Result, Write};
 use crate::mem::{self, MaybeUninit};
+use crate::{cmp, ptr};
 
 /// A borrowed byte buffer which is incrementally filled and initialized.
 ///
@@ -250,8 +250,11 @@ pub unsafe fn advance(&mut self, n: usize) -> &mut Self {
     /// Initializes all bytes in the cursor.
     #[inline]
     pub fn ensure_init(&mut self) -> &mut Self {
-        for byte in self.uninit_mut() {
-            byte.write(0);
+        let uninit = self.uninit_mut();
+        // SAFETY: 0 is a valid value for MaybeUninit<u8> and the length matches the allocation
+        // since it is comes from a slice reference.
+        unsafe {
+            ptr::write_bytes(uninit.as_mut_ptr(), 0, uninit.len());
         }
         self.buf.init = self.buf.capacity();