]> git.proxmox.com Git - rustc.git/blame - src/libstd/io/impls.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libstd / io / impls.rs
CommitLineData
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
SL
11use boxed::Box;
12use cmp;
c34b1796
AL
13use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
14use fmt;
85aaf69f 15use mem;
c34b1796 16use string::String;
85aaf69f
SL
17use vec::Vec;
18
19// =============================================================================
20// Forwarding implementations
21
c34b1796 22#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 23impl<'a, R: Read + ?Sized> Read for &'a mut R {
c34b1796
AL
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 }
e9174d1e
SL
38
39 #[inline]
40 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
41 (**self).read_exact(buf)
42 }
85aaf69f 43}
c34b1796 44#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 45impl<'a, W: Write + ?Sized> Write for &'a mut W {
c34b1796 46 #[inline]
85aaf69f 47 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
c34b1796
AL
48
49 #[inline]
85aaf69f 50 fn flush(&mut self) -> io::Result<()> { (**self).flush() }
c34b1796
AL
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 }
85aaf69f 61}
c34b1796 62#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 63impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
c34b1796 64 #[inline]
85aaf69f
SL
65 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
66}
c34b1796 67#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 68impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
c34b1796 69 #[inline]
85aaf69f 70 fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
c34b1796
AL
71
72 #[inline]
85aaf69f 73 fn consume(&mut self, amt: usize) { (**self).consume(amt) }
c34b1796
AL
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 }
85aaf69f
SL
84}
85
c34b1796 86#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 87impl<R: Read + ?Sized> Read for Box<R> {
c34b1796
AL
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 }
e9174d1e
SL
102
103 #[inline]
104 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
105 (**self).read_exact(buf)
106 }
85aaf69f 107}
c34b1796 108#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 109impl<W: Write + ?Sized> Write for Box<W> {
c34b1796 110 #[inline]
85aaf69f 111 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
c34b1796
AL
112
113 #[inline]
85aaf69f 114 fn flush(&mut self) -> io::Result<()> { (**self).flush() }
c34b1796
AL
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 }
85aaf69f 125}
c34b1796 126#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 127impl<S: Seek + ?Sized> Seek for Box<S> {
c34b1796 128 #[inline]
85aaf69f
SL
129 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
130}
c34b1796 131#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 132impl<B: BufRead + ?Sized> BufRead for Box<B> {
c34b1796 133 #[inline]
85aaf69f 134 fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
c34b1796
AL
135
136 #[inline]
85aaf69f 137 fn consume(&mut self, amt: usize) { (**self).consume(amt) }
c34b1796
AL
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 }
85aaf69f
SL
148}
149
150// =============================================================================
151// In-memory buffer implementations
152
c34b1796 153#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 154impl<'a> Read for &'a [u8] {
c34b1796 155 #[inline]
85aaf69f
SL
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);
7453a54e 159 buf[..amt].copy_from_slice(a);
85aaf69f
SL
160 *self = b;
161 Ok(amt)
162 }
e9174d1e
SL
163
164 #[inline]
165 fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
166 if buf.len() > self.len() {
92a42be0
SL
167 return Err(Error::new(ErrorKind::UnexpectedEof,
168 "failed to fill whole buffer"));
e9174d1e
SL
169 }
170 let (a, b) = self.split_at(buf.len());
7453a54e 171 buf.copy_from_slice(a);
e9174d1e
SL
172 *self = b;
173 Ok(())
174 }
85aaf69f
SL
175}
176
c34b1796 177#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 178impl<'a> BufRead for &'a [u8] {
c34b1796 179 #[inline]
85aaf69f 180 fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) }
c34b1796
AL
181
182 #[inline]
85aaf69f
SL
183 fn consume(&mut self, amt: usize) { *self = &self[amt..]; }
184}
185
c34b1796 186#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 187impl<'a> Write for &'a mut [u8] {
c34b1796 188 #[inline]
85aaf69f
SL
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);
7453a54e 192 a.copy_from_slice(&data[..amt]);
85aaf69f
SL
193 *self = b;
194 Ok(amt)
195 }
c34b1796
AL
196
197 #[inline]
198 fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
54a0048b 199 if self.write(data)? == data.len() {
c34b1796
AL
200 Ok(())
201 } else {
202 Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer"))
203 }
204 }
205
206 #[inline]
85aaf69f
SL
207 fn flush(&mut self) -> io::Result<()> { Ok(()) }
208}
209
c34b1796 210#[stable(feature = "rust1", since = "1.0.0")]
85aaf69f 211impl Write for Vec<u8> {
c34b1796 212 #[inline]
85aaf69f 213 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
92a42be0 214 self.extend_from_slice(buf);
85aaf69f
SL
215 Ok(buf.len())
216 }
c34b1796
AL
217
218 #[inline]
219 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
92a42be0 220 self.extend_from_slice(buf);
c34b1796
AL
221 Ok(())
222 }
223
224 #[inline]
85aaf69f
SL
225 fn flush(&mut self) -> io::Result<()> { Ok(()) }
226}
c34b1796
AL
227
228#[cfg(test)]
229mod 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[..];
b039eaaf 241 for _ in 0..8 {
c34b1796
AL
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[..];
b039eaaf 255 for _ in 0..8 {
c34b1796
AL
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[..];
b039eaaf 269 for _ in 0..8 {
c34b1796
AL
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[..];
b039eaaf 283 for _ in 0..8 {
c34b1796
AL
284 let _ = wr.write_all(&src);
285 test::black_box(&wr);
286 }
287 })
288 }
289}