]>
Commit | Line | Data |
---|---|---|
85aaf69f SL |
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. | |
4 | // | |
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. | |
10 | ||
85aaf69f | 11 | use cmp; |
c34b1796 AL |
12 | use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind}; |
13 | use fmt; | |
85aaf69f | 14 | use mem; |
85aaf69f SL |
15 | |
16 | // ============================================================================= | |
17 | // Forwarding implementations | |
18 | ||
c34b1796 | 19 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 20 | impl<'a, R: Read + ?Sized> Read for &'a mut R { |
c34b1796 AL |
21 | #[inline] |
22 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | |
23 | (**self).read(buf) | |
24 | } | |
25 | ||
26 | #[inline] | |
27 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { | |
28 | (**self).read_to_end(buf) | |
29 | } | |
30 | ||
31 | #[inline] | |
32 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { | |
33 | (**self).read_to_string(buf) | |
34 | } | |
e9174d1e SL |
35 | |
36 | #[inline] | |
37 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | |
38 | (**self).read_exact(buf) | |
39 | } | |
85aaf69f | 40 | } |
c34b1796 | 41 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 42 | impl<'a, W: Write + ?Sized> Write for &'a mut W { |
c34b1796 | 43 | #[inline] |
85aaf69f | 44 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) } |
c34b1796 AL |
45 | |
46 | #[inline] | |
85aaf69f | 47 | fn flush(&mut self) -> io::Result<()> { (**self).flush() } |
c34b1796 AL |
48 | |
49 | #[inline] | |
50 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | |
51 | (**self).write_all(buf) | |
52 | } | |
53 | ||
54 | #[inline] | |
55 | fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> { | |
56 | (**self).write_fmt(fmt) | |
57 | } | |
85aaf69f | 58 | } |
c34b1796 | 59 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 60 | impl<'a, S: Seek + ?Sized> Seek for &'a mut S { |
c34b1796 | 61 | #[inline] |
85aaf69f SL |
62 | fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) } |
63 | } | |
c34b1796 | 64 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 65 | impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B { |
c34b1796 | 66 | #[inline] |
85aaf69f | 67 | fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } |
c34b1796 AL |
68 | |
69 | #[inline] | |
85aaf69f | 70 | fn consume(&mut self, amt: usize) { (**self).consume(amt) } |
c34b1796 AL |
71 | |
72 | #[inline] | |
73 | fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { | |
74 | (**self).read_until(byte, buf) | |
75 | } | |
76 | ||
77 | #[inline] | |
78 | fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { | |
79 | (**self).read_line(buf) | |
80 | } | |
85aaf69f SL |
81 | } |
82 | ||
c34b1796 | 83 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 84 | impl<R: Read + ?Sized> Read for Box<R> { |
c34b1796 AL |
85 | #[inline] |
86 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { | |
87 | (**self).read(buf) | |
88 | } | |
89 | ||
90 | #[inline] | |
91 | fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> { | |
92 | (**self).read_to_end(buf) | |
93 | } | |
94 | ||
95 | #[inline] | |
96 | fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> { | |
97 | (**self).read_to_string(buf) | |
98 | } | |
e9174d1e SL |
99 | |
100 | #[inline] | |
101 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | |
102 | (**self).read_exact(buf) | |
103 | } | |
85aaf69f | 104 | } |
c34b1796 | 105 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 106 | impl<W: Write + ?Sized> Write for Box<W> { |
c34b1796 | 107 | #[inline] |
85aaf69f | 108 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) } |
c34b1796 AL |
109 | |
110 | #[inline] | |
85aaf69f | 111 | fn flush(&mut self) -> io::Result<()> { (**self).flush() } |
c34b1796 AL |
112 | |
113 | #[inline] | |
114 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | |
115 | (**self).write_all(buf) | |
116 | } | |
117 | ||
118 | #[inline] | |
119 | fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> { | |
120 | (**self).write_fmt(fmt) | |
121 | } | |
85aaf69f | 122 | } |
c34b1796 | 123 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 124 | impl<S: Seek + ?Sized> Seek for Box<S> { |
c34b1796 | 125 | #[inline] |
85aaf69f SL |
126 | fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) } |
127 | } | |
c34b1796 | 128 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 129 | impl<B: BufRead + ?Sized> BufRead for Box<B> { |
c34b1796 | 130 | #[inline] |
85aaf69f | 131 | fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() } |
c34b1796 AL |
132 | |
133 | #[inline] | |
85aaf69f | 134 | fn consume(&mut self, amt: usize) { (**self).consume(amt) } |
c34b1796 AL |
135 | |
136 | #[inline] | |
137 | fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> { | |
138 | (**self).read_until(byte, buf) | |
139 | } | |
140 | ||
141 | #[inline] | |
142 | fn read_line(&mut self, buf: &mut String) -> io::Result<usize> { | |
143 | (**self).read_line(buf) | |
144 | } | |
85aaf69f SL |
145 | } |
146 | ||
147 | // ============================================================================= | |
148 | // In-memory buffer implementations | |
149 | ||
c34b1796 | 150 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 151 | impl<'a> Read for &'a [u8] { |
c34b1796 | 152 | #[inline] |
85aaf69f SL |
153 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
154 | let amt = cmp::min(buf.len(), self.len()); | |
155 | let (a, b) = self.split_at(amt); | |
7453a54e | 156 | buf[..amt].copy_from_slice(a); |
85aaf69f SL |
157 | *self = b; |
158 | Ok(amt) | |
159 | } | |
e9174d1e SL |
160 | |
161 | #[inline] | |
162 | fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | |
163 | if buf.len() > self.len() { | |
92a42be0 SL |
164 | return Err(Error::new(ErrorKind::UnexpectedEof, |
165 | "failed to fill whole buffer")); | |
e9174d1e SL |
166 | } |
167 | let (a, b) = self.split_at(buf.len()); | |
7453a54e | 168 | buf.copy_from_slice(a); |
e9174d1e SL |
169 | *self = b; |
170 | Ok(()) | |
171 | } | |
85aaf69f SL |
172 | } |
173 | ||
c34b1796 | 174 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 175 | impl<'a> BufRead for &'a [u8] { |
c34b1796 | 176 | #[inline] |
85aaf69f | 177 | fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) } |
c34b1796 AL |
178 | |
179 | #[inline] | |
85aaf69f SL |
180 | fn consume(&mut self, amt: usize) { *self = &self[amt..]; } |
181 | } | |
182 | ||
c34b1796 | 183 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 184 | impl<'a> Write for &'a mut [u8] { |
c34b1796 | 185 | #[inline] |
85aaf69f SL |
186 | fn write(&mut self, data: &[u8]) -> io::Result<usize> { |
187 | let amt = cmp::min(data.len(), self.len()); | |
188 | let (a, b) = mem::replace(self, &mut []).split_at_mut(amt); | |
7453a54e | 189 | a.copy_from_slice(&data[..amt]); |
85aaf69f SL |
190 | *self = b; |
191 | Ok(amt) | |
192 | } | |
c34b1796 AL |
193 | |
194 | #[inline] | |
195 | fn write_all(&mut self, data: &[u8]) -> io::Result<()> { | |
54a0048b | 196 | if self.write(data)? == data.len() { |
c34b1796 AL |
197 | Ok(()) |
198 | } else { | |
199 | Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer")) | |
200 | } | |
201 | } | |
202 | ||
203 | #[inline] | |
85aaf69f SL |
204 | fn flush(&mut self) -> io::Result<()> { Ok(()) } |
205 | } | |
206 | ||
c34b1796 | 207 | #[stable(feature = "rust1", since = "1.0.0")] |
85aaf69f | 208 | impl Write for Vec<u8> { |
c34b1796 | 209 | #[inline] |
85aaf69f | 210 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
92a42be0 | 211 | self.extend_from_slice(buf); |
85aaf69f SL |
212 | Ok(buf.len()) |
213 | } | |
c34b1796 AL |
214 | |
215 | #[inline] | |
216 | fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { | |
92a42be0 | 217 | self.extend_from_slice(buf); |
c34b1796 AL |
218 | Ok(()) |
219 | } | |
220 | ||
221 | #[inline] | |
85aaf69f SL |
222 | fn flush(&mut self) -> io::Result<()> { Ok(()) } |
223 | } | |
c34b1796 AL |
224 | |
225 | #[cfg(test)] | |
226 | mod tests { | |
227 | use io::prelude::*; | |
c34b1796 AL |
228 | use test; |
229 | ||
230 | #[bench] | |
231 | fn bench_read_slice(b: &mut test::Bencher) { | |
232 | let buf = [5; 1024]; | |
233 | let mut dst = [0; 128]; | |
234 | ||
235 | b.iter(|| { | |
236 | let mut rd = &buf[..]; | |
b039eaaf | 237 | for _ in 0..8 { |
c34b1796 AL |
238 | let _ = rd.read(&mut dst); |
239 | test::black_box(&dst); | |
240 | } | |
241 | }) | |
242 | } | |
243 | ||
244 | #[bench] | |
245 | fn bench_write_slice(b: &mut test::Bencher) { | |
246 | let mut buf = [0; 1024]; | |
247 | let src = [5; 128]; | |
248 | ||
249 | b.iter(|| { | |
250 | let mut wr = &mut buf[..]; | |
b039eaaf | 251 | for _ in 0..8 { |
c34b1796 AL |
252 | let _ = wr.write_all(&src); |
253 | test::black_box(&wr); | |
254 | } | |
255 | }) | |
256 | } | |
257 | ||
258 | #[bench] | |
259 | fn bench_read_vec(b: &mut test::Bencher) { | |
260 | let buf = vec![5; 1024]; | |
261 | let mut dst = [0; 128]; | |
262 | ||
263 | b.iter(|| { | |
264 | let mut rd = &buf[..]; | |
b039eaaf | 265 | for _ in 0..8 { |
c34b1796 AL |
266 | let _ = rd.read(&mut dst); |
267 | test::black_box(&dst); | |
268 | } | |
269 | }) | |
270 | } | |
271 | ||
272 | #[bench] | |
273 | fn bench_write_vec(b: &mut test::Bencher) { | |
274 | let mut buf = Vec::with_capacity(1024); | |
275 | let src = [5; 128]; | |
276 | ||
277 | b.iter(|| { | |
278 | let mut wr = &mut buf[..]; | |
b039eaaf | 279 | for _ in 0..8 { |
c34b1796 AL |
280 | let _ = wr.write_all(&src); |
281 | test::black_box(&wr); | |
282 | } | |
283 | }) | |
284 | } | |
285 | } |