Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make std::io::copy take Read and Write by value instead of by reference #111063

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 13 additions & 10 deletions library/std/src/io/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::mem::MaybeUninit;
///
/// [changes]: crate::io#platform-specific-behavior
#[stable(feature = "rust1", since = "1.0.0")]
pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
pub fn copy<R, W>(reader: R, writer: W) -> Result<u64>
where
R: Read,
W: Write,
Expand All @@ -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<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
pub(crate) fn generic_copy<R, W>(reader: R, writer: W) -> Result<u64>
where
R: Read,
W: Write,
Expand All @@ -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<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
fn copy_to<R: Read>(reader: R, writer: Self) -> Result<u64>;
}

impl<W: Write + ?Sized> BufferedCopySpec for W {
default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
impl<W: Write> BufferedCopySpec for W {
default fn copy_to<R: Read>(reader: R, writer: Self) -> Result<u64> {
stack_buffer_copy(reader, writer)
}
}

impl<I: Write> BufferedCopySpec for BufWriter<I> {
fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
fn copy_to<R: Read>(reader: R, mut writer: Self) -> Result<u64> {
BufferedCopySpec::copy_to(reader, &mut writer)
}
}

impl<I: Write> BufferedCopySpec for &mut BufWriter<I> {
fn copy_to<R: Read>(mut reader: R, writer: Self) -> Result<u64> {
if writer.capacity() < DEFAULT_BUF_SIZE {
return stack_buffer_copy(reader, writer);
}
Expand Down Expand Up @@ -134,10 +140,7 @@ impl<I: Write> BufferedCopySpec for BufWriter<I> {
}
}

fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
reader: &mut R,
writer: &mut W,
) -> Result<u64> {
fn stack_buffer_copy<R: Read, W: Write>(mut reader: R, mut writer: W) -> Result<u64> {
let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
let mut buf: BorrowedBuf<'_> = buf.into();

Expand Down
25 changes: 11 additions & 14 deletions library/std/src/sys/unix/kernel_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ use libc::{EBADF, EINVAL, ENOSYS, EOPNOTSUPP, EOVERFLOW, EPERM, EXDEV};
#[cfg(test)]
mod tests;

pub(crate) fn copy_spec<R: Read + ?Sized, W: Write + ?Sized>(
read: &mut R,
write: &mut W,
) -> Result<u64> {
pub(crate) fn copy_spec<R: Read, W: Write>(read: R, write: W) -> Result<u64> {
let copier = Copier { read, write };
SpecCopy::copy(copier)
}
Expand Down Expand Up @@ -160,30 +157,30 @@ fn safe_kernel_copy(source: &FdMeta, sink: &FdMeta) -> bool {

struct CopyParams(FdMeta, Option<RawFd>);

struct Copier<'a, 'b, R: Read + ?Sized, W: Write + ?Sized> {
read: &'a mut R,
write: &'b mut W,
struct Copier<R: Read, W: Write> {
read: R,
write: W,
}

trait SpecCopy {
fn copy(self) -> Result<u64>;
}

impl<R: Read + ?Sized, W: Write + ?Sized> SpecCopy for Copier<'_, '_, R, W> {
impl<R: Read, W: Write> SpecCopy for Copier<R, W> {
default fn copy(self) -> Result<u64> {
generic_copy(self.read, self.write)
}
}

impl<R: CopyRead, W: CopyWrite> SpecCopy for Copier<'_, '_, R, W> {
impl<R: CopyRead, W: CopyWrite> SpecCopy for Copier<R, W> {
fn copy(self) -> Result<u64> {
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<u64> {
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)
Expand All @@ -199,7 +196,7 @@ impl<R: CopyRead, W: CopyWrite> 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),
Expand All @@ -216,7 +213,7 @@ impl<R: CopyRead, W: CopyWrite> 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),
Expand All @@ -229,7 +226,7 @@ impl<R: CopyRead, W: CopyWrite> 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),
Expand Down
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 2 files
+4 −6 Cargo.lock
+4 −0 Cargo.toml