]>
git.proxmox.com Git - rustc.git/blob - library/std/src/io/impls.rs
4 use crate::alloc
::Allocator
;
8 self, BufRead
, Error
, ErrorKind
, Initializer
, IoSlice
, IoSliceMut
, Read
, 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_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
24 (**self).read_vectored(bufs
)
28 fn is_read_vectored(&self) -> bool
{
29 (**self).is_read_vectored()
33 unsafe fn initializer(&self) -> Initializer
{
34 (**self).initializer()
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> {
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<B
: BufRead
+ ?Sized
> BufRead
for &mut B
{
94 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
99 fn consume(&mut self, amt
: usize) {
100 (**self).consume(amt
)
104 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
105 (**self).read_until(byte
, buf
)
109 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
110 (**self).read_line(buf
)
114 #[stable(feature = "rust1", since = "1.0.0")]
115 impl<R
: Read
+ ?Sized
> Read
for Box
<R
> {
117 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
122 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
123 (**self).read_vectored(bufs
)
127 fn is_read_vectored(&self) -> bool
{
128 (**self).is_read_vectored()
132 unsafe fn initializer(&self) -> Initializer
{
133 (**self).initializer()
137 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
138 (**self).read_to_end(buf
)
142 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
143 (**self).read_to_string(buf
)
147 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
148 (**self).read_exact(buf
)
151 #[stable(feature = "rust1", since = "1.0.0")]
152 impl<W
: Write
+ ?Sized
> Write
for Box
<W
> {
154 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
159 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
160 (**self).write_vectored(bufs
)
164 fn is_write_vectored(&self) -> bool
{
165 (**self).is_write_vectored()
169 fn flush(&mut self) -> io
::Result
<()> {
174 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
175 (**self).write_all(buf
)
179 fn write_fmt(&mut self, fmt
: fmt
::Arguments
<'_
>) -> io
::Result
<()> {
180 (**self).write_fmt(fmt
)
183 #[stable(feature = "rust1", since = "1.0.0")]
184 impl<S
: Seek
+ ?Sized
> Seek
for Box
<S
> {
186 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> {
190 #[stable(feature = "rust1", since = "1.0.0")]
191 impl<B
: BufRead
+ ?Sized
> BufRead
for Box
<B
> {
193 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
198 fn consume(&mut self, amt
: usize) {
199 (**self).consume(amt
)
203 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
204 (**self).read_until(byte
, buf
)
208 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
209 (**self).read_line(buf
)
213 // =============================================================================
214 // In-memory buffer implementations
216 /// Read is implemented for `&[u8]` by copying from the slice.
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] {
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
);
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.
233 buf
[..amt
].copy_from_slice(a
);
241 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
244 nread
+= self.read(buf
)?
;
254 fn is_read_vectored(&self) -> bool
{
259 unsafe fn initializer(&self) -> Initializer
{
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"));
268 let (a
, b
) = self.split_at(buf
.len());
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.
276 buf
.copy_from_slice(a
);
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
..];
292 #[stable(feature = "rust1", since = "1.0.0")]
293 impl BufRead
for &[u8] {
295 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
300 fn consume(&mut self, amt
: usize) {
301 *self = &self[amt
..];
305 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
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.
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] {
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
]);
326 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
327 let mut nwritten
= 0;
329 nwritten
+= self.write(buf
)?
;
339 fn is_write_vectored(&self) -> bool
{
344 fn write_all(&mut self, data
: &[u8]) -> io
::Result
<()> {
345 if self.write(data
)?
== data
.len() {
348 Err(Error
::new_const(ErrorKind
::WriteZero
, &"failed to write whole buffer"))
353 fn flush(&mut self) -> io
::Result
<()> {
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
> {
363 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
364 self.extend_from_slice(buf
);
369 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
370 let len
= bufs
.iter().map(|b
| b
.len()).sum();
373 self.extend_from_slice(buf
);
379 fn is_write_vectored(&self) -> bool
{
384 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
385 self.extend_from_slice(buf
);
390 fn flush(&mut self) -> io
::Result
<()> {