1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
// Licensed to the Apache Software Foundation (ASF) under one // or more contributor license agreements. See the NOTICE file // distributed with this work for additional information // regarding copyright ownership. The ASF licenses this file // to you under the Apache License, Version 2.0 (the // "License"); you may not use this file except in compliance // with the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, // software distributed under the License is distributed on an // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations // under the License.. #![allow(missing_copy_implementations)] use crate::io::{self, BufRead, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Write}; use core::fmt; use core::mem::MaybeUninit; /// Copies the entire contents of a reader into a writer. /// /// This function will continuously read data from `reader` and then /// write it into `writer` in a streaming fashion until `reader` /// returns EOF. /// /// On success, the total number of bytes that were copied from /// `reader` to `writer` is returned. /// /// If you’re wanting to copy the contents of one file to another and you’re /// working with filesystem paths, see the [`fs::copy`] function. /// /// [`fs::copy`]: ../fs/fn.copy.html /// /// # Errors /// /// This function will return an error immediately if any call to `read` or /// `write` returns an error. All instances of `ErrorKind::Interrupted` are /// handled by this function and the underlying operation is retried. pub fn copy<R: ?Sized, W: ?Sized>(reader: &mut R, writer: &mut W) -> io::Result<u64> where R: Read, W: Write, { let mut buf = MaybeUninit::<[u8; super::DEFAULT_BUF_SIZE]>::uninit(); // FIXME(#53491): This is calling `get_mut` and `get_ref` on an uninitialized // `MaybeUninit`. Revisit this once we decided whether that is valid or not. // This is still technically undefined behavior due to creating a reference // to uninitialized data, but within libstd we can rely on more guarantees // than if this code were in an external lib. unsafe { reader.initializer().initialize(buf.assume_init_mut()); } let mut written = 0; loop { let len = match reader.read(unsafe { buf.assume_init_mut() }) { Ok(0) => return Ok(written), Ok(len) => len, Err(ref e) if e.kind() == ErrorKind::Interrupted => continue, Err(e) => return Err(e), }; writer.write_all(unsafe { &buf.assume_init_ref()[..len] })?; written += len as u64; } } /// A reader which is always at EOF. /// /// This struct is generally created by calling [`empty`]. Please see /// the documentation of [`empty()`][`empty`] for more details. /// /// [`empty`]: fn.empty.html pub struct Empty { _priv: (), } /// Constructs a new handle to an empty reader. /// /// All reads from the returned reader will return [`Ok`]`(0)`. /// /// [`Ok`]: ../result/enum.Result.html#variant.Ok /// pub fn empty() -> Empty { Empty { _priv: () } } impl Read for Empty { #[inline] fn read(&mut self, _buf: &mut [u8]) -> io::Result<usize> { Ok(0) } #[inline] unsafe fn initializer(&self) -> Initializer { Initializer::nop() } } impl BufRead for Empty { #[inline] fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(&[]) } #[inline] fn consume(&mut self, _n: usize) {} } impl fmt::Debug for Empty { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Empty { .. }") } } /// A reader which yields one byte over and over and over and over and over and... /// /// This struct is generally created by calling [`repeat`][repeat]. Please /// see the documentation of `repeat()` for more details. /// /// [repeat]: fn.repeat.html pub struct Repeat { byte: u8, } /// Creates an instance of a reader that infinitely repeats one byte. /// /// All reads from this reader will succeed by filling the specified buffer with /// the given byte. pub fn repeat(byte: u8) -> Repeat { Repeat { byte } } impl Read for Repeat { #[inline] fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { for slot in &mut *buf { *slot = self.byte; } Ok(buf.len()) } #[inline] fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { let mut nwritten = 0; for buf in bufs { nwritten += self.read(buf)?; } Ok(nwritten) } #[inline] unsafe fn initializer(&self) -> Initializer { Initializer::nop() } } impl fmt::Debug for Repeat { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Repeat { .. }") } } /// A writer which will move data into the void. /// /// This struct is generally created by calling [`sink`][sink]. Please /// see the documentation of `sink()` for more details. /// /// [sink]: fn.sink.html pub struct Sink { _priv: (), } /// Creates an instance of a writer which will successfully consume all data. /// /// All calls to `write` on the returned instance will return `Ok(buf.len())` /// and the contents of the buffer will not be inspected. pub fn sink() -> Sink { Sink { _priv: () } } impl Write for Sink { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result<usize> { Ok(buf.len()) } #[inline] fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { let total_len = bufs.iter().map(|b| b.len()).sum(); Ok(total_len) } #[inline] fn flush(&mut self) -> io::Result<()> { Ok(()) } } impl fmt::Debug for Sink { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.pad("Sink { .. }") } }