Skip to content

Commit 757fd7a

Browse files
committed
Use slice::fill in io::Repeat implementation
Use the existing `fill` methods on slices instead of manually writing the fill loop.
1 parent cfe9ffc commit 757fd7a

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

library/std/src/io/util.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -182,35 +182,26 @@ pub const fn repeat(byte: u8) -> Repeat {
182182
impl Read for Repeat {
183183
#[inline]
184184
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
185-
for slot in &mut *buf {
186-
*slot = self.byte;
187-
}
185+
buf.fill(self.byte);
188186
Ok(buf.len())
189187
}
190188

189+
#[inline]
191190
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
192-
for slot in &mut *buf {
193-
*slot = self.byte;
194-
}
191+
buf.fill(self.byte);
195192
Ok(())
196193
}
197194

195+
#[inline]
198196
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
199-
// SAFETY: No uninit bytes are being written
200-
for slot in unsafe { buf.as_mut() } {
201-
slot.write(self.byte);
202-
}
203-
204-
let remaining = buf.capacity();
205-
206-
// SAFETY: the entire unfilled portion of buf has been initialized
207-
unsafe {
208-
buf.advance_unchecked(remaining);
209-
}
210-
197+
// SAFETY: No uninit bytes are being written.
198+
crate::mem::MaybeUninit::fill(unsafe { buf.as_mut() }, self.byte);
199+
// SAFETY: the entire unfilled portion of buf has been initialized.
200+
unsafe { buf.advance_unchecked(buf.capacity()) };
211201
Ok(())
212202
}
213203

204+
#[inline]
214205
fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
215206
self.read_buf(buf)
216207
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@
302302
#![feature(link_cfg)]
303303
#![feature(linkage)]
304304
#![feature(macro_metavar_expr_concat)]
305+
#![feature(maybe_uninit_fill)]
305306
#![feature(min_specialization)]
306307
#![feature(must_not_suspend)]
307308
#![feature(needs_panic_runtime)]

0 commit comments

Comments
 (0)