summary refs log tree commit diff
path: root/library
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-14 16:23:32 +0100
committerGitHub <noreply@github.com>2025-02-14 16:23:32 +0100
commitb5fce2ab8e8d08d881df3dec7a13ab174e0762d2 (patch)
tree7ed8133b9268e4cb1277e888a46a1a9a4a1f1d65 /library
parent49fb61c4961bb0244abe708d24a5e206a96f5304 (diff)
parent1a3efd27abc25372930fef847ff2d2027f5e821a (diff)
downloadrust-b5fce2ab8e8d08d881df3dec7a13ab174e0762d2.tar.gz
rust-b5fce2ab8e8d08d881df3dec7a13ab174e0762d2.zip
Rollup merge of #136967 - DaniPopes:io-repeat-fill, r=joboet
Use `slice::fill` in `io::Repeat` implementation

Use the existing `fill` methods on slices instead of manually writing the fill loop.
Diffstat (limited to 'library')
-rw-r--r--library/std/src/io/util.rs28
-rw-r--r--library/std/src/lib.rs1
2 files changed, 11 insertions, 18 deletions
diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs
index 424f862090f..cb3f864fd4e 100644
--- a/library/std/src/io/util.rs
+++ b/library/std/src/io/util.rs
@@ -7,6 +7,7 @@ use crate::fmt;
 use crate::io::{
     self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
 };
+use crate::mem::MaybeUninit;
 
 /// `Empty` ignores any data written via [`Write`], and will always be empty
 /// (returning zero bytes) when read via [`Read`].
@@ -182,35 +183,26 @@ pub const fn repeat(byte: u8) -> Repeat {
 impl Read for Repeat {
     #[inline]
     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
-        for slot in &mut *buf {
-            *slot = self.byte;
-        }
+        buf.fill(self.byte);
         Ok(buf.len())
     }
 
+    #[inline]
     fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
-        for slot in &mut *buf {
-            *slot = self.byte;
-        }
+        buf.fill(self.byte);
         Ok(())
     }
 
+    #[inline]
     fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
-        // SAFETY: No uninit bytes are being written
-        for slot in unsafe { buf.as_mut() } {
-            slot.write(self.byte);
-        }
-
-        let remaining = buf.capacity();
-
-        // SAFETY: the entire unfilled portion of buf has been initialized
-        unsafe {
-            buf.advance_unchecked(remaining);
-        }
-
+        // SAFETY: No uninit bytes are being written.
+        MaybeUninit::fill(unsafe { buf.as_mut() }, self.byte);
+        // SAFETY: the entire unfilled portion of buf has been initialized.
+        unsafe { buf.advance_unchecked(buf.capacity()) };
         Ok(())
     }
 
+    #[inline]
     fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
         self.read_buf(buf)
     }
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index 7d7d192c4dd..750116c6269 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -302,6 +302,7 @@
 #![feature(link_cfg)]
 #![feature(linkage)]
 #![feature(macro_metavar_expr_concat)]
+#![feature(maybe_uninit_fill)]
 #![feature(min_specialization)]
 #![feature(must_not_suspend)]
 #![feature(needs_panic_runtime)]