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 | |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 14 | /// An implementation of the io::Reader interface which reads a buffer of bytes |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 15 | pub struct BufReader { |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 16 | /// The buffer of bytes to read |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 17 | buf: ~[u8], |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 18 | /// The current position in the buffer of bytes |
Patrick Walton | 92d2ec4 | 2013-05-03 05:44:03 | [diff] [blame] | 19 | pos: @mut uint |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 20 | } |
| 21 | |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 22 | impl BufReader { |
| 23 | /// Creates a new buffer reader for the specified buffer |
Patrick Walton | 4634f7e | 2013-03-22 02:07:54 | [diff] [blame] | 24 | pub fn new(v: ~[u8]) -> BufReader { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 25 | BufReader { |
Luqman Aden | 4cf51c2 | 2013-02-15 07:30:30 | [diff] [blame] | 26 | buf: v, |
Patrick Walton | 92d2ec4 | 2013-05-03 05:44:03 | [diff] [blame] | 27 | pos: @mut 0 |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 28 | } |
| 29 | } |
| 30 | |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 31 | fn as_bytes_reader<A>(&self, f: &fn(&BytesReader) -> A) -> A { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 32 | // 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 Walton | d12d255 | 2013-05-04 00:37:08 | [diff] [blame] | 36 | pos: @mut *self.pos |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | let res = f(&bytes_reader); |
| 40 | |
| 41 | // FIXME #4429: This isn't correct if f fails |
Patrick Walton | d12d255 | 2013-05-04 00:37:08 | [diff] [blame] | 42 | *self.pos = *bytes_reader.pos; |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 43 | |
Luqman Aden | 4cf51c2 | 2013-02-15 07:30:30 | [diff] [blame] | 44 | return res; |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
Patrick Walton | 9143688 | 2013-02-14 19:47:00 | [diff] [blame] | 48 | impl Reader for BufReader { |
Ben Striegel | f08af9a | 2013-01-30 02:30:22 | [diff] [blame] | 49 | fn read(&self, bytes: &mut [u8], len: uint) -> uint { |
Tim Chevalier | 0274292 | 2013-01-11 04:09:26 | [diff] [blame] | 50 | 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 | } |