copy.rs - source
Description: Source of the Rust file `library/std/src/io/copy.rs`.

#
std/io/
copy.rs

    1use super::{BorrowedBuf, BufReader, BufWriter, DEFAULT_BUF_SIZE, Read, Result, Write};
    2use crate::alloc::Allocator;
    3use crate::cmp;
    4use crate::collections::VecDeque;
    5use crate::io::IoSlice;
    6use crate::mem::MaybeUninit;
    7use crate::sys::io::{CopyState, kernel_copy};
    8
    9#[cfg(test)]
    10mod tests;
    11
    12/// Copies the entire contents of a reader into a writer.
    13///
    14/// This function will continuously read data from `reader` and then
    15/// write it into `writer` in a streaming fashion until `reader`
    16/// returns EOF.
    17///
    18/// On success, the total number of bytes that were copied from
    19/// `reader` to `writer` is returned.
    20///
    21/// If you want to copy the contents of one file to another and you’re
    22/// working with filesystem paths, see the [`fs::copy`] function.
    23///
    24/// [`fs::copy`]: crate::fs::copy
    25///
    26/// # Errors
    27///
    28/// This function will return an error immediately if any call to [`read`] or
    29/// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
    30/// handled by this function and the underlying operation is retried.
    31///
    32/// [`read`]: Read::read
    33/// [`write`]: Write::write
    34/// [`ErrorKind::Interrupted`]: crate::io::ErrorKind::Interrupted
    35///
    36/// # Examples
    37///
    38/// ```
    39/// use std::io;
    40///
    41/// fn main() -> io::Result<()> {
    42///     let mut reader: &[u8] = b"hello";
    43///     let mut writer: Vec<u8> = vec![];
    44///
    45///     io::copy(&mut reader, &mut writer)?;
    46///
    47///     assert_eq!(&b"hello"[..], &writer[..]);
    48///     Ok(())
    49/// }
    50/// ```
    51///
    52/// # Platform-specific behavior
    53///
    54/// On Linux (including Android), this function uses `copy_file_range(2)`,
    55/// `sendfile(2)` or `splice(2)` syscalls to move data directly between file
    56/// descriptors if possible.
    57///
    58/// Note that platform-specific behavior [may change in the future][changes].
    59///
    60/// [changes]: crate::io#platform-specific-behavior
    61#[stable(feature = "rust1", since = "1.0.0")]
    62pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
    63where
    64    R: Read,
    65    W: Write,
    66{
    67    match kernel_copy(reader, writer)? {
    68        CopyState::Ended(copied) => Ok(copied),
    69        CopyState::Fallback(copied) => {
    70            generic_copy(reader, writer).map(|additional| copied + additional)
    71        }
    72    }
    73}
    74
    75/// The userspace read-write-loop implementation of `io::copy` that is used when
    76/// OS-specific specializations for copy offloading are not available or not applicable.
    77fn generic_copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
    78where
    79    R: Read,
    80    W: Write,
    81{
    82    let read_buf = BufferedReaderSpec::buffer_size(reader);
    83    let write_buf = BufferedWriterSpec::buffer_size(writer);
    84
    85    if read_buf >= DEFAULT_BUF_SIZE && read_buf >= write_buf {
    86        return BufferedReaderSpec::copy_to(reader, writer);
    87    }
    88
    89    BufferedWriterSpec::copy_from(writer, reader)
    90}
    91
    92/// Specialization of the read-write loop that reuses the internal
    93/// buffer of a BufReader. If there's no buffer then the writer side
    94/// should be used instead.
    95trait BufferedReaderSpec {
    96    fn buffer_size(&self) -> usize;
    97
    98    fn copy_to(&mut self, to: &mut (impl Write + ?Sized)) -> Result<u64>;
    99}
    100
    101impl<T> BufferedReaderSpec for T
    102where
    103    Self: Read,
    104    T: ?Sized,
    105{
    106    #[inline]
    107    default fn buffer_size(&self) -> usize {
    108        0
    109    }
    110
    111    default fn copy_to(&mut self, _to: &mut (impl Write + ?Sized)) -> Result<u64> {
    112        unreachable!("only called from specializations")
    113    }
    114}
    115
    116impl BufferedReaderSpec for &[u8] {
    117    fn buffer_size(&self) -> usize {
    118        // prefer this specialization since the source "buffer" is all we'll ever need,
    119        // even if it's small
    120        usize::MAX
    121    }
    122
    123    fn copy_to(&mut self, to: &mut (impl Write + ?Sized)) -> Result<u64> {
    124        let len = self.len();
    125        to.write_all(self)?;
    126        *self = &self[len..];
    127        Ok(len as u64)
    128    }
    129}
    130
    131impl<A: Allocator> BufferedReaderSpec for VecDeque<u8, A> {
    132    fn buffer_size(&self) -> usize {
    133        // prefer this specialization since the source "buffer" is all we'll ever need,
    134        // even if it's small
    135        usize::MAX
    136    }
    137
    138    fn copy_to(&mut self, to: &mut (impl Write + ?Sized)) -> Result<u64> {
    139        let len = self.len();
    140        let (front, back) = self.as_slices();
    141        let bufs = &mut [IoSlice::new(front), IoSlice::new(back)];
    142        to.write_all_vectored(bufs)?;
    143        self.clear();
    144        Ok(len as u64)
    145    }
    146}
    147
    148impl<I> BufferedReaderSpec for BufReader<I>
    149where
    150    Self: Read,
    151    I: ?Sized,
    152{
    153    fn buffer_size(&self) -> usize {
    154        self.capacity()
    155    }
    156
    157    fn copy_to(&mut self, to: &mut (impl Write + ?Sized)) -> Result<u64> {
    158        let mut len = 0;
    159
    160        loop {
    161            // Hack: this relies on `impl Read for BufReader` always calling fill_buf
    162            // if the buffer is empty, even for empty slices.
    163            // It can't be called directly here since specialization prevents us
    164            // from adding I: Read
    165            match self.read(&mut []) {
    166                Ok(_) => {}
    167                Err(e) if e.is_interrupted() => continue,
    168                Err(e) => return Err(e),
    169            }
    170            let buf = self.buffer();
    171            if self.buffer().len() == 0 {
    172                return Ok(len);
    173            }
    174
    175            // In case the writer side is a BufWriter then its write_all
    176            // implements an optimization that passes through large
    177            // buffers to the underlying writer. That code path is #[cold]
    178            // but we're still avoiding redundant memcopies when doing
    179            // a copy between buffered inputs and outputs.
    180            to.write_all(buf)?;
    181            len += buf.len() as u64;
    182            self.discard_buffer();
    183        }
    184    }
    185}
    186
    187/// Specialization of the read-write loop that either uses a stack buffer
    188/// or reuses the internal buffer of a BufWriter
    189trait BufferedWriterSpec: Write {
    190    fn buffer_size(&self) -> usize;
    191
    192    fn copy_from<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<u64>;
    193}
    194
    195impl<W: Write + ?Sized> BufferedWriterSpec for W {
    196    #[inline]
    197    default fn buffer_size(&self) -> usize {
    198        0
    199    }
    200
    201    default fn copy_from<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<u64> {
    202        stack_buffer_copy(reader, self)
    203    }
    204}
    205
    206impl<I: Write + ?Sized> BufferedWriterSpec for BufWriter<I> {
    207    fn buffer_size(&self) -> usize {
    208        self.capacity()
    209    }
    210
    211    fn copy_from<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<u64> {
    212        if self.capacity() < DEFAULT_BUF_SIZE {
    213            return stack_buffer_copy(reader, self);
    214        }
    215
    216        let mut len = 0;
    217        let mut init = 0;
    218
    219        loop {
    220            let buf = self.buffer_mut();
    221            let mut read_buf: BorrowedBuf<'_> = buf.spare_capacity_mut().into();
    222
    223            unsafe {
    224                // SAFETY: init is either 0 or the init_len from the previous iteration.
    225                read_buf.set_init(init);
    226            }
    227
    228            if read_buf.capacity() >= DEFAULT_BUF_SIZE {
    229                let mut cursor = read_buf.unfilled();
    230                match reader.read_buf(cursor.reborrow()) {
    231                    Ok(()) => {
    232                        let bytes_read = cursor.written();
    233
    234                        if bytes_read == 0 {
    235                            return Ok(len);
    236                        }
    237
    238                        init = read_buf.init_len() - bytes_read;
    239                        len += bytes_read as u64;
    240
    241                        // SAFETY: BorrowedBuf guarantees all of its filled bytes are init
    242                        unsafe { buf.set_len(buf.len() + bytes_read) };
    243
    244                        // Read again if the buffer still has enough capacity, as BufWriter itself would do
    245                        // This will occur if the reader returns short reads
    246                    }
    247                    Err(ref e) if e.is_interrupted() => {}
    248                    Err(e) => return Err(e),
    249                }
    250            } else {
    251                // All the bytes that were already in the buffer are initialized,
    252                // treat them as such when the buffer is flushed.
    253                init += buf.len();
    254
    255                self.flush_buf()?;
    256            }
    257        }
    258    }
    259}
    260
    261impl BufferedWriterSpec for Vec<u8> {
    262    fn buffer_size(&self) -> usize {
    263        cmp::max(DEFAULT_BUF_SIZE, self.capacity() - self.len())
    264    }
    265
    266    fn copy_from<R: Read + ?Sized>(&mut self, reader: &mut R) -> Result<u64> {
    267        reader.read_to_end(self).map(|bytes| u64::try_from(bytes).expect("usize overflowed u64"))
    268    }
    269}
    270
    271fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
    272    reader: &mut R,
    273    writer: &mut W,
    274) -> Result<u64> {
    275    let buf: &mut [_] = &mut [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
    276    let mut buf: BorrowedBuf<'_> = buf.into();
    277
    278    let mut len = 0;
    279
    280    loop {
    281        match reader.read_buf(buf.unfilled()) {
    282            Ok(()) => {}
    283            Err(e) if e.is_interrupted() => continue,
    284            Err(e) => return Err(e),
    285        };
    286
    287        if buf.filled().is_empty() {
    288            break;
    289        }
    290
    291        len += buf.filled().len() as u64;
    292        writer.write_all(buf.filled())?;
    293        buf.clear();
    294    }
    295
    296    Ok(len)
    297}