Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 1 | // 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 | |
| 11 | use core::io::{Reader, BytesReader}; |
| 12 | use core::io; |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 13 | |
| 14 | pub struct BufReader { |
| 15 | buf: ~[u8], |
Patrick Walton | 92d2ec4 | 2013-05-03 05:44:03 | [diff] [blame] | 16 | pos: @mut uint |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | pub impl BufReader { |
Patrick Walton | 4634f7e | 2013-03-22 02:07:54 | [diff] [blame] | 20 | pub fn new(v: ~[u8]) -> BufReader { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 21 | BufReader { |
Luqman Aden | 4cf51c2 | 2013-02-15 07:30:30 | [diff] [blame] | 22 | buf: v, |
Patrick Walton | 92d2ec4 | 2013-05-03 05:44:03 | [diff] [blame] | 23 | pos: @mut 0 |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
Ben Striegel | 0fed29c | 2013-03-08 02:11:09 | [diff] [blame] | 27 | priv fn as_bytes_reader<A>(&self, f: &fn(&BytesReader) -> A) -> A { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 28 | // Recreating the BytesReader state every call since |
| 29 | // I can't get the borrowing to work correctly |
| 30 | let bytes_reader = BytesReader { |
| 31 | bytes: ::core::util::id::<&[u8]>(self.buf), |
Patrick Walton | d12d255 | 2013-05-04 00:37:08 | [diff] [blame^] | 32 | pos: @mut *self.pos |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | let res = f(&bytes_reader); |
| 36 | |
| 37 | // FIXME #4429: This isn't correct if f fails |
Patrick Walton | d12d255 | 2013-05-04 00:37:08 | [diff] [blame^] | 38 | *self.pos = *bytes_reader.pos; |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 39 | |
Luqman Aden | 4cf51c2 | 2013-02-15 07:30:30 | [diff] [blame] | 40 | return res; |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 44 | impl Reader for BufReader { |
Ben Striegel | f08af9a | 2013-01-30 02:30:22 | [diff] [blame] | 45 | fn read(&self, bytes: &mut [u8], len: uint) -> uint { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 46 | self.as_bytes_reader(|r| r.read(bytes, len) ) |
| 47 | } |
| 48 | fn read_byte(&self) -> int { |
| 49 | self.as_bytes_reader(|r| r.read_byte() ) |
| 50 | } |
| 51 | fn eof(&self) -> bool { |
| 52 | self.as_bytes_reader(|r| r.eof() ) |
| 53 | } |
| 54 | fn seek(&self, offset: int, whence: io::SeekStyle) { |
| 55 | self.as_bytes_reader(|r| r.seek(offset, whence) ) |
| 56 | } |
| 57 | fn tell(&self) -> uint { |
| 58 | self.as_bytes_reader(|r| r.tell() ) |
| 59 | } |
| 60 | } |