1 use super::{BufWriter, ErrorKind, Read, ReadBuf, Result, Write, DEFAULT_BUF_SIZE};
2 use crate::mem::MaybeUninit;
4 /// Copies the entire contents of a reader into a writer.
6 /// This function will continuously read data from `reader` and then
7 /// write it into `writer` in a streaming fashion until `reader`
10 /// On success, the total number of bytes that were copied from
11 /// `reader` to `writer` is returned.
13 /// If you’re wanting to copy the contents of one file to another and you’re
14 /// working with filesystem paths, see the [`fs::copy`] function.
16 /// [`fs::copy`]: crate::fs::copy
20 /// This function will return an error immediately if any call to [`read`] or
21 /// [`write`] returns an error. All instances of [`ErrorKind::Interrupted`] are
22 /// handled by this function and the underlying operation is retried.
24 /// [`read`]: Read::read
25 /// [`write`]: Write::write
32 /// fn main() -> io::Result<()> {
33 /// let mut reader: &[u8] = b"hello";
34 /// let mut writer: Vec<u8> = vec![];
36 /// io::copy(&mut reader, &mut writer)?;
38 /// assert_eq!(&b"hello"[..], &writer[..]);
42 #[stable(feature = "rust1", since = "1.0.0")]
43 pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
49 if #[cfg(any(target_os = "linux", target_os = "android"))] {
50 crate::sys::kernel_copy::copy_spec(reader, writer)
52 generic_copy(reader, writer)
57 /// The userspace read-write-loop implementation of `io::copy` that is used when
58 /// OS-specific specializations for copy offloading are not available or not applicable.
59 pub(crate) fn generic_copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> Result<u64>
64 BufferedCopySpec::copy_to(reader, writer)
67 /// Specialization of the read-write loop that either uses a stack buffer
68 /// or reuses the internal buffer of a BufWriter
69 trait BufferedCopySpec: Write {
70 fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64>;
73 impl<W: Write + ?Sized> BufferedCopySpec for W {
74 default fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
75 stack_buffer_copy(reader, writer)
79 impl<I: Write> BufferedCopySpec for BufWriter<I> {
80 fn copy_to<R: Read + ?Sized>(reader: &mut R, writer: &mut Self) -> Result<u64> {
81 if writer.capacity() < DEFAULT_BUF_SIZE {
82 return stack_buffer_copy(reader, writer);
89 let buf = writer.buffer_mut();
90 let mut read_buf = ReadBuf::uninit(buf.spare_capacity_mut());
92 // SAFETY: init is either 0 or the initialized_len of the previous iteration
94 read_buf.assume_init(init);
97 if read_buf.capacity() >= DEFAULT_BUF_SIZE {
98 match reader.read_buf(&mut read_buf) {
100 let bytes_read = read_buf.filled_len();
106 init = read_buf.initialized_len() - bytes_read;
108 // SAFETY: ReadBuf guarantees all of its filled bytes are init
109 unsafe { buf.set_len(buf.len() + bytes_read) };
110 len += bytes_read as u64;
111 // Read again if the buffer still has enough capacity, as BufWriter itself would do
112 // This will occur if the reader returns short reads
115 Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
116 Err(e) => return Err(e),
125 fn stack_buffer_copy<R: Read + ?Sized, W: Write + ?Sized>(
129 let mut buf = [MaybeUninit::uninit(); DEFAULT_BUF_SIZE];
130 let mut buf = ReadBuf::uninit(&mut buf);
135 match reader.read_buf(&mut buf) {
137 Err(e) if e.kind() == ErrorKind::Interrupted => continue,
138 Err(e) => return Err(e),
141 if buf.filled().is_empty() {
145 len += buf.filled().len() as u64;
146 writer.write_all(buf.filled())?;