1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use io
::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}
;
19 // =============================================================================
20 // Forwarding implementations
22 #[stable(feature = "rust1", since = "1.0.0")]
23 impl<'a
, R
: Read
+ ?Sized
> Read
for &'a
mut R
{
25 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
30 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
31 (**self).read_to_end(buf
)
35 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
36 (**self).read_to_string(buf
)
40 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
41 (**self).read_exact(buf
)
44 #[stable(feature = "rust1", since = "1.0.0")]
45 impl<'a
, W
: Write
+ ?Sized
> Write
for &'a
mut W
{
47 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> { (**self).write(buf) }
50 fn flush(&mut self) -> io
::Result
<()> { (**self).flush() }
53 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
54 (**self).write_all(buf
)
58 fn write_fmt(&mut self, fmt
: fmt
::Arguments
) -> io
::Result
<()> {
59 (**self).write_fmt(fmt
)
62 #[stable(feature = "rust1", since = "1.0.0")]
63 impl<'a
, S
: Seek
+ ?Sized
> Seek
for &'a
mut S
{
65 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> { (**self).seek(pos) }
67 #[stable(feature = "rust1", since = "1.0.0")]
68 impl<'a
, B
: BufRead
+ ?Sized
> BufRead
for &'a
mut B
{
70 fn fill_buf(&mut self) -> io
::Result
<&[u8]> { (**self).fill_buf() }
73 fn consume(&mut self, amt
: usize) { (**self).consume(amt) }
76 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
77 (**self).read_until(byte
, buf
)
81 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
82 (**self).read_line(buf
)
86 #[stable(feature = "rust1", since = "1.0.0")]
87 impl<R
: Read
+ ?Sized
> Read
for Box
<R
> {
89 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
94 fn read_to_end(&mut self, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
95 (**self).read_to_end(buf
)
99 fn read_to_string(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
100 (**self).read_to_string(buf
)
104 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
105 (**self).read_exact(buf
)
108 #[stable(feature = "rust1", since = "1.0.0")]
109 impl<W
: Write
+ ?Sized
> Write
for Box
<W
> {
111 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> { (**self).write(buf) }
114 fn flush(&mut self) -> io
::Result
<()> { (**self).flush() }
117 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
118 (**self).write_all(buf
)
122 fn write_fmt(&mut self, fmt
: fmt
::Arguments
) -> io
::Result
<()> {
123 (**self).write_fmt(fmt
)
126 #[stable(feature = "rust1", since = "1.0.0")]
127 impl<S
: Seek
+ ?Sized
> Seek
for Box
<S
> {
129 fn seek(&mut self, pos
: SeekFrom
) -> io
::Result
<u64> { (**self).seek(pos) }
131 #[stable(feature = "rust1", since = "1.0.0")]
132 impl<B
: BufRead
+ ?Sized
> BufRead
for Box
<B
> {
134 fn fill_buf(&mut self) -> io
::Result
<&[u8]> { (**self).fill_buf() }
137 fn consume(&mut self, amt
: usize) { (**self).consume(amt) }
140 fn read_until(&mut self, byte
: u8, buf
: &mut Vec
<u8>) -> io
::Result
<usize> {
141 (**self).read_until(byte
, buf
)
145 fn read_line(&mut self, buf
: &mut String
) -> io
::Result
<usize> {
146 (**self).read_line(buf
)
150 // =============================================================================
151 // In-memory buffer implementations
153 #[stable(feature = "rust1", since = "1.0.0")]
154 impl<'a
> Read
for &'a
[u8] {
156 fn read(&mut self, buf
: &mut [u8]) -> io
::Result
<usize> {
157 let amt
= cmp
::min(buf
.len(), self.len());
158 let (a
, b
) = self.split_at(amt
);
159 buf
.clone_from_slice(a
);
165 fn read_exact(&mut self, buf
: &mut [u8]) -> io
::Result
<()> {
166 if buf
.len() > self.len() {
167 return Err(Error
::new(ErrorKind
::UnexpectedEof
,
168 "failed to fill whole buffer"));
170 let (a
, b
) = self.split_at(buf
.len());
171 buf
.clone_from_slice(a
);
177 #[stable(feature = "rust1", since = "1.0.0")]
178 impl<'a
> BufRead
for &'a
[u8] {
180 fn fill_buf(&mut self) -> io
::Result
<&[u8]> { Ok(*self) }
183 fn consume(&mut self, amt
: usize) { *self = &self[amt..]; }
186 #[stable(feature = "rust1", since = "1.0.0")]
187 impl<'a
> Write
for &'a
mut [u8] {
189 fn write(&mut self, data
: &[u8]) -> io
::Result
<usize> {
190 let amt
= cmp
::min(data
.len(), self.len());
191 let (a
, b
) = mem
::replace(self, &mut []).split_at_mut(amt
);
192 a
.clone_from_slice(&data
[..amt
]);
198 fn write_all(&mut self, data
: &[u8]) -> io
::Result
<()> {
199 if try
!(self.write(data
)) == data
.len() {
202 Err(Error
::new(ErrorKind
::WriteZero
, "failed to write whole buffer"))
207 fn flush(&mut self) -> io
::Result
<()> { Ok(()) }
210 #[stable(feature = "rust1", since = "1.0.0")]
211 impl Write
for Vec
<u8> {
213 fn write(&mut self, buf
: &[u8]) -> io
::Result
<usize> {
214 self.extend_from_slice(buf
);
219 fn write_all(&mut self, buf
: &[u8]) -> io
::Result
<()> {
220 self.extend_from_slice(buf
);
225 fn flush(&mut self) -> io
::Result
<()> { Ok(()) }
235 fn bench_read_slice(b
: &mut test
::Bencher
) {
237 let mut dst
= [0; 128];
240 let mut rd
= &buf
[..];
242 let _
= rd
.read(&mut dst
);
243 test
::black_box(&dst
);
249 fn bench_write_slice(b
: &mut test
::Bencher
) {
250 let mut buf
= [0; 1024];
254 let mut wr
= &mut buf
[..];
256 let _
= wr
.write_all(&src
);
257 test
::black_box(&wr
);
263 fn bench_read_vec(b
: &mut test
::Bencher
) {
264 let buf
= vec
![5; 1024];
265 let mut dst
= [0; 128];
268 let mut rd
= &buf
[..];
270 let _
= rd
.read(&mut dst
);
271 test
::black_box(&dst
);
277 fn bench_write_vec(b
: &mut test
::Bencher
) {
278 let mut buf
= Vec
::with_capacity(1024);
282 let mut wr
= &mut buf
[..];
284 let _
= wr
.write_all(&src
);
285 test
::black_box(&wr
);