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