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.
15 use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
22 // =============================================================================
23 // Forwarding implementations
25 #[stable(feature = "rust1", since = "1.0.0")]
26 impl<'a, R: Read + ?Sized> Read for &'a mut R {
27 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)
33 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
34 (**self).read_to_string(buf)
37 #[stable(feature = "rust1", since = "1.0.0")]
38 impl<'a, W: Write + ?Sized> Write for &'a mut W {
39 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
40 fn flush(&mut self) -> io::Result<()> { (**self).flush() }
41 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
42 (**self).write_all(buf)
44 fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
45 (**self).write_fmt(fmt)
48 #[stable(feature = "rust1", since = "1.0.0")]
49 impl<'a, S: Seek + ?Sized> Seek for &'a mut S {
50 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
52 #[stable(feature = "rust1", since = "1.0.0")]
53 impl<'a, B: BufRead + ?Sized> BufRead for &'a mut B {
54 fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
55 fn consume(&mut self, amt: usize) { (**self).consume(amt) }
56 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
57 (**self).read_until(byte, buf)
59 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
60 (**self).read_line(buf)
64 #[stable(feature = "rust1", since = "1.0.0")]
65 impl<R: Read + ?Sized> Read for Box<R> {
66 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
69 fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
70 (**self).read_to_end(buf)
72 fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
73 (**self).read_to_string(buf)
76 #[stable(feature = "rust1", since = "1.0.0")]
77 impl<W: Write + ?Sized> Write for Box<W> {
78 fn write(&mut self, buf: &[u8]) -> io::Result<usize> { (**self).write(buf) }
79 fn flush(&mut self) -> io::Result<()> { (**self).flush() }
80 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
81 (**self).write_all(buf)
83 fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
84 (**self).write_fmt(fmt)
87 #[stable(feature = "rust1", since = "1.0.0")]
88 impl<S: Seek + ?Sized> Seek for Box<S> {
89 fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { (**self).seek(pos) }
91 #[stable(feature = "rust1", since = "1.0.0")]
92 impl<B: BufRead + ?Sized> BufRead for Box<B> {
93 fn fill_buf(&mut self) -> io::Result<&[u8]> { (**self).fill_buf() }
94 fn consume(&mut self, amt: usize) { (**self).consume(amt) }
95 fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
96 (**self).read_until(byte, buf)
98 fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
99 (**self).read_line(buf)
103 // =============================================================================
104 // In-memory buffer implementations
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a> Read for &'a [u8] {
108 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
109 let amt = cmp::min(buf.len(), self.len());
110 let (a, b) = self.split_at(amt);
111 slice::bytes::copy_memory(buf, a);
117 #[stable(feature = "rust1", since = "1.0.0")]
118 impl<'a> BufRead for &'a [u8] {
119 fn fill_buf(&mut self) -> io::Result<&[u8]> { Ok(*self) }
120 fn consume(&mut self, amt: usize) { *self = &self[amt..]; }
123 #[stable(feature = "rust1", since = "1.0.0")]
124 impl<'a> Write for &'a mut [u8] {
125 fn write(&mut self, data: &[u8]) -> io::Result<usize> {
126 let amt = cmp::min(data.len(), self.len());
127 let (a, b) = mem::replace(self, &mut []).split_at_mut(amt);
128 slice::bytes::copy_memory(a, &data[..amt]);
133 fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
134 if try!(self.write(data)) == data.len() {
137 Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer", None))
141 fn flush(&mut self) -> io::Result<()> { Ok(()) }
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl Write for Vec<u8> {
146 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
151 fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
156 fn flush(&mut self) -> io::Result<()> { Ok(()) }