Skip to content

Commit b5fce2a

Browse files
authored
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.
2 parents 49fb61c + 1a3efd2 commit b5fce2a

File tree

2 files changed

+11
-18
lines changed

2 files changed

+11
-18
lines changed

Diff for: library/std/src/io/util.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::fmt;
77
use crate::io::{
88
self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
99
};
10+
use crate::mem::MaybeUninit;
1011

1112
/// `Empty` ignores any data written via [`Write`], and will always be empty
1213
/// (returning zero bytes) when read via [`Read`].
@@ -182,35 +183,26 @@ pub const fn repeat(byte: u8) -> Repeat {
182183
impl Read for Repeat {
183184
#[inline]
184185
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
185-
for slot in &mut *buf {
186-
*slot = self.byte;
187-
}
186+
buf.fill(self.byte);
188187
Ok(buf.len())
189188
}
190189

190+
#[inline]
191191
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
192-
for slot in &mut *buf {
193-
*slot = self.byte;
194-
}
192+
buf.fill(self.byte);
195193
Ok(())
196194
}
197195

196+
#[inline]
198197
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-
198+
// SAFETY: No uninit bytes are being written.
199+
MaybeUninit::fill(unsafe { buf.as_mut() }, self.byte);
200+
// SAFETY: the entire unfilled portion of buf has been initialized.
201+
unsafe { buf.advance_unchecked(buf.capacity()) };
211202
Ok(())
212203
}
213204

205+
#[inline]
214206
fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
215207
self.read_buf(buf)
216208
}

Diff for: 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)