]> git.proxmox.com Git - rustc.git/blame - library/std/src/io/impls.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / library / std / src / io / impls.rs
CommitLineData
1b1a35ee
XL
1#[cfg(test)]
2mod tests;
3
6a06907d 4use crate::alloc::Allocator;
532ac7d7 5use crate::cmp;
923072b8 6use crate::collections::VecDeque;
532ac7d7 7use crate::fmt;
60c5eb7d 8use crate::io::{
5099ac24 9 self, BufRead, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, Seek, SeekFrom, Write,
60c5eb7d 10};
532ac7d7 11use crate::mem;
85aaf69f
SL
12
13// =============================================================================
14// Forwarding implementations
15
c34b1796 16#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 17impl<R: Read + ?Sized> Read for &mut R {
c34b1796
AL
18 #[inline]
19 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
20 (**self).read(buf)
21 }
22
a2a8927a
XL
23 #[inline]
24 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
25 (**self).read_buf(buf)
26 }
27
9fa01778 28 #[inline]
48663c56 29 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
9fa01778
XL
30 (**self).read_vectored(bufs)
31 }
32
f9f354fc
XL
33 #[inline]
34 fn is_read_vectored(&self) -> bool {
35 (**self).is_read_vectored()
36 }
37
c34b1796
AL
38 #[inline]
39 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
40 (**self).read_to_end(buf)
41 }
42
43 #[inline]
44 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
45 (**self).read_to_string(buf)
46 }
e9174d1e
SL
47
48 #[inline]
49 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
50 (**self).read_exact(buf)
51 }
85aaf69f 52}
c34b1796 53#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 54impl<W: Write + ?Sized> Write for &mut W {
c34b1796 55 #[inline]
60c5eb7d
XL
56 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
57 (**self).write(buf)
58 }
c34b1796 59
9fa01778 60 #[inline]
48663c56 61 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
9fa01778
XL
62 (**self).write_vectored(bufs)
63 }
64
f9f354fc
XL
65 #[inline]
66 fn is_write_vectored(&self) -> bool {
67 (**self).is_write_vectored()
68 }
69
c34b1796 70 #[inline]
60c5eb7d
XL
71 fn flush(&mut self) -> io::Result<()> {
72 (**self).flush()
73 }
c34b1796
AL
74
75 #[inline]
76 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
77 (**self).write_all(buf)
78 }
79
80 #[inline]
532ac7d7 81 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
c34b1796
AL
82 (**self).write_fmt(fmt)
83 }
85aaf69f 84}
c34b1796 85#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 86impl<S: Seek + ?Sized> Seek for &mut S {
c34b1796 87 #[inline]
60c5eb7d
XL
88 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
89 (**self).seek(pos)
90 }
17df50a5
XL
91
92 #[inline]
93 fn stream_position(&mut self) -> io::Result<u64> {
94 (**self).stream_position()
95 }
85aaf69f 96}
c34b1796 97#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 98impl<B: BufRead + ?Sized> BufRead for &mut B {
c34b1796 99 #[inline]
60c5eb7d
XL
100 fn fill_buf(&mut self) -> io::Result<&[u8]> {
101 (**self).fill_buf()
102 }
c34b1796
AL
103
104 #[inline]
60c5eb7d
XL
105 fn consume(&mut self, amt: usize) {
106 (**self).consume(amt)
107 }
c34b1796
AL
108
109 #[inline]
110 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
111 (**self).read_until(byte, buf)
112 }
113
114 #[inline]
115 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
116 (**self).read_line(buf)
117 }
85aaf69f
SL
118}
119
c34b1796 120#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 121impl<R: Read + ?Sized> Read for Box<R> {
c34b1796
AL
122 #[inline]
123 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
124 (**self).read(buf)
125 }
126
a2a8927a
XL
127 #[inline]
128 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
129 (**self).read_buf(buf)
130 }
131
9fa01778 132 #[inline]
48663c56 133 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
9fa01778
XL
134 (**self).read_vectored(bufs)
135 }
136
f9f354fc
XL
137 #[inline]
138 fn is_read_vectored(&self) -> bool {
139 (**self).is_read_vectored()
140 }
141
c34b1796
AL
142 #[inline]
143 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
144 (**self).read_to_end(buf)
145 }
146
147 #[inline]
148 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
149 (**self).read_to_string(buf)
150 }
e9174d1e
SL
151
152 #[inline]
153 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
154 (**self).read_exact(buf)
155 }
85aaf69f 156}
c34b1796 157#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 158impl<W: Write + ?Sized> Write for Box<W> {
c34b1796 159 #[inline]
60c5eb7d
XL
160 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
161 (**self).write(buf)
162 }
c34b1796 163
9fa01778 164 #[inline]
48663c56 165 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
9fa01778
XL
166 (**self).write_vectored(bufs)
167 }
168
f9f354fc
XL
169 #[inline]
170 fn is_write_vectored(&self) -> bool {
171 (**self).is_write_vectored()
172 }
173
c34b1796 174 #[inline]
60c5eb7d
XL
175 fn flush(&mut self) -> io::Result<()> {
176 (**self).flush()
177 }
c34b1796
AL
178
179 #[inline]
180 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
181 (**self).write_all(buf)
182 }
183
184 #[inline]
532ac7d7 185 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
c34b1796
AL
186 (**self).write_fmt(fmt)
187 }
85aaf69f 188}
c34b1796 189#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 190impl<S: Seek + ?Sized> Seek for Box<S> {
c34b1796 191 #[inline]
60c5eb7d
XL
192 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
193 (**self).seek(pos)
194 }
17df50a5
XL
195
196 #[inline]
197 fn stream_position(&mut self) -> io::Result<u64> {
198 (**self).stream_position()
199 }
85aaf69f 200}
c34b1796 201#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 202impl<B: BufRead + ?Sized> BufRead for Box<B> {
c34b1796 203 #[inline]
60c5eb7d
XL
204 fn fill_buf(&mut self) -> io::Result<&[u8]> {
205 (**self).fill_buf()
206 }
c34b1796
AL
207
208 #[inline]
60c5eb7d
XL
209 fn consume(&mut self, amt: usize) {
210 (**self).consume(amt)
211 }
c34b1796
AL
212
213 #[inline]
214 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
215 (**self).read_until(byte, buf)
216 }
217
218 #[inline]
219 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
220 (**self).read_line(buf)
221 }
85aaf69f
SL
222}
223
224// =============================================================================
225// In-memory buffer implementations
226
c30ab7b3
SL
227/// Read is implemented for `&[u8]` by copying from the slice.
228///
229/// Note that reading updates the slice to point to the yet unread part.
230/// The slice will be empty when EOF is reached.
c34b1796 231#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 232impl Read for &[u8] {
c34b1796 233 #[inline]
85aaf69f
SL
234 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
235 let amt = cmp::min(buf.len(), self.len());
236 let (a, b) = self.split_at(amt);
476ff2be
SL
237
238 // First check if the amount of bytes we want to read is small:
239 // `copy_from_slice` will generally expand to a call to `memcpy`, and
240 // for a single byte the overhead is significant.
241 if amt == 1 {
242 buf[0] = a[0];
243 } else {
244 buf[..amt].copy_from_slice(a);
245 }
246
85aaf69f
SL
247 *self = b;
248 Ok(amt)
249 }
e9174d1e 250
a2a8927a
XL
251 #[inline]
252 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
253 let amt = cmp::min(buf.remaining(), self.len());
254 let (a, b) = self.split_at(amt);
255
256 buf.append(a);
257
258 *self = b;
259 Ok(())
260 }
261
9fa01778 262 #[inline]
48663c56 263 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
9fa01778
XL
264 let mut nread = 0;
265 for buf in bufs {
266 nread += self.read(buf)?;
267 if self.is_empty() {
268 break;
269 }
270 }
271
272 Ok(nread)
273 }
274
f9f354fc
XL
275 #[inline]
276 fn is_read_vectored(&self) -> bool {
277 true
278 }
279
e9174d1e
SL
280 #[inline]
281 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
282 if buf.len() > self.len() {
5099ac24
FG
283 return Err(io::const_io_error!(
284 ErrorKind::UnexpectedEof,
285 "failed to fill whole buffer"
286 ));
e9174d1e
SL
287 }
288 let (a, b) = self.split_at(buf.len());
476ff2be
SL
289
290 // First check if the amount of bytes we want to read is small:
291 // `copy_from_slice` will generally expand to a call to `memcpy`, and
292 // for a single byte the overhead is significant.
293 if buf.len() == 1 {
294 buf[0] = a[0];
295 } else {
296 buf.copy_from_slice(a);
297 }
298
e9174d1e
SL
299 *self = b;
300 Ok(())
301 }
ea8adc8c
XL
302
303 #[inline]
304 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
305 buf.extend_from_slice(*self);
306 let len = self.len();
307 *self = &self[len..];
308 Ok(len)
309 }
85aaf69f
SL
310}
311
c34b1796 312#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 313impl BufRead for &[u8] {
c34b1796 314 #[inline]
60c5eb7d
XL
315 fn fill_buf(&mut self) -> io::Result<&[u8]> {
316 Ok(*self)
317 }
c34b1796
AL
318
319 #[inline]
60c5eb7d
XL
320 fn consume(&mut self, amt: usize) {
321 *self = &self[amt..];
322 }
85aaf69f
SL
323}
324
c30ab7b3
SL
325/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
326/// its data.
327///
328/// Note that writing updates the slice to point to the yet unwritten part.
329/// The slice will be empty when it has been completely overwritten.
fc512014
XL
330///
331/// If the number of bytes to be written exceeds the size of the slice, write operations will
332/// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
333/// kind `ErrorKind::WriteZero`.
c34b1796 334#[stable(feature = "rust1", since = "1.0.0")]
9fa01778 335impl Write for &mut [u8] {
c34b1796 336 #[inline]
85aaf69f
SL
337 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
338 let amt = cmp::min(data.len(), self.len());
339 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
7453a54e 340 a.copy_from_slice(&data[..amt]);
85aaf69f
SL
341 *self = b;
342 Ok(amt)
343 }
c34b1796 344
9fa01778 345 #[inline]
48663c56 346 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
9fa01778
XL
347 let mut nwritten = 0;
348 for buf in bufs {
349 nwritten += self.write(buf)?;
350 if self.is_empty() {
351 break;
352 }
353 }
354
355 Ok(nwritten)
356 }
357
f9f354fc
XL
358 #[inline]
359 fn is_write_vectored(&self) -> bool {
360 true
361 }
362
c34b1796
AL
363 #[inline]
364 fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
54a0048b 365 if self.write(data)? == data.len() {
c34b1796
AL
366 Ok(())
367 } else {
5099ac24 368 Err(io::const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer"))
c34b1796
AL
369 }
370 }
371
372 #[inline]
60c5eb7d
XL
373 fn flush(&mut self) -> io::Result<()> {
374 Ok(())
375 }
85aaf69f
SL
376}
377
c30ab7b3
SL
378/// Write is implemented for `Vec<u8>` by appending to the vector.
379/// The vector will grow as needed.
c34b1796 380#[stable(feature = "rust1", since = "1.0.0")]
6a06907d 381impl<A: Allocator> Write for Vec<u8, A> {
c34b1796 382 #[inline]
85aaf69f 383 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
92a42be0 384 self.extend_from_slice(buf);
85aaf69f
SL
385 Ok(buf.len())
386 }
c34b1796 387
9fa01778 388 #[inline]
48663c56 389 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
9fa01778
XL
390 let len = bufs.iter().map(|b| b.len()).sum();
391 self.reserve(len);
392 for buf in bufs {
393 self.extend_from_slice(buf);
394 }
395 Ok(len)
396 }
397
f9f354fc
XL
398 #[inline]
399 fn is_write_vectored(&self) -> bool {
400 true
401 }
402
c34b1796
AL
403 #[inline]
404 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
92a42be0 405 self.extend_from_slice(buf);
c34b1796
AL
406 Ok(())
407 }
408
409 #[inline]
60c5eb7d
XL
410 fn flush(&mut self) -> io::Result<()> {
411 Ok(())
412 }
85aaf69f 413}
923072b8
FG
414
415/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`.
416#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
417impl<A: Allocator> Read for VecDeque<u8, A> {
418 /// Fill `buf` with the contents of the "front" slice as returned by
419 /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are
420 /// discontiguous, multiple calls to `read` will be needed to read the entire content.
421 #[inline]
422 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
423 let (ref mut front, _) = self.as_slices();
424 let n = Read::read(front, buf)?;
425 self.drain(..n);
426 Ok(n)
427 }
428
429 #[inline]
430 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
431 let (ref mut front, _) = self.as_slices();
432 let n = cmp::min(buf.remaining(), front.len());
433 Read::read_buf(front, buf)?;
434 self.drain(..n);
435 Ok(())
436 }
437}
438
439/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed.
440#[stable(feature = "vecdeque_read_write", since = "1.63.0")]
441impl<A: Allocator> Write for VecDeque<u8, A> {
442 #[inline]
443 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
444 self.extend(buf);
445 Ok(buf.len())
446 }
447
448 #[inline]
449 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
450 self.extend(buf);
451 Ok(())
452 }
453
454 #[inline]
455 fn flush(&mut self) -> io::Result<()> {
456 Ok(())
457 }
458}