]>
git.proxmox.com Git - rustc.git/blob - library/std/src/io/impls.rs
4 use crate::alloc
::Allocator
;
8 self, BufRead
, ErrorKind
, IoSlice
, IoSliceMut
, Read
, ReadBuf
, Seek
, SeekFrom
, Write
,
12 // =============================================================================
13 // Forwarding implementations
15 #[stable(feature = "rust1", since = "1.0.0")]
16 impl<R
: Read
+ ?Sized
> Read
for &mut R
{
18 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
23 fn read_buf(&mut self, buf
: &mut ReadBuf
<'_
>) -> io
::Result
<()> {
24 (**self).read_buf(buf
)
28 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
29 (**self).read_vectored(bufs
)
33 fn is_read_vectored(&self) -> bool
{
34 (**self).is_read_vectored()
38 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
39 (**self).read_to_end(buf
)
43 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
44 (**self).read_to_string(buf
)
48 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
49 (**self).read_exact(buf
)
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<W
: Write
+ ?Sized
> Write
for &mut W
{
55 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
60 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
61 (**self).write_vectored(bufs
)
65 fn is_write_vectored(&self) -> bool
{
66 (**self).is_write_vectored()
70 fn flush(&mut self) -> io
::Result
<()> {
75 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
76 (**self).write_all(buf
)
80 fn write_fmt(&mut self, fmt
: fmt
::Arguments
<'_
>) -> io
::Result
<()> {
81 (**self).write_fmt(fmt
)
84 #[stable(feature = "rust1", since = "1.0.0")]
85 impl<S
: Seek
+ ?Sized
> Seek
for &mut S
{
87 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> {
92 fn stream_position(&mut self) -> io
::Result
<u64> {
93 (**self).stream_position()
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl<B
: BufRead
+ ?Sized
> BufRead
for &mut B
{
99 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
104 fn consume(&mut self, amt
: usize) {
105 (**self).consume(amt
)
109 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
110 (**self).read_until(byte
, buf
)
114 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
115 (**self).read_line(buf
)
119 #[stable(feature = "rust1", since = "1.0.0")]
120 impl<R
: Read
+ ?Sized
> Read
for Box
<R
> {
122 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
127 fn read_buf(&mut self, buf
: &mut ReadBuf
<'_
>) -> io
::Result
<()> {
128 (**self).read_buf(buf
)
132 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
133 (**self).read_vectored(bufs
)
137 fn is_read_vectored(&self) -> bool
{
138 (**self).is_read_vectored()
142 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
143 (**self).read_to_end(buf
)
147 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
148 (**self).read_to_string(buf
)
152 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
153 (**self).read_exact(buf
)
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl<W
: Write
+ ?Sized
> Write
for Box
<W
> {
159 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
164 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
165 (**self).write_vectored(bufs
)
169 fn is_write_vectored(&self) -> bool
{
170 (**self).is_write_vectored()
174 fn flush(&mut self) -> io
::Result
<()> {
179 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
180 (**self).write_all(buf
)
184 fn write_fmt(&mut self, fmt
: fmt
::Arguments
<'_
>) -> io
::Result
<()> {
185 (**self).write_fmt(fmt
)
188 #[stable(feature = "rust1", since = "1.0.0")]
189 impl<S
: Seek
+ ?Sized
> Seek
for Box
<S
> {
191 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> {
196 fn stream_position(&mut self) -> io
::Result
<u64> {
197 (**self).stream_position()
200 #[stable(feature = "rust1", since = "1.0.0")]
201 impl<B
: BufRead
+ ?Sized
> BufRead
for Box
<B
> {
203 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
208 fn consume(&mut self, amt
: usize) {
209 (**self).consume(amt
)
213 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
214 (**self).read_until(byte
, buf
)
218 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
219 (**self).read_line(buf
)
223 // =============================================================================
224 // In-memory buffer implementations
226 /// Read is implemented for `&[u8]` by copying from the slice.
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] {
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
);
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.
243 buf
[..amt
].copy_from_slice(a
);
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
);
262 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
265 nread
+= self.read(buf
)?
;
275 fn is_read_vectored(&self) -> bool
{
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"
287 let (a
, b
) = self.split_at(buf
.len());
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.
295 buf
.copy_from_slice(a
);
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
..];
311 #[stable(feature = "rust1", since = "1.0.0")]
312 impl BufRead
for &[u8] {
314 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
319 fn consume(&mut self, amt
: usize) {
320 *self = &self[amt
..];
324 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
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.
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] {
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
]);
345 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
346 let mut nwritten
= 0;
348 nwritten
+= self.write(buf
)?
;
358 fn is_write_vectored(&self) -> bool
{
363 fn write_all(&mut self, data
: &[u8]) -> io
::Result
<()> {
364 if self.write(data
)?
== data
.len() {
367 Err(io
::const_io_error
!(ErrorKind
::WriteZero
, "failed to write whole buffer"))
372 fn flush(&mut self) -> io
::Result
<()> {
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
> {
382 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
383 self.extend_from_slice(buf
);
388 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
389 let len
= bufs
.iter().map(|b
| b
.len()).sum();
392 self.extend_from_slice(buf
);
398 fn is_write_vectored(&self) -> bool
{
403 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
404 self.extend_from_slice(buf
);
409 fn flush(&mut self) -> io
::Result
<()> {