mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
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.
This commit is contained in:
commit
b5fce2ab8e
@ -7,6 +7,7 @@ use crate::fmt;
|
|||||||
use crate::io::{
|
use crate::io::{
|
||||||
self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, SizeHint, Write,
|
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
|
/// `Empty` ignores any data written via [`Write`], and will always be empty
|
||||||
/// (returning zero bytes) when read via [`Read`].
|
/// (returning zero bytes) when read via [`Read`].
|
||||||
@ -182,35 +183,26 @@ pub const fn repeat(byte: u8) -> Repeat {
|
|||||||
impl Read for Repeat {
|
impl Read for Repeat {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
for slot in &mut *buf {
|
buf.fill(self.byte);
|
||||||
*slot = self.byte;
|
|
||||||
}
|
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
||||||
for slot in &mut *buf {
|
buf.fill(self.byte);
|
||||||
*slot = self.byte;
|
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
|
fn read_buf(&mut self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
|
||||||
// SAFETY: No uninit bytes are being written
|
// SAFETY: No uninit bytes are being written.
|
||||||
for slot in unsafe { buf.as_mut() } {
|
MaybeUninit::fill(unsafe { buf.as_mut() }, self.byte);
|
||||||
slot.write(self.byte);
|
// SAFETY: the entire unfilled portion of buf has been initialized.
|
||||||
}
|
unsafe { buf.advance_unchecked(buf.capacity()) };
|
||||||
|
|
||||||
let remaining = buf.capacity();
|
|
||||||
|
|
||||||
// SAFETY: the entire unfilled portion of buf has been initialized
|
|
||||||
unsafe {
|
|
||||||
buf.advance_unchecked(remaining);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
|
fn read_buf_exact(&mut self, buf: BorrowedCursor<'_>) -> io::Result<()> {
|
||||||
self.read_buf(buf)
|
self.read_buf(buf)
|
||||||
}
|
}
|
||||||
|
@ -302,6 +302,7 @@
|
|||||||
#![feature(link_cfg)]
|
#![feature(link_cfg)]
|
||||||
#![feature(linkage)]
|
#![feature(linkage)]
|
||||||
#![feature(macro_metavar_expr_concat)]
|
#![feature(macro_metavar_expr_concat)]
|
||||||
|
#![feature(maybe_uninit_fill)]
|
||||||
#![feature(min_specialization)]
|
#![feature(min_specialization)]
|
||||||
#![feature(must_not_suspend)]
|
#![feature(must_not_suspend)]
|
||||||
#![feature(needs_panic_runtime)]
|
#![feature(needs_panic_runtime)]
|
||||||
|
Loading…
Reference in New Issue
Block a user