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