(index<- ) ./libstd/rt/io/mem.rs
1 // Copyright 2013 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 //! Readers and Writers for in-memory buffers
12 //!
13 //! # XXX
14 //!
15 //! * Should probably have something like this for strings.
16 //! * Should they implement Closable? Would take extra state.
17
18 use cmp::min;
19 use prelude::*;
20 use super::*;
21 use vec;
22
23 /// Writes to an owned, growable byte vector
24 pub struct MemWriter {
25 priv buf: ~[u8]
26 }
27
28 impl MemWriter {
29 pub fn new() -> MemWriter { MemWriter { buf: vec::with_capacity(128) } }
30 }
31
32 impl Writer for MemWriter {
33 fn write(&mut self, buf: &[u8]) {
34 self.buf.push_all(buf)
35 }
36
37 fn flush(&mut self) { /* no-op */ }
38 }
39
40 impl Seek for MemWriter {
41 fn tell(&self) -> u64 { self.buf.len() as u64 }
42
43 fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() }
44 }
45
46 impl Decorator<~[u8]> for MemWriter {
47
48 fn inner(self) -> ~[u8] {
49 match self {
50 MemWriter { buf: buf } => buf
51 }
52 }
53
54 fn inner_ref<'a>(&'a self) -> &'a ~[u8] {
55 match *self {
56 MemWriter { buf: ref buf } => buf
57 }
58 }
59
60 fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] {
61 match *self {
62 MemWriter { buf: ref mut buf } => buf
63 }
64 }
65 }
66
67 /// Reads from an owned byte vector
68 pub struct MemReader {
69 priv buf: ~[u8],
70 priv pos: uint
71 }
72
73 impl MemReader {
74 pub fn new(buf: ~[u8]) -> MemReader {
75 MemReader {
76 buf: buf,
77 pos: 0
78 }
79 }
80 }
81
82 impl Reader for MemReader {
83 fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
84 { if self.eof() { return None; } }
85
86 let write_len = min(buf.len(), self.buf.len() - self.pos);
87 {
88 let input = self.buf.slice(self.pos, self.pos + write_len);
89 let output = buf.mut_slice(0, write_len);
90 assert_eq!(input.len(), output.len());
91 vec::bytes::copy_memory(output, input, write_len);
92 }
93 self.pos += write_len;
94 assert!(self.pos <= self.buf.len());
95
96 return Some(write_len);
97 }
98
99 fn eof(&mut self) -> bool { self.pos == self.buf.len() }
100 }
101
102 impl Seek for MemReader {
103 fn tell(&self) -> u64 { self.pos as u64 }
104
105 fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() }
106 }
107
108 impl Decorator<~[u8]> for MemReader {
109
110 fn inner(self) -> ~[u8] {
111 match self {
112 MemReader { buf: buf, _ } => buf
113 }
114 }
115
116 fn inner_ref<'a>(&'a self) -> &'a ~[u8] {
117 match *self {
118 MemReader { buf: ref buf, _ } => buf
119 }
120 }
121
122 fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] {
123 match *self {
124 MemReader { buf: ref mut buf, _ } => buf
125 }
126 }
127 }
128
129
130 /// Writes to a fixed-size byte slice
131 pub struct BufWriter<'self> {
132 priv buf: &'self mut [u8],
133 priv pos: uint
134 }
135
136 impl<'self> BufWriter<'self> {
137 pub fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> {
138 BufWriter {
139 buf: buf,
140 pos: 0
141 }
142 }
143 }
144
145 impl<'self> Writer for BufWriter<'self> {
146 fn write(&mut self, _buf: &[u8]) { fail2!() }
147
148 fn flush(&mut self) { fail2!() }
149 }
150
151 impl<'self> Seek for BufWriter<'self> {
152 fn tell(&self) -> u64 { fail2!() }
153
154 fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() }
155 }
156
157
158 /// Reads from a fixed-size byte slice
159 pub struct BufReader<'self> {
160 priv buf: &'self [u8],
161 priv pos: uint
162 }
163
164 impl<'self> BufReader<'self> {
165 pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
166 BufReader {
167 buf: buf,
168 pos: 0
169 }
170 }
171 }
172
173 impl<'self> Reader for BufReader<'self> {
174 fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
175 { if self.eof() { return None; } }
176
177 let write_len = min(buf.len(), self.buf.len() - self.pos);
178 {
179 let input = self.buf.slice(self.pos, self.pos + write_len);
180 let output = buf.mut_slice(0, write_len);
181 assert_eq!(input.len(), output.len());
182 vec::bytes::copy_memory(output, input, write_len);
183 }
184 self.pos += write_len;
185 assert!(self.pos <= self.buf.len());
186
187 return Some(write_len);
188 }
189
190 fn eof(&mut self) -> bool { self.pos == self.buf.len() }
191 }
192
193 impl<'self> Seek for BufReader<'self> {
194 fn tell(&self) -> u64 { self.pos as u64 }
195
196 fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail2!() }
197 }
198
199 ///Calls a function with a MemWriter and returns
200 ///the writer's stored vector.
201 pub fn with_mem_writer(writeFn:&fn(&mut MemWriter)) -> ~[u8] {
202 let mut writer = MemWriter::new();
203 writeFn(&mut writer);
204 writer.inner()
205 }
206
207 #[cfg(test)]
208 mod test {
209 use prelude::*;
210 use super::*;
211
212 #[test]
213 fn test_mem_writer() {
214 let mut writer = MemWriter::new();
215 assert_eq!(writer.tell(), 0);
216 writer.write([0]);
217 assert_eq!(writer.tell(), 1);
218 writer.write([1, 2, 3]);
219 writer.write([4, 5, 6, 7]);
220 assert_eq!(writer.tell(), 8);
221 assert_eq!(writer.inner(), ~[0, 1, 2, 3, 4, 5 , 6, 7]);
222 }
223
224 #[test]
225 fn test_mem_reader() {
226 let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
227 let mut buf = [];
228 assert_eq!(reader.read(buf), Some(0));
229 assert_eq!(reader.tell(), 0);
230 let mut buf = [0];
231 assert_eq!(reader.read(buf), Some(1));
232 assert_eq!(reader.tell(), 1);
233 assert_eq!(buf, [0]);
234 let mut buf = [0, ..4];
235 assert_eq!(reader.read(buf), Some(4));
236 assert_eq!(reader.tell(), 5);
237 assert_eq!(buf, [1, 2, 3, 4]);
238 assert_eq!(reader.read(buf), Some(3));
239 assert_eq!(buf.slice(0, 3), [5, 6, 7]);
240 assert!(reader.eof());
241 assert_eq!(reader.read(buf), None);
242 assert!(reader.eof());
243 }
244
245 #[test]
246 fn test_buf_reader() {
247 let in_buf = ~[0, 1, 2, 3, 4, 5, 6, 7];
248 let mut reader = BufReader::new(in_buf);
249 let mut buf = [];
250 assert_eq!(reader.read(buf), Some(0));
251 assert_eq!(reader.tell(), 0);
252 let mut buf = [0];
253 assert_eq!(reader.read(buf), Some(1));
254 assert_eq!(reader.tell(), 1);
255 assert_eq!(buf, [0]);
256 let mut buf = [0, ..4];
257 assert_eq!(reader.read(buf), Some(4));
258 assert_eq!(reader.tell(), 5);
259 assert_eq!(buf, [1, 2, 3, 4]);
260 assert_eq!(reader.read(buf), Some(3));
261 assert_eq!(buf.slice(0, 3), [5, 6, 7]);
262 assert!(reader.eof());
263 assert_eq!(reader.read(buf), None);
264 assert!(reader.eof());
265 }
266
267 #[test]
268 fn test_with_mem_writer() {
269 let buf = with_mem_writer(|wr| wr.write([1,2,3,4,5,6,7]));
270 assert_eq!(buf, ~[1,2,3,4,5,6,7]);
271 }
272 }