From bf1a8dadafcf8c800ad66dd0956f701ecdbc8c8e Mon Sep 17 00:00:00 2001 From: est31 Date: Mon, 1 May 2023 15:51:15 +0200 Subject: [PATCH 1/2] Make std::io::copy take Read and Write by value instead of by reference The traits are also implemented on mutable references of types implementing that trait. --- library/std/src/io/copy.rs | 23 +++++++++++++---------- library/std/src/sys/unix/kernel_copy.rs | 25 +++++++++++-------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/library/std/src/io/copy.rs b/library/std/src/io/copy.rs index 38b98afffa168..e47482086dca1 100644 --- a/library/std/src/io/copy.rs +++ b/library/std/src/io/copy.rs @@ -50,7 +50,7 @@ use crate::mem::MaybeUninit; /// /// [changes]: crate::io#platform-specific-behavior #[stable(feature = "rust1", since = "1.0.0")] -pub fn copy(reader: &mut R, writer: &mut W) -> Result +pub fn copy(reader: R, writer: W) -> Result where R: Read, W: Write, @@ -66,7 +66,7 @@ where /// The userspace read-write-loop implementation of `io::copy` that is used when /// OS-specific specializations for copy offloading are not available or not applicable. -pub(crate) fn generic_copy(reader: &mut R, writer: &mut W) -> Result +pub(crate) fn generic_copy(reader: R, writer: W) -> Result where R: Read, W: Write, @@ -77,17 +77,23 @@ where /// Specialization of the read-write loop that either uses a stack buffer /// or reuses the internal buffer of a BufWriter trait BufferedCopySpec: Write { - fn copy_to(reader: &mut R, writer: &mut Self) -> Result; + fn copy_to(reader: R, writer: Self) -> Result; } -impl BufferedCopySpec for W { - default fn copy_to(reader: &mut R, writer: &mut Self) -> Result { +impl BufferedCopySpec for W { + default fn copy_to(reader: R, writer: Self) -> Result { stack_buffer_copy(reader, writer) } } impl BufferedCopySpec for BufWriter { - fn copy_to(reader: &mut R, writer: &mut Self) -> Result { + fn copy_to(reader: R, mut writer: Self) -> Result { + BufferedCopySpec::copy_to(reader, &mut writer) + } +} + +impl BufferedCopySpec for &mut BufWriter { + fn copy_to(mut reader: R, writer: Self) -> Result { if writer.capacity() < DEFAULT_BUF_SIZE { return stack_buffer_copy(reader, writer); } @@ -134,10 +140,7 @@ impl BufferedCopySpec for BufWriter { } } -fn stack_buffer_copy( - reader: &mut R, - writer: &mut W, -) -> Result { +fn stack_buffer_copy(mut reader: R, mut writer: W) -> Result { let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE]; let mut buf: BorrowedBuf<'_> = buf.into(); diff --git a/library/std/src/sys/unix/kernel_copy.rs b/library/std/src/sys/unix/kernel_copy.rs index 16c8e0c0ebfc5..a046823ff31d8 100644 --- a/library/std/src/sys/unix/kernel_copy.rs +++ b/library/std/src/sys/unix/kernel_copy.rs @@ -68,10 +68,7 @@ use libc::{EBADF, EINVAL, ENOSYS, EOPNOTSUPP, EOVERFLOW, EPERM, EXDEV}; #[cfg(test)] mod tests; -pub(crate) fn copy_spec( - read: &mut R, - write: &mut W, -) -> Result { +pub(crate) fn copy_spec(read: R, write: W) -> Result { let copier = Copier { read, write }; SpecCopy::copy(copier) } @@ -160,30 +157,30 @@ fn safe_kernel_copy(source: &FdMeta, sink: &FdMeta) -> bool { struct CopyParams(FdMeta, Option); -struct Copier<'a, 'b, R: Read + ?Sized, W: Write + ?Sized> { - read: &'a mut R, - write: &'b mut W, +struct Copier { + read: R, + write: W, } trait SpecCopy { fn copy(self) -> Result; } -impl SpecCopy for Copier<'_, '_, R, W> { +impl SpecCopy for Copier { default fn copy(self) -> Result { generic_copy(self.read, self.write) } } -impl SpecCopy for Copier<'_, '_, R, W> { +impl SpecCopy for Copier { fn copy(self) -> Result { - let (reader, writer) = (self.read, self.write); + let Copier { read: mut reader, write: mut writer } = self; let r_cfg = reader.properties(); let w_cfg = writer.properties(); // before direct operations on file descriptors ensure that all source and sink buffers are empty let mut flush = || -> crate::io::Result { - let bytes = reader.drain_to(writer, u64::MAX)?; + let bytes = reader.drain_to(&mut writer, u64::MAX)?; // BufWriter buffered bytes have already been accounted for in earlier write() calls writer.flush()?; Ok(bytes) @@ -199,7 +196,7 @@ impl SpecCopy for Copier<'_, '_, R, W> { if input_meta.copy_file_range_candidate() && output_meta.copy_file_range_candidate() { let result = copy_regular_files(readfd, writefd, max_write); - result.update_take(reader); + result.update_take(&mut reader); match result { CopyResult::Ended(bytes_copied) => return Ok(bytes_copied + written), @@ -216,7 +213,7 @@ impl SpecCopy for Copier<'_, '_, R, W> { if input_meta.potential_sendfile_source() && safe_kernel_copy(&input_meta, &output_meta) { let result = sendfile_splice(SpliceMode::Sendfile, readfd, writefd, max_write); - result.update_take(reader); + result.update_take(&mut reader); match result { CopyResult::Ended(bytes_copied) => return Ok(bytes_copied + written), @@ -229,7 +226,7 @@ impl SpecCopy for Copier<'_, '_, R, W> { && safe_kernel_copy(&input_meta, &output_meta) { let result = sendfile_splice(SpliceMode::Splice, readfd, writefd, max_write); - result.update_take(reader); + result.update_take(&mut reader); match result { CopyResult::Ended(bytes_copied) => return Ok(bytes_copied + written), From 81617fe3af71de6a0c4fe8b4ada46cc2b62ebe5e Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 2 May 2023 03:40:24 +0200 Subject: [PATCH 2/2] update cargo submodule for gix-core patch --- .gitmodules | 2 +- src/tools/cargo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitmodules b/.gitmodules index 4596ae17d0238..1ec20b312adc6 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,7 +3,7 @@ url = https://github.com/rust-lang/nomicon.git [submodule "src/tools/cargo"] path = src/tools/cargo - url = https://github.com/rust-lang/cargo.git + url = https://github.com/est31/cargo.git [submodule "src/doc/reference"] path = src/doc/reference url = https://github.com/rust-lang/reference.git diff --git a/src/tools/cargo b/src/tools/cargo index ac84010322a31..88654adcc85b5 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit ac84010322a31f4a581dafe26258aa4ac8dea9cd +Subproject commit 88654adcc85b58c047512e90ccf4151459cac8bf