1 | #[cfg (test)] |
2 | mod tests; |
3 | |
4 | use crate::alloc::Allocator; |
5 | use crate::cmp; |
6 | use crate::collections::VecDeque; |
7 | use crate::fmt; |
8 | use crate::io::{self, BorrowedCursor, BufRead, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; |
9 | use crate::mem; |
10 | use crate::str; |
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 ] |
18 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
19 | (**self).read(buf) |
20 | } |
21 | |
22 | #[inline ] |
23 | fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { |
24 | (**self).read_buf(cursor) |
25 | } |
26 | |
27 | #[inline ] |
28 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
29 | (**self).read_vectored(bufs) |
30 | } |
31 | |
32 | #[inline ] |
33 | fn is_read_vectored(&self) -> bool { |
34 | (**self).is_read_vectored() |
35 | } |
36 | |
37 | #[inline ] |
38 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
39 | (**self).read_to_end(buf) |
40 | } |
41 | |
42 | #[inline ] |
43 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { |
44 | (**self).read_to_string(buf) |
45 | } |
46 | |
47 | #[inline ] |
48 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { |
49 | (**self).read_exact(buf) |
50 | } |
51 | #[inline ] |
52 | fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { |
53 | (**self).read_buf_exact(cursor) |
54 | } |
55 | } |
56 | #[stable (feature = "rust1" , since = "1.0.0" )] |
57 | impl<W: Write + ?Sized> Write for &mut W { |
58 | #[inline ] |
59 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
60 | (**self).write(buf) |
61 | } |
62 | |
63 | #[inline ] |
64 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
65 | (**self).write_vectored(bufs) |
66 | } |
67 | |
68 | #[inline ] |
69 | fn is_write_vectored(&self) -> bool { |
70 | (**self).is_write_vectored() |
71 | } |
72 | |
73 | #[inline ] |
74 | fn flush(&mut self) -> io::Result<()> { |
75 | (**self).flush() |
76 | } |
77 | |
78 | #[inline ] |
79 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
80 | (**self).write_all(buf) |
81 | } |
82 | |
83 | #[inline ] |
84 | fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { |
85 | (**self).write_fmt(fmt) |
86 | } |
87 | } |
88 | #[stable (feature = "rust1" , since = "1.0.0" )] |
89 | impl<S: Seek + ?Sized> Seek for &mut S { |
90 | #[inline ] |
91 | fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { |
92 | (**self).seek(pos) |
93 | } |
94 | |
95 | #[inline ] |
96 | fn stream_position(&mut self) -> io::Result<u64> { |
97 | (**self).stream_position() |
98 | } |
99 | } |
100 | #[stable (feature = "rust1" , since = "1.0.0" )] |
101 | impl<B: BufRead + ?Sized> BufRead for &mut B { |
102 | #[inline ] |
103 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
104 | (**self).fill_buf() |
105 | } |
106 | |
107 | #[inline ] |
108 | fn consume(&mut self, amt: usize) { |
109 | (**self).consume(amt) |
110 | } |
111 | |
112 | #[inline ] |
113 | fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { |
114 | (**self).read_until(byte, buf) |
115 | } |
116 | |
117 | #[inline ] |
118 | fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { |
119 | (**self).read_line(buf) |
120 | } |
121 | } |
122 | |
123 | #[stable (feature = "rust1" , since = "1.0.0" )] |
124 | impl<R: Read + ?Sized> Read for Box<R> { |
125 | #[inline ] |
126 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
127 | (**self).read(buf) |
128 | } |
129 | |
130 | #[inline ] |
131 | fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { |
132 | (**self).read_buf(cursor) |
133 | } |
134 | |
135 | #[inline ] |
136 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
137 | (**self).read_vectored(bufs) |
138 | } |
139 | |
140 | #[inline ] |
141 | fn is_read_vectored(&self) -> bool { |
142 | (**self).is_read_vectored() |
143 | } |
144 | |
145 | #[inline ] |
146 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
147 | (**self).read_to_end(buf) |
148 | } |
149 | |
150 | #[inline ] |
151 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { |
152 | (**self).read_to_string(buf) |
153 | } |
154 | |
155 | #[inline ] |
156 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { |
157 | (**self).read_exact(buf) |
158 | } |
159 | #[inline ] |
160 | fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { |
161 | (**self).read_buf_exact(cursor) |
162 | } |
163 | } |
164 | #[stable (feature = "rust1" , since = "1.0.0" )] |
165 | impl<W: Write + ?Sized> Write for Box<W> { |
166 | #[inline ] |
167 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
168 | (**self).write(buf) |
169 | } |
170 | |
171 | #[inline ] |
172 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
173 | (**self).write_vectored(bufs) |
174 | } |
175 | |
176 | #[inline ] |
177 | fn is_write_vectored(&self) -> bool { |
178 | (**self).is_write_vectored() |
179 | } |
180 | |
181 | #[inline ] |
182 | fn flush(&mut self) -> io::Result<()> { |
183 | (**self).flush() |
184 | } |
185 | |
186 | #[inline ] |
187 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
188 | (**self).write_all(buf) |
189 | } |
190 | |
191 | #[inline ] |
192 | fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { |
193 | (**self).write_fmt(fmt) |
194 | } |
195 | } |
196 | #[stable (feature = "rust1" , since = "1.0.0" )] |
197 | impl<S: Seek + ?Sized> Seek for Box<S> { |
198 | #[inline ] |
199 | fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { |
200 | (**self).seek(pos) |
201 | } |
202 | |
203 | #[inline ] |
204 | fn stream_position(&mut self) -> io::Result<u64> { |
205 | (**self).stream_position() |
206 | } |
207 | } |
208 | #[stable (feature = "rust1" , since = "1.0.0" )] |
209 | impl<B: BufRead + ?Sized> BufRead for Box<B> { |
210 | #[inline ] |
211 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
212 | (**self).fill_buf() |
213 | } |
214 | |
215 | #[inline ] |
216 | fn consume(&mut self, amt: usize) { |
217 | (**self).consume(amt) |
218 | } |
219 | |
220 | #[inline ] |
221 | fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { |
222 | (**self).read_until(byte, buf) |
223 | } |
224 | |
225 | #[inline ] |
226 | fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { |
227 | (**self).read_line(buf) |
228 | } |
229 | } |
230 | |
231 | // ============================================================================= |
232 | // In-memory buffer implementations |
233 | |
234 | /// Read is implemented for `&[u8]` by copying from the slice. |
235 | /// |
236 | /// Note that reading updates the slice to point to the yet unread part. |
237 | /// The slice will be empty when EOF is reached. |
238 | #[stable (feature = "rust1" , since = "1.0.0" )] |
239 | impl Read for &[u8] { |
240 | #[inline ] |
241 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
242 | let amt = cmp::min(buf.len(), self.len()); |
243 | let (a, b) = self.split_at(amt); |
244 | |
245 | // First check if the amount of bytes we want to read is small: |
246 | // `copy_from_slice` will generally expand to a call to `memcpy`, and |
247 | // for a single byte the overhead is significant. |
248 | if amt == 1 { |
249 | buf[0] = a[0]; |
250 | } else { |
251 | buf[..amt].copy_from_slice(a); |
252 | } |
253 | |
254 | *self = b; |
255 | Ok(amt) |
256 | } |
257 | |
258 | #[inline ] |
259 | fn read_buf(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { |
260 | let amt = cmp::min(cursor.capacity(), self.len()); |
261 | let (a, b) = self.split_at(amt); |
262 | |
263 | cursor.append(a); |
264 | |
265 | *self = b; |
266 | Ok(()) |
267 | } |
268 | |
269 | #[inline ] |
270 | fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
271 | let mut nread = 0; |
272 | for buf in bufs { |
273 | nread += self.read(buf)?; |
274 | if self.is_empty() { |
275 | break; |
276 | } |
277 | } |
278 | |
279 | Ok(nread) |
280 | } |
281 | |
282 | #[inline ] |
283 | fn is_read_vectored(&self) -> bool { |
284 | true |
285 | } |
286 | |
287 | #[inline ] |
288 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { |
289 | if buf.len() > self.len() { |
290 | return Err(io::Error::READ_EXACT_EOF); |
291 | } |
292 | let (a, b) = self.split_at(buf.len()); |
293 | |
294 | // First check if the amount of bytes we want to read is small: |
295 | // `copy_from_slice` will generally expand to a call to `memcpy`, and |
296 | // for a single byte the overhead is significant. |
297 | if buf.len() == 1 { |
298 | buf[0] = a[0]; |
299 | } else { |
300 | buf.copy_from_slice(a); |
301 | } |
302 | |
303 | *self = b; |
304 | Ok(()) |
305 | } |
306 | |
307 | #[inline ] |
308 | fn read_buf_exact(&mut self, mut cursor: BorrowedCursor<'_>) -> io::Result<()> { |
309 | if cursor.capacity() > self.len() { |
310 | return Err(io::Error::READ_EXACT_EOF); |
311 | } |
312 | let (a, b) = self.split_at(cursor.capacity()); |
313 | |
314 | cursor.append(a); |
315 | |
316 | *self = b; |
317 | Ok(()) |
318 | } |
319 | |
320 | #[inline ] |
321 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
322 | let len = self.len(); |
323 | buf.try_reserve(len)?; |
324 | buf.extend_from_slice(*self); |
325 | *self = &self[len..]; |
326 | Ok(len) |
327 | } |
328 | |
329 | #[inline ] |
330 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { |
331 | let content = str::from_utf8(self).map_err(|_| io::Error::INVALID_UTF8)?; |
332 | buf.push_str(content); |
333 | let len = self.len(); |
334 | *self = &self[len..]; |
335 | Ok(len) |
336 | } |
337 | } |
338 | |
339 | #[stable (feature = "rust1" , since = "1.0.0" )] |
340 | impl BufRead for &[u8] { |
341 | #[inline ] |
342 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
343 | Ok(*self) |
344 | } |
345 | |
346 | #[inline ] |
347 | fn consume(&mut self, amt: usize) { |
348 | *self = &self[amt..]; |
349 | } |
350 | } |
351 | |
352 | /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting |
353 | /// its data. |
354 | /// |
355 | /// Note that writing updates the slice to point to the yet unwritten part. |
356 | /// The slice will be empty when it has been completely overwritten. |
357 | /// |
358 | /// If the number of bytes to be written exceeds the size of the slice, write operations will |
359 | /// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of |
360 | /// kind `ErrorKind::WriteZero`. |
361 | #[stable (feature = "rust1" , since = "1.0.0" )] |
362 | impl Write for &mut [u8] { |
363 | #[inline ] |
364 | fn write(&mut self, data: &[u8]) -> io::Result<usize> { |
365 | let amt = cmp::min(data.len(), self.len()); |
366 | let (a, b) = mem::take(self).split_at_mut(amt); |
367 | a.copy_from_slice(&data[..amt]); |
368 | *self = b; |
369 | Ok(amt) |
370 | } |
371 | |
372 | #[inline ] |
373 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
374 | let mut nwritten = 0; |
375 | for buf in bufs { |
376 | nwritten += self.write(buf)?; |
377 | if self.is_empty() { |
378 | break; |
379 | } |
380 | } |
381 | |
382 | Ok(nwritten) |
383 | } |
384 | |
385 | #[inline ] |
386 | fn is_write_vectored(&self) -> bool { |
387 | true |
388 | } |
389 | |
390 | #[inline ] |
391 | fn write_all(&mut self, data: &[u8]) -> io::Result<()> { |
392 | if self.write(data)? == data.len() { Ok(()) } else { Err(io::Error::WRITE_ALL_EOF) } |
393 | } |
394 | |
395 | #[inline ] |
396 | fn flush(&mut self) -> io::Result<()> { |
397 | Ok(()) |
398 | } |
399 | } |
400 | |
401 | /// Write is implemented for `Vec<u8>` by appending to the vector. |
402 | /// The vector will grow as needed. |
403 | #[stable (feature = "rust1" , since = "1.0.0" )] |
404 | impl<A: Allocator> Write for Vec<u8, A> { |
405 | #[inline ] |
406 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
407 | self.extend_from_slice(buf); |
408 | Ok(buf.len()) |
409 | } |
410 | |
411 | #[inline ] |
412 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
413 | let len = bufs.iter().map(|b| b.len()).sum(); |
414 | self.reserve(len); |
415 | for buf in bufs { |
416 | self.extend_from_slice(buf); |
417 | } |
418 | Ok(len) |
419 | } |
420 | |
421 | #[inline ] |
422 | fn is_write_vectored(&self) -> bool { |
423 | true |
424 | } |
425 | |
426 | #[inline ] |
427 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
428 | self.extend_from_slice(buf); |
429 | Ok(()) |
430 | } |
431 | |
432 | #[inline ] |
433 | fn flush(&mut self) -> io::Result<()> { |
434 | Ok(()) |
435 | } |
436 | } |
437 | |
438 | /// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`. |
439 | #[stable (feature = "vecdeque_read_write" , since = "1.63.0" )] |
440 | impl<A: Allocator> Read for VecDeque<u8, A> { |
441 | /// Fill `buf` with the contents of the "front" slice as returned by |
442 | /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are |
443 | /// discontiguous, multiple calls to `read` will be needed to read the entire content. |
444 | #[inline ] |
445 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
446 | let (ref mut front, _) = self.as_slices(); |
447 | let n = Read::read(front, buf)?; |
448 | self.drain(..n); |
449 | Ok(n) |
450 | } |
451 | |
452 | #[inline ] |
453 | fn read_buf(&mut self, cursor: BorrowedCursor<'_>) -> io::Result<()> { |
454 | let (ref mut front, _) = self.as_slices(); |
455 | let n = cmp::min(cursor.capacity(), front.len()); |
456 | Read::read_buf(front, cursor)?; |
457 | self.drain(..n); |
458 | Ok(()) |
459 | } |
460 | |
461 | #[inline ] |
462 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { |
463 | // The total len is known upfront so we can reserve it in a single call. |
464 | let len = self.len(); |
465 | buf.try_reserve(len)?; |
466 | |
467 | let (front, back) = self.as_slices(); |
468 | buf.extend_from_slice(front); |
469 | buf.extend_from_slice(back); |
470 | self.clear(); |
471 | Ok(len) |
472 | } |
473 | |
474 | #[inline ] |
475 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { |
476 | // We have to use a single contiguous slice because the `VecDequeue` might be split in the |
477 | // middle of an UTF-8 character. |
478 | let len = self.len(); |
479 | let content = self.make_contiguous(); |
480 | let string = str::from_utf8(content).map_err(|_| io::Error::INVALID_UTF8)?; |
481 | buf.push_str(string); |
482 | self.clear(); |
483 | Ok(len) |
484 | } |
485 | } |
486 | |
487 | /// BufRead is implemented for `VecDeque<u8>` by reading bytes from the front of the `VecDeque`. |
488 | #[stable (feature = "vecdeque_buf_read" , since = "1.75.0" )] |
489 | impl<A: Allocator> BufRead for VecDeque<u8, A> { |
490 | /// Returns the contents of the "front" slice as returned by |
491 | /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are |
492 | /// discontiguous, multiple calls to `fill_buf` will be needed to read the entire content. |
493 | #[inline ] |
494 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
495 | let (front: &[u8], _) = self.as_slices(); |
496 | Ok(front) |
497 | } |
498 | |
499 | #[inline ] |
500 | fn consume(&mut self, amt: usize) { |
501 | self.drain(..amt); |
502 | } |
503 | } |
504 | |
505 | /// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed. |
506 | #[stable (feature = "vecdeque_read_write" , since = "1.63.0" )] |
507 | impl<A: Allocator> Write for VecDeque<u8, A> { |
508 | #[inline ] |
509 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
510 | self.extend(buf); |
511 | Ok(buf.len()) |
512 | } |
513 | |
514 | #[inline ] |
515 | fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
516 | let len = bufs.iter().map(|b| b.len()).sum(); |
517 | self.reserve(len); |
518 | for buf in bufs { |
519 | self.extend(&**buf); |
520 | } |
521 | Ok(len) |
522 | } |
523 | |
524 | #[inline ] |
525 | fn is_write_vectored(&self) -> bool { |
526 | true |
527 | } |
528 | |
529 | #[inline ] |
530 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { |
531 | self.extend(buf); |
532 | Ok(()) |
533 | } |
534 | |
535 | #[inline ] |
536 | fn flush(&mut self) -> io::Result<()> { |
537 | Ok(()) |
538 | } |
539 | } |
540 | |
541 | #[unstable (feature = "read_buf" , issue = "78485" )] |
542 | impl<'a> io::Write for core::io::BorrowedCursor<'a> { |
543 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
544 | let amt: usize = cmp::min(v1:buf.len(), self.capacity()); |
545 | self.append(&buf[..amt]); |
546 | Ok(amt) |
547 | } |
548 | |
549 | #[inline ] |
550 | fn flush(&mut self) -> io::Result<()> { |
551 | Ok(()) |
552 | } |
553 | } |
554 | |