]> git.proxmox.com Git - rustc.git/blob - library/std/src/io/impls.rs
New upstream version 1.53.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, 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]
18 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
19 (**self).read(buf)
20 }
21
22 #[inline]
23 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
24 (**self).read_vectored(bufs)
25 }
26
27 #[inline]
28 fn is_read_vectored(&self) -> bool {
29 (**self).is_read_vectored()
30 }
31
32 #[inline]
33 unsafe fn initializer(&self) -> Initializer {
34 (**self).initializer()
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 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<B: BufRead + ?Sized> BufRead for &mut B {
93 #[inline]
94 fn fill_buf(&mut self) -> io::Result<&[u8]> {
95 (**self).fill_buf()
96 }
97
98 #[inline]
99 fn consume(&mut self, amt: usize) {
100 (**self).consume(amt)
101 }
102
103 #[inline]
104 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
105 (**self).read_until(byte, buf)
106 }
107
108 #[inline]
109 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
110 (**self).read_line(buf)
111 }
112 }
113
114 #[stable(feature = "rust1", since = "1.0.0")]
115 impl<R: Read + ?Sized> Read for Box<R> {
116 #[inline]
117 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
118 (**self).read(buf)
119 }
120
121 #[inline]
122 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
123 (**self).read_vectored(bufs)
124 }
125
126 #[inline]
127 fn is_read_vectored(&self) -> bool {
128 (**self).is_read_vectored()
129 }
130
131 #[inline]
132 unsafe fn initializer(&self) -> Initializer {
133 (**self).initializer()
134 }
135
136 #[inline]
137 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
138 (**self).read_to_end(buf)
139 }
140
141 #[inline]
142 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
143 (**self).read_to_string(buf)
144 }
145
146 #[inline]
147 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
148 (**self).read_exact(buf)
149 }
150 }
151 #[stable(feature = "rust1", since = "1.0.0")]
152 impl<W: Write + ?Sized> Write for Box<W> {
153 #[inline]
154 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
155 (**self).write(buf)
156 }
157
158 #[inline]
159 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
160 (**self).write_vectored(bufs)
161 }
162
163 #[inline]
164 fn is_write_vectored(&self) -> bool {
165 (**self).is_write_vectored()
166 }
167
168 #[inline]
169 fn flush(&mut self) -> io::Result<()> {
170 (**self).flush()
171 }
172
173 #[inline]
174 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
175 (**self).write_all(buf)
176 }
177
178 #[inline]
179 fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> {
180 (**self).write_fmt(fmt)
181 }
182 }
183 #[stable(feature = "rust1", since = "1.0.0")]
184 impl<S: Seek + ?Sized> Seek for Box<S> {
185 #[inline]
186 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
187 (**self).seek(pos)
188 }
189 }
190 #[stable(feature = "rust1", since = "1.0.0")]
191 impl<B: BufRead + ?Sized> BufRead for Box<B> {
192 #[inline]
193 fn fill_buf(&mut self) -> io::Result<&[u8]> {
194 (**self).fill_buf()
195 }
196
197 #[inline]
198 fn consume(&mut self, amt: usize) {
199 (**self).consume(amt)
200 }
201
202 #[inline]
203 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
204 (**self).read_until(byte, buf)
205 }
206
207 #[inline]
208 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
209 (**self).read_line(buf)
210 }
211 }
212
213 // =============================================================================
214 // In-memory buffer implementations
215
216 /// Read is implemented for `&[u8]` by copying from the slice.
217 ///
218 /// Note that reading updates the slice to point to the yet unread part.
219 /// The slice will be empty when EOF is reached.
220 #[stable(feature = "rust1", since = "1.0.0")]
221 impl Read for &[u8] {
222 #[inline]
223 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
224 let amt = cmp::min(buf.len(), self.len());
225 let (a, b) = self.split_at(amt);
226
227 // First check if the amount of bytes we want to read is small:
228 // `copy_from_slice` will generally expand to a call to `memcpy`, and
229 // for a single byte the overhead is significant.
230 if amt == 1 {
231 buf[0] = a[0];
232 } else {
233 buf[..amt].copy_from_slice(a);
234 }
235
236 *self = b;
237 Ok(amt)
238 }
239
240 #[inline]
241 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
242 let mut nread = 0;
243 for buf in bufs {
244 nread += self.read(buf)?;
245 if self.is_empty() {
246 break;
247 }
248 }
249
250 Ok(nread)
251 }
252
253 #[inline]
254 fn is_read_vectored(&self) -> bool {
255 true
256 }
257
258 #[inline]
259 unsafe fn initializer(&self) -> Initializer {
260 Initializer::nop()
261 }
262
263 #[inline]
264 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
265 if buf.len() > self.len() {
266 return Err(Error::new_const(ErrorKind::UnexpectedEof, &"failed to fill whole buffer"));
267 }
268 let (a, b) = self.split_at(buf.len());
269
270 // First check if the amount of bytes we want to read is small:
271 // `copy_from_slice` will generally expand to a call to `memcpy`, and
272 // for a single byte the overhead is significant.
273 if buf.len() == 1 {
274 buf[0] = a[0];
275 } else {
276 buf.copy_from_slice(a);
277 }
278
279 *self = b;
280 Ok(())
281 }
282
283 #[inline]
284 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
285 buf.extend_from_slice(*self);
286 let len = self.len();
287 *self = &self[len..];
288 Ok(len)
289 }
290 }
291
292 #[stable(feature = "rust1", since = "1.0.0")]
293 impl BufRead for &[u8] {
294 #[inline]
295 fn fill_buf(&mut self) -> io::Result<&[u8]> {
296 Ok(*self)
297 }
298
299 #[inline]
300 fn consume(&mut self, amt: usize) {
301 *self = &self[amt..];
302 }
303 }
304
305 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
306 /// its data.
307 ///
308 /// Note that writing updates the slice to point to the yet unwritten part.
309 /// The slice will be empty when it has been completely overwritten.
310 ///
311 /// If the number of bytes to be written exceeds the size of the slice, write operations will
312 /// return short writes: ultimately, `Ok(0)`; in this situation, `write_all` returns an error of
313 /// kind `ErrorKind::WriteZero`.
314 #[stable(feature = "rust1", since = "1.0.0")]
315 impl Write for &mut [u8] {
316 #[inline]
317 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
318 let amt = cmp::min(data.len(), self.len());
319 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
320 a.copy_from_slice(&data[..amt]);
321 *self = b;
322 Ok(amt)
323 }
324
325 #[inline]
326 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
327 let mut nwritten = 0;
328 for buf in bufs {
329 nwritten += self.write(buf)?;
330 if self.is_empty() {
331 break;
332 }
333 }
334
335 Ok(nwritten)
336 }
337
338 #[inline]
339 fn is_write_vectored(&self) -> bool {
340 true
341 }
342
343 #[inline]
344 fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
345 if self.write(data)? == data.len() {
346 Ok(())
347 } else {
348 Err(Error::new_const(ErrorKind::WriteZero, &"failed to write whole buffer"))
349 }
350 }
351
352 #[inline]
353 fn flush(&mut self) -> io::Result<()> {
354 Ok(())
355 }
356 }
357
358 /// Write is implemented for `Vec<u8>` by appending to the vector.
359 /// The vector will grow as needed.
360 #[stable(feature = "rust1", since = "1.0.0")]
361 impl<A: Allocator> Write for Vec<u8, A> {
362 #[inline]
363 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
364 self.extend_from_slice(buf);
365 Ok(buf.len())
366 }
367
368 #[inline]
369 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
370 let len = bufs.iter().map(|b| b.len()).sum();
371 self.reserve(len);
372 for buf in bufs {
373 self.extend_from_slice(buf);
374 }
375 Ok(len)
376 }
377
378 #[inline]
379 fn is_write_vectored(&self) -> bool {
380 true
381 }
382
383 #[inline]
384 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
385 self.extend_from_slice(buf);
386 Ok(())
387 }
388
389 #[inline]
390 fn flush(&mut self) -> io::Result<()> {
391 Ok(())
392 }
393 }