1 #[cfg(test)] 2 mod tests; 3 4 use crate::alloc::Allocator; 5 use crate::cmp; 6 use crate::fmt; 7 use crate::io::{ 8 self, BufRead, Error, ErrorKind, Initializer, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write, 9 }; 10 use crate::mem; 11 12 // ============================================================================= 13 // Forwarding implementations 14 15 #[stable(feature = "rust1", since = "1.0.0")] 16 impl<R: Read + ?Sized> Read for &mut R { 17 #[inline] read(&mut self, buf: &mut [u8]) -> io::Result<usize>18 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 19 (**self).read(buf) 20 } 21 22 #[inline] read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize>23 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { 24 (**self).read_vectored(bufs) 25 } 26 27 #[inline] is_read_vectored(&self) -> bool28 fn is_read_vectored(&self) -> bool { 29 (**self).is_read_vectored() 30 } 31 32 #[inline] initializer(&self) -> Initializer33 unsafe fn initializer(&self) -> Initializer { 34 (**self).initializer() 35 } 36 37 #[inline] read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>38 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 39 (**self).read_to_end(buf) 40 } 41 42 #[inline] read_to_string(&mut self, buf: &mut String) -> io::Result<usize>43 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { 44 (**self).read_to_string(buf) 45 } 46 47 #[inline] read_exact(&mut self, buf: &mut [u8]) -> io::Result<()>48 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 49 (**self).read_exact(buf) 50 } 51 } 52 #[stable(feature = "rust1", since = "1.0.0")] 53 impl<W: Write + ?Sized> Write for &mut W { 54 #[inline] write(&mut self, buf: &[u8]) -> io::Result<usize>55 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 56 (**self).write(buf) 57 } 58 59 #[inline] write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>60 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { 61 (**self).write_vectored(bufs) 62 } 63 64 #[inline] is_write_vectored(&self) -> bool65 fn is_write_vectored(&self) -> bool { 66 (**self).is_write_vectored() 67 } 68 69 #[inline] flush(&mut self) -> io::Result<()>70 fn flush(&mut self) -> io::Result<()> { 71 (**self).flush() 72 } 73 74 #[inline] write_all(&mut self, buf: &[u8]) -> io::Result<()>75 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 76 (**self).write_all(buf) 77 } 78 79 #[inline] write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()>80 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { 81 (**self).write_fmt(fmt) 82 } 83 } 84 #[stable(feature = "rust1", since = "1.0.0")] 85 impl<S: Seek + ?Sized> Seek for &mut S { 86 #[inline] seek(&mut self, pos: SeekFrom) -> io::Result<u64>87 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { 88 (**self).seek(pos) 89 } 90 91 #[inline] stream_position(&mut self) -> io::Result<u64>92 fn stream_position(&mut self) -> io::Result<u64> { 93 (**self).stream_position() 94 } 95 } 96 #[stable(feature = "rust1", since = "1.0.0")] 97 impl<B: BufRead + ?Sized> BufRead for &mut B { 98 #[inline] fill_buf(&mut self) -> io::Result<&[u8]>99 fn fill_buf(&mut self) -> io::Result<&[u8]> { 100 (**self).fill_buf() 101 } 102 103 #[inline] consume(&mut self, amt: usize)104 fn consume(&mut self, amt: usize) { 105 (**self).consume(amt) 106 } 107 108 #[inline] read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize>109 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { 110 (**self).read_until(byte, buf) 111 } 112 113 #[inline] read_line(&mut self, buf: &mut String) -> io::Result<usize>114 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { 115 (**self).read_line(buf) 116 } 117 } 118 119 #[stable(feature = "rust1", since = "1.0.0")] 120 impl<R: Read + ?Sized> Read for Box<R> { 121 #[inline] read(&mut self, buf: &mut [u8]) -> io::Result<usize>122 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 123 (**self).read(buf) 124 } 125 126 #[inline] read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize>127 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { 128 (**self).read_vectored(bufs) 129 } 130 131 #[inline] is_read_vectored(&self) -> bool132 fn is_read_vectored(&self) -> bool { 133 (**self).is_read_vectored() 134 } 135 136 #[inline] initializer(&self) -> Initializer137 unsafe fn initializer(&self) -> Initializer { 138 (**self).initializer() 139 } 140 141 #[inline] read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>142 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 143 (**self).read_to_end(buf) 144 } 145 146 #[inline] read_to_string(&mut self, buf: &mut String) -> io::Result<usize>147 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { 148 (**self).read_to_string(buf) 149 } 150 151 #[inline] read_exact(&mut self, buf: &mut [u8]) -> io::Result<()>152 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 153 (**self).read_exact(buf) 154 } 155 } 156 #[stable(feature = "rust1", since = "1.0.0")] 157 impl<W: Write + ?Sized> Write for Box<W> { 158 #[inline] write(&mut self, buf: &[u8]) -> io::Result<usize>159 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 160 (**self).write(buf) 161 } 162 163 #[inline] write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>164 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { 165 (**self).write_vectored(bufs) 166 } 167 168 #[inline] is_write_vectored(&self) -> bool169 fn is_write_vectored(&self) -> bool { 170 (**self).is_write_vectored() 171 } 172 173 #[inline] flush(&mut self) -> io::Result<()>174 fn flush(&mut self) -> io::Result<()> { 175 (**self).flush() 176 } 177 178 #[inline] write_all(&mut self, buf: &[u8]) -> io::Result<()>179 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 180 (**self).write_all(buf) 181 } 182 183 #[inline] write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()>184 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { 185 (**self).write_fmt(fmt) 186 } 187 } 188 #[stable(feature = "rust1", since = "1.0.0")] 189 impl<S: Seek + ?Sized> Seek for Box<S> { 190 #[inline] seek(&mut self, pos: SeekFrom) -> io::Result<u64>191 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { 192 (**self).seek(pos) 193 } 194 195 #[inline] stream_position(&mut self) -> io::Result<u64>196 fn stream_position(&mut self) -> io::Result<u64> { 197 (**self).stream_position() 198 } 199 } 200 #[stable(feature = "rust1", since = "1.0.0")] 201 impl<B: BufRead + ?Sized> BufRead for Box<B> { 202 #[inline] fill_buf(&mut self) -> io::Result<&[u8]>203 fn fill_buf(&mut self) -> io::Result<&[u8]> { 204 (**self).fill_buf() 205 } 206 207 #[inline] consume(&mut self, amt: usize)208 fn consume(&mut self, amt: usize) { 209 (**self).consume(amt) 210 } 211 212 #[inline] read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize>213 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { 214 (**self).read_until(byte, buf) 215 } 216 217 #[inline] read_line(&mut self, buf: &mut String) -> io::Result<usize>218 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { 219 (**self).read_line(buf) 220 } 221 } 222 223 // ============================================================================= 224 // In-memory buffer implementations 225 226 /// Read is implemented for `&[u8]` by copying from the slice. 227 /// 228 /// Note that reading updates the slice to point to the yet unread part. 229 /// The slice will be empty when EOF is reached. 230 #[stable(feature = "rust1", since = "1.0.0")] 231 impl Read for &[u8] { 232 #[inline] read(&mut self, buf: &mut [u8]) -> io::Result<usize>233 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 234 let amt = cmp::min(buf.len(), self.len()); 235 let (a, b) = self.split_at(amt); 236 237 // First check if the amount of bytes we want to read is small: 238 // `copy_from_slice` will generally expand to a call to `memcpy`, and 239 // for a single byte the overhead is significant. 240 if amt == 1 { 241 buf[0] = a[0]; 242 } else { 243 buf[..amt].copy_from_slice(a); 244 } 245 246 *self = b; 247 Ok(amt) 248 } 249 250 #[inline] read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize>251 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { 252 let mut nread = 0; 253 for buf in bufs { 254 nread += self.read(buf)?; 255 if self.is_empty() { 256 break; 257 } 258 } 259 260 Ok(nread) 261 } 262 263 #[inline] is_read_vectored(&self) -> bool264 fn is_read_vectored(&self) -> bool { 265 true 266 } 267 268 #[inline] initializer(&self) -> Initializer269 unsafe fn initializer(&self) -> Initializer { 270 Initializer::nop() 271 } 272 273 #[inline] read_exact(&mut self, buf: &mut [u8]) -> io::Result<()>274 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { 275 if buf.len() > self.len() { 276 return Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer")); 277 } 278 let (a, b) = self.split_at(buf.len()); 279 280 // First check if the amount of bytes we want to read is small: 281 // `copy_from_slice` will generally expand to a call to `memcpy`, and 282 // for a single byte the overhead is significant. 283 if buf.len() == 1 { 284 buf[0] = a[0]; 285 } else { 286 buf.copy_from_slice(a); 287 } 288 289 *self = b; 290 Ok(()) 291 } 292 293 #[inline] read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize>294 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { 295 buf.extend_from_slice(*self); 296 let len = self.len(); 297 *self = &self[len..]; 298 Ok(len) 299 } 300 } 301 302 #[stable(feature = "rust1", since = "1.0.0")] 303 impl BufRead for &[u8] { 304 #[inline] fill_buf(&mut self) -> io::Result<&[u8]>305 fn fill_buf(&mut self) -> io::Result<&[u8]> { 306 Ok(*self) 307 } 308 309 #[inline] consume(&mut self, amt: usize)310 fn consume(&mut self, amt: usize) { 311 *self = &self[amt..]; 312 } 313 } 314 315 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting 316 /// its data. 317 /// 318 /// Note that writing updates the slice to point to the yet unwritten part. 319 /// The slice will be empty when it has been completely overwritten. 320 /// 321 /// If the number of bytes to be written exceeds the size of the slice, write operations will 322 /// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of 323 /// kind `ErrorKind::WriteZero`. 324 #[stable(feature = "rust1", since = "1.0.0")] 325 impl Write for &mut [u8] { 326 #[inline] write(&mut self, data: &[u8]) -> io::Result<usize>327 fn write(&mut self, data: &[u8]) -> io::Result<usize> { 328 let amt = cmp::min(data.len(), self.len()); 329 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt); 330 a.copy_from_slice(&data[..amt]); 331 *self = b; 332 Ok(amt) 333 } 334 335 #[inline] write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>336 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { 337 let mut nwritten = 0; 338 for buf in bufs { 339 nwritten += self.write(buf)?; 340 if self.is_empty() { 341 break; 342 } 343 } 344 345 Ok(nwritten) 346 } 347 348 #[inline] is_write_vectored(&self) -> bool349 fn is_write_vectored(&self) -> bool { 350 true 351 } 352 353 #[inline] write_all(&mut self, data: &[u8]) -> io::Result<()>354 fn write_all(&mut self, data: &[u8]) -> io::Result<()> { 355 if self.write(data)? == data.len() { 356 Ok(()) 357 } else { 358 Err(Error::new_const(ErrorKind::WriteZero, &"failed to write whole buffer")) 359 } 360 } 361 362 #[inline] flush(&mut self) -> io::Result<()>363 fn flush(&mut self) -> io::Result<()> { 364 Ok(()) 365 } 366 } 367 368 /// Write is implemented for `Vec<u8>` by appending to the vector. 369 /// The vector will grow as needed. 370 #[stable(feature = "rust1", since = "1.0.0")] 371 impl<A: Allocator> Write for Vec<u8, A> { 372 #[inline] write(&mut self, buf: &[u8]) -> io::Result<usize>373 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { 374 self.extend_from_slice(buf); 375 Ok(buf.len()) 376 } 377 378 #[inline] write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize>379 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { 380 let len = bufs.iter().map(|b| b.len()).sum(); 381 self.reserve(len); 382 for buf in bufs { 383 self.extend_from_slice(buf); 384 } 385 Ok(len) 386 } 387 388 #[inline] is_write_vectored(&self) -> bool389 fn is_write_vectored(&self) -> bool { 390 true 391 } 392 393 #[inline] write_all(&mut self, buf: &[u8]) -> io::Result<()>394 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { 395 self.extend_from_slice(buf); 396 Ok(()) 397 } 398 399 #[inline] flush(&mut self) -> io::Result<()>400 fn flush(&mut self) -> io::Result<()> { 401 Ok(()) 402 } 403 } 404