]> git.lizzy.rs Git - rust.git/blob - src/libstd/io/impls.rs
Fix spelling errors in comments.
[rust.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 core::prelude::*;
12
13 use boxed::Box;
14 use cmp;
15 use io::{self, SeekFrom, Read, Write, Seek, BufRead, Error, ErrorKind};
16 use fmt;
17 use mem;
18 use slice;
19 use string::String;
20 use vec::Vec;
21
22 // =============================================================================
23 // Forwarding implementations
24
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> {
28         (**self).read(buf)
29     }
30     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
31         (**self).read_to_end(buf)
32     }
33     fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
34         (**self).read_to_string(buf)
35     }
36 }
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)
43     }
44     fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
45         (**self).write_fmt(fmt)
46     }
47 }
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) }
51 }
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)
58     }
59     fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
60         (**self).read_line(buf)
61     }
62 }
63
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> {
67         (**self).read(buf)
68     }
69     fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
70         (**self).read_to_end(buf)
71     }
72     fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
73         (**self).read_to_string(buf)
74     }
75 }
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)
82     }
83     fn write_fmt(&mut self, fmt: fmt::Arguments) -> io::Result<()> {
84         (**self).write_fmt(fmt)
85     }
86 }
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) }
90 }
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)
97     }
98     fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
99         (**self).read_line(buf)
100     }
101 }
102
103 // =============================================================================
104 // In-memory buffer implementations
105
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);
112         *self = b;
113         Ok(amt)
114     }
115 }
116
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..]; }
121 }
122
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]);
129         *self = b;
130         Ok(amt)
131     }
132
133     fn write_all(&mut self, data: &[u8]) -> io::Result<()> {
134         if try!(self.write(data)) == data.len() {
135             Ok(())
136         } else {
137             Err(Error::new(ErrorKind::WriteZero, "failed to write whole buffer", None))
138         }
139     }
140
141     fn flush(&mut self) -> io::Result<()> { Ok(()) }
142 }
143
144 #[stable(feature = "rust1", since = "1.0.0")]
145 impl Write for Vec<u8> {
146     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
147         self.push_all(buf);
148         Ok(buf.len())
149     }
150
151     fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
152         self.push_all(buf);
153         Ok(())
154     }
155
156     fn flush(&mut self) -> io::Result<()> { Ok(()) }
157 }