blob: 91424ae3ba2c0d6a749352549c54717232d3a59c [file] [log] [blame]
Tim Chevalier02742922013-01-11 04:09:261// Copyright 2012 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
11use core::io::{Reader, BytesReader};
12use core::io;
Tim Chevalier02742922013-01-11 04:09:2613
Alex Crichton39568502013-05-29 03:11:4114/// An implementation of the io::Reader interface which reads a buffer of bytes
Tim Chevalier02742922013-01-11 04:09:2615pub struct BufReader {
Alex Crichton39568502013-05-29 03:11:4116 /// The buffer of bytes to read
Tim Chevalier02742922013-01-11 04:09:2617 buf: ~[u8],
Alex Crichton39568502013-05-29 03:11:4118 /// The current position in the buffer of bytes
Patrick Walton92d2ec42013-05-03 05:44:0319 pos: @mut uint
Tim Chevalier02742922013-01-11 04:09:2620}
21
Alex Crichton39568502013-05-29 03:11:4122impl BufReader {
23 /// Creates a new buffer reader for the specified buffer
Patrick Walton4634f7e2013-03-22 02:07:5424 pub fn new(v: ~[u8]) -> BufReader {
Tim Chevalier02742922013-01-11 04:09:2625 BufReader {
Luqman Aden4cf51c22013-02-15 07:30:3026 buf: v,
Patrick Walton92d2ec42013-05-03 05:44:0327 pos: @mut 0
Tim Chevalier02742922013-01-11 04:09:2628 }
29 }
30
Alex Crichton39568502013-05-29 03:11:4131 fn as_bytes_reader<A>(&self, f: &fn(&BytesReader) -> A) -> A {
Tim Chevalier02742922013-01-11 04:09:2632 // Recreating the BytesReader state every call since
33 // I can't get the borrowing to work correctly
34 let bytes_reader = BytesReader {
35 bytes: ::core::util::id::<&[u8]>(self.buf),
Patrick Waltond12d2552013-05-04 00:37:0836 pos: @mut *self.pos
Tim Chevalier02742922013-01-11 04:09:2637 };
38
39 let res = f(&bytes_reader);
40
41 // FIXME #4429: This isn't correct if f fails
Patrick Waltond12d2552013-05-04 00:37:0842 *self.pos = *bytes_reader.pos;
Tim Chevalier02742922013-01-11 04:09:2643
Luqman Aden4cf51c22013-02-15 07:30:3044 return res;
Tim Chevalier02742922013-01-11 04:09:2645 }
46}
47
Patrick Walton91436882013-02-14 19:47:0048impl Reader for BufReader {
Ben Striegelf08af9a2013-01-30 02:30:2249 fn read(&self, bytes: &mut [u8], len: uint) -> uint {
Tim Chevalier02742922013-01-11 04:09:2650 self.as_bytes_reader(|r| r.read(bytes, len) )
51 }
52 fn read_byte(&self) -> int {
53 self.as_bytes_reader(|r| r.read_byte() )
54 }
55 fn eof(&self) -> bool {
56 self.as_bytes_reader(|r| r.eof() )
57 }
58 fn seek(&self, offset: int, whence: io::SeekStyle) {
59 self.as_bytes_reader(|r| r.seek(offset, whence) )
60 }
61 fn tell(&self) -> uint {
62 self.as_bytes_reader(|r| r.tell() )
63 }
64}