]>
git.proxmox.com Git - rustc.git/blob - library/std/src/io/impls.rs
7 self, BufRead
, Error
, ErrorKind
, Initializer
, IoSlice
, IoSliceMut
, Read
, Seek
, SeekFrom
, Write
,
11 // =============================================================================
12 // Forwarding implementations
14 #[stable(feature = "rust1", since = "1.0.0")]
15 impl<R
: Read
+ ?Sized
> Read
for &mut R
{
17 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
22 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
23 (**self).read_vectored(bufs
)
27 fn is_read_vectored(&self) -> bool
{
28 (**self).is_read_vectored()
32 unsafe fn initializer(&self) -> Initializer
{
33 (**self).initializer()
37 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
38 (**self).read_to_end(buf
)
42 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
43 (**self).read_to_string(buf
)
47 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
48 (**self).read_exact(buf
)
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl<W
: Write
+ ?Sized
> Write
for &mut W
{
54 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
59 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
60 (**self).write_vectored(bufs
)
64 fn is_write_vectored(&self) -> bool
{
65 (**self).is_write_vectored()
69 fn flush(&mut self) -> io
::Result
<()> {
74 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
75 (**self).write_all(buf
)
79 fn write_fmt(&mut self, fmt
: fmt
::Arguments
<'_
>) -> io
::Result
<()> {
80 (**self).write_fmt(fmt
)
83 #[stable(feature = "rust1", since = "1.0.0")]
84 impl<S
: Seek
+ ?Sized
> Seek
for &mut S
{
86 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> {
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl<B
: BufRead
+ ?Sized
> BufRead
for &mut B
{
93 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
98 fn consume(&mut self, amt
: usize) {
103 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
104 (**self).read_until(byte
, buf
)
108 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
109 (**self).read_line(buf
)
113 #[stable(feature = "rust1", since = "1.0.0")]
114 impl<R
: Read
+ ?Sized
> Read
for Box
<R
> {
116 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
121 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
122 (**self).read_vectored(bufs
)
126 fn is_read_vectored(&self) -> bool
{
127 (**self).is_read_vectored()
131 unsafe fn initializer(&self) -> Initializer
{
132 (**self).initializer()
136 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
137 (**self).read_to_end(buf
)
141 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
142 (**self).read_to_string(buf
)
146 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
147 (**self).read_exact(buf
)
150 #[stable(feature = "rust1", since = "1.0.0")]
151 impl<W
: Write
+ ?Sized
> Write
for Box
<W
> {
153 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
158 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
159 (**self).write_vectored(bufs
)
163 fn is_write_vectored(&self) -> bool
{
164 (**self).is_write_vectored()
168 fn flush(&mut self) -> io
::Result
<()> {
173 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
174 (**self).write_all(buf
)
178 fn write_fmt(&mut self, fmt
: fmt
::Arguments
<'_
>) -> io
::Result
<()> {
179 (**self).write_fmt(fmt
)
182 #[stable(feature = "rust1", since = "1.0.0")]
183 impl<S
: Seek
+ ?Sized
> Seek
for Box
<S
> {
185 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> {
189 #[stable(feature = "rust1", since = "1.0.0")]
190 impl<B
: BufRead
+ ?Sized
> BufRead
for Box
<B
> {
192 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
197 fn consume(&mut self, amt
: usize) {
198 (**self).consume(amt
)
202 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
203 (**self).read_until(byte
, buf
)
207 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
208 (**self).read_line(buf
)
212 // Used by panicking::default_hook
214 /// This impl is only used by printing logic, so any error returned is always
215 /// of kind `Other`, and should be ignored.
216 impl Write
for dyn ::realstd
::io
::LocalOutput
{
217 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
218 (*self).write(buf
).map_err(|_
| ErrorKind
::Other
.into())
221 fn flush(&mut self) -> io
::Result
<()> {
222 (*self).flush().map_err(|_
| ErrorKind
::Other
.into())
226 // =============================================================================
227 // In-memory buffer implementations
229 /// Read is implemented for `&[u8]` by copying from the slice.
231 /// Note that reading updates the slice to point to the yet unread part.
232 /// The slice will be empty when EOF is reached.
233 #[stable(feature = "rust1", since = "1.0.0")]
234 impl Read
for &[u8] {
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
);
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.
246 buf
[..amt
].copy_from_slice(a
);
254 fn read_vectored(&mut self, bufs
: &mut [IoSliceMut
<'_
>]) -> io
::Result
<usize> {
257 nread
+= self.read(buf
)?
;
267 fn is_read_vectored(&self) -> bool
{
272 unsafe fn initializer(&self) -> Initializer
{
277 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
278 if buf
.len() > self.len() {
279 return Err(Error
::new(ErrorKind
::UnexpectedEof
, "failed to fill whole buffer"));
281 let (a
, b
) = self.split_at(buf
.len());
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.
289 buf
.copy_from_slice(a
);
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
..];
305 #[stable(feature = "rust1", since = "1.0.0")]
306 impl BufRead
for &[u8] {
308 fn fill_buf(&mut self) -> io
::Result
<&[u8]> {
313 fn consume(&mut self, amt
: usize) {
314 *self = &self[amt
..];
318 /// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
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.
323 #[stable(feature = "rust1", since = "1.0.0")]
324 impl Write
for &mut [u8] {
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
);
329 a
.copy_from_slice(&data
[..amt
]);
335 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
336 let mut nwritten
= 0;
338 nwritten
+= self.write(buf
)?
;
348 fn is_write_vectored(&self) -> bool
{
353 fn write_all(&mut self, data
: &[u8]) -> io
::Result
<()> {
354 if self.write(data
)?
== data
.len() {
357 Err(Error
::new(ErrorKind
::WriteZero
, "failed to write whole buffer"))
362 fn flush(&mut self) -> io
::Result
<()> {
367 /// Write is implemented for `Vec<u8>` by appending to the vector.
368 /// The vector will grow as needed.
369 #[stable(feature = "rust1", since = "1.0.0")]
370 impl Write
for Vec
<u8> {
372 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
373 self.extend_from_slice(buf
);
378 fn write_vectored(&mut self, bufs
: &[IoSlice
<'_
>]) -> io
::Result
<usize> {
379 let len
= bufs
.iter().map(|b
| b
.len()).sum();
382 self.extend_from_slice(buf
);
388 fn is_write_vectored(&self) -> bool
{
393 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
394 self.extend_from_slice(buf
);
399 fn flush(&mut self) -> io
::Result
<()> {