]> git.proxmox.com Git - rustc.git/blob - library/std/src/io/impls.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / library / std / src / io / impls.rs
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, ErrorKind, IoSlice, IoSliceMut, Read, ReadBuf, 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]
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, buf: &mut ReadBuf<'_>) -> io::Result<()> {
24 (**self).read_buf(buf)
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 }
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<W: Write + ?Sized> Write for &mut W {
54 #[inline]
55 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
56 (**self).write(buf)
57 }
58
59 #[inline]
60 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
61 (**self).write_vectored(bufs)
62 }
63
64 #[inline]
65 fn is_write_vectored(&self) -> bool {
66 (**self).is_write_vectored()
67 }
68
69 #[inline]
70 fn flush(&mut self) -> io::Result<()> {
71 (**self).flush()
72 }
73
74 #[inline]
75 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
76 (**self).write_all(buf)
77 }
78
79 #[inline]
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]
87 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
88 (**self).seek(pos)
89 }
90
91 #[inline]
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]
99 fn fill_buf(&mut self) -> io::Result<&[u8]> {
100 (**self).fill_buf()
101 }
102
103 #[inline]
104 fn consume(&mut self, amt: usize) {
105 (**self).consume(amt)
106 }
107
108 #[inline]
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]
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]
122 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
123 (**self).read(buf)
124 }
125
126 #[inline]
127 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
128 (**self).read_buf(buf)
129 }
130
131 #[inline]
132 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
133 (**self).read_vectored(bufs)
134 }
135
136 #[inline]
137 fn is_read_vectored(&self) -> bool {
138 (**self).is_read_vectored()
139 }
140
141 #[inline]
142 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
143 (**self).read_to_end(buf)
144 }
145
146 #[inline]
147 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
148 (**self).read_to_string(buf)
149 }
150
151 #[inline]
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]
159 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
160 (**self).write(buf)
161 }
162
163 #[inline]
164 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
165 (**self).write_vectored(bufs)
166 }
167
168 #[inline]
169 fn is_write_vectored(&self) -> bool {
170 (**self).is_write_vectored()
171 }
172
173 #[inline]
174 fn flush(&mut self) -> io::Result<()> {
175 (**self).flush()
176 }
177
178 #[inline]
179 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
180 (**self).write_all(buf)
181 }
182
183 #[inline]
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]
191 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
192 (**self).seek(pos)
193 }
194
195 #[inline]
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]
203 fn fill_buf(&mut self) -> io::Result<&[u8]> {
204 (**self).fill_buf()
205 }
206
207 #[inline]
208 fn consume(&mut self, amt: usize) {
209 (**self).consume(amt)
210 }
211
212 #[inline]
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]
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]
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]
251 fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> io::Result<()> {
252 let amt = cmp::min(buf.remaining(), self.len());
253 let (a, b) = self.split_at(amt);
254
255 buf.append(a);
256
257 *self = b;
258 Ok(())
259 }
260
261 #[inline]
262 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
263 let mut nread = 0;
264 for buf in bufs {
265 nread += self.read(buf)?;
266 if self.is_empty() {
267 break;
268 }
269 }
270
271 Ok(nread)
272 }
273
274 #[inline]
275 fn is_read_vectored(&self) -> bool {
276 true
277 }
278
279 #[inline]
280 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
281 if buf.len() > self.len() {
282 return Err(io::const_io_error!(
283 ErrorKind::UnexpectedEof,
284 "failed to fill whole buffer"
285 ));
286 }
287 let (a, b) = self.split_at(buf.len());
288
289 // First check if the amount of bytes we want to read is small:
290 // `copy_from_slice` will generally expand to a call to `memcpy`, and
291 // for a single byte the overhead is significant.
292 if buf.len() == 1 {
293 buf[0] = a[0];
294 } else {
295 buf.copy_from_slice(a);
296 }
297
298 *self = b;
299 Ok(())
300 }
301
302 #[inline]
303 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
304 buf.extend_from_slice(*self);
305 let len = self.len();
306 *self = &self[len..];
307 Ok(len)
308 }
309 }
310
311 #[stable(feature = "rust1", since = "1.0.0")]
312 impl BufRead for &[u8] {
313 #[inline]
314 fn fill_buf(&mut self) -> io::Result<&[u8]> {
315 Ok(*self)
316 }
317
318 #[inline]
319 fn consume(&mut self, amt: usize) {
320 *self = &self[amt..];
321 }
322 }
323
324 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
325 /// its data.
326 ///
327 /// Note that writing updates the slice to point to the yet unwritten part.
328 /// The slice will be empty when it has been completely overwritten.
329 ///
330 /// If the number of bytes to be written exceeds the size of the slice, write operations will
331 /// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
332 /// kind `ErrorKind::WriteZero`.
333 #[stable(feature = "rust1", since = "1.0.0")]
334 impl Write for &mut [u8] {
335 #[inline]
336 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
337 let amt = cmp::min(data.len(), self.len());
338 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
339 a.copy_from_slice(&data[..amt]);
340 *self = b;
341 Ok(amt)
342 }
343
344 #[inline]
345 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
346 let mut nwritten = 0;
347 for buf in bufs {
348 nwritten += self.write(buf)?;
349 if self.is_empty() {
350 break;
351 }
352 }
353
354 Ok(nwritten)
355 }
356
357 #[inline]
358 fn is_write_vectored(&self) -> bool {
359 true
360 }
361
362 #[inline]
363 fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
364 if self.write(data)? == data.len() {
365 Ok(())
366 } else {
367 Err(io::const_io_error!(ErrorKind::WriteZero, "failed to write whole buffer"))
368 }
369 }
370
371 #[inline]
372 fn flush(&mut self) -> io::Result<()> {
373 Ok(())
374 }
375 }
376
377 /// Write is implemented for `Vec<u8>` by appending to the vector.
378 /// The vector will grow as needed.
379 #[stable(feature = "rust1", since = "1.0.0")]
380 impl<A: Allocator> Write for Vec<u8, A> {
381 #[inline]
382 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
383 self.extend_from_slice(buf);
384 Ok(buf.len())
385 }
386
387 #[inline]
388 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
389 let len = bufs.iter().map(|b| b.len()).sum();
390 self.reserve(len);
391 for buf in bufs {
392 self.extend_from_slice(buf);
393 }
394 Ok(len)
395 }
396
397 #[inline]
398 fn is_write_vectored(&self) -> bool {
399 true
400 }
401
402 #[inline]
403 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
404 self.extend_from_slice(buf);
405 Ok(())
406 }
407
408 #[inline]
409 fn flush(&mut self) -> io::Result<()> {
410 Ok(())
411 }
412 }