2013-04-17 19:55:21 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! Readers and Writers for in-memory buffers
|
|
|
|
//!
|
2013-04-19 14:04:19 -05:00
|
|
|
//! # XXX
|
2013-04-17 19:55:21 -05:00
|
|
|
//!
|
|
|
|
//! * Should probably have something like this for strings.
|
|
|
|
//! * Should they implement Closable? Would take extra state.
|
|
|
|
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-04-20 19:25:00 -05:00
|
|
|
use cmp::min;
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
/// Writes to an owned, growable byte vector
|
|
|
|
pub struct MemWriter {
|
|
|
|
buf: ~[u8]
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MemWriter {
|
|
|
|
pub fn new() -> MemWriter { MemWriter { buf: ~[] } }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for MemWriter {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn write(&mut self, buf: &[u8]) {
|
|
|
|
self.buf.push_all(buf)
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
fn flush(&mut self) { /* no-op */ }
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemWriter {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn tell(&self) -> u64 { self.buf.len() as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decorator<~[u8]> for MemWriter {
|
|
|
|
|
|
|
|
fn inner(self) -> ~[u8] {
|
|
|
|
match self {
|
|
|
|
MemWriter { buf: buf } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_ref<'a>(&'a self) -> &'a ~[u8] {
|
|
|
|
match *self {
|
|
|
|
MemWriter { buf: ref buf } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] {
|
|
|
|
match *self {
|
|
|
|
MemWriter { buf: ref mut buf } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from an owned byte vector
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemReader {
|
|
|
|
buf: ~[u8],
|
|
|
|
pos: uint
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MemReader {
|
|
|
|
pub fn new(buf: ~[u8]) -> MemReader {
|
|
|
|
MemReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
|
|
|
|
{ if self.eof() { return None; } }
|
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
|
|
|
let input = self.buf.slice(self.pos, self.pos + write_len);
|
|
|
|
let output = vec::mut_slice(buf, 0, write_len);
|
|
|
|
assert!(input.len() == output.len());
|
|
|
|
vec::bytes::copy_memory(output, input, write_len);
|
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
return Some(write_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eof(&mut self) -> bool { self.pos == self.buf.len() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemReader {
|
2013-04-20 19:25:00 -05:00
|
|
|
fn tell(&self) -> u64 { self.pos as u64 }
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decorator<~[u8]> for MemReader {
|
|
|
|
|
|
|
|
fn inner(self) -> ~[u8] {
|
|
|
|
match self {
|
|
|
|
MemReader { buf: buf, _ } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_ref<'a>(&'a self) -> &'a ~[u8] {
|
|
|
|
match *self {
|
|
|
|
MemReader { buf: ref buf, _ } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] {
|
|
|
|
match *self {
|
|
|
|
MemReader { buf: ref mut buf, _ } => buf
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
/// Writes to a fixed-size byte slice
|
|
|
|
struct BufWriter<'self> {
|
|
|
|
buf: &'self mut [u8],
|
|
|
|
pos: uint
|
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
impl<'self> BufWriter<'self> {
|
|
|
|
pub fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> {
|
|
|
|
BufWriter {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Writer for BufWriter<'self> {
|
|
|
|
fn write(&mut self, _buf: &[u8]) { fail!() }
|
|
|
|
|
|
|
|
fn flush(&mut self) { fail!() }
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl<'self> Seek for BufWriter<'self> {
|
2013-04-17 19:55:21 -05:00
|
|
|
fn tell(&self) -> u64 { fail!() }
|
|
|
|
|
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from a fixed-size byte slice
|
2013-04-17 19:55:21 -05:00
|
|
|
struct BufReader<'self> {
|
|
|
|
buf: &'self [u8],
|
|
|
|
pos: uint
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> BufReader<'self> {
|
|
|
|
pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
|
|
|
|
BufReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self> Reader for BufReader<'self> {
|
|
|
|
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
|
|
|
|
|
|
|
|
fn eof(&mut self) -> bool { fail!() }
|
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl<'self> Seek for BufReader<'self> {
|
2013-04-17 19:55:21 -05:00
|
|
|
fn tell(&self) -> u64 { fail!() }
|
|
|
|
|
|
|
|
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_writer() {
|
|
|
|
let mut writer = MemWriter::new();
|
|
|
|
assert!(writer.tell() == 0);
|
|
|
|
writer.write([0]);
|
|
|
|
assert!(writer.tell() == 1);
|
|
|
|
writer.write([1, 2, 3]);
|
|
|
|
writer.write([4, 5, 6, 7]);
|
|
|
|
assert!(writer.tell() == 8);
|
|
|
|
assert!(writer.inner() == ~[0, 1, 2, 3, 4, 5 , 6, 7]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_reader() {
|
|
|
|
let mut reader = MemReader::new(~[0, 1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
let mut buf = [];
|
|
|
|
assert!(reader.read(buf) == Some(0));
|
|
|
|
assert!(reader.tell() == 0);
|
|
|
|
let mut buf = [0];
|
|
|
|
assert!(reader.read(buf) == Some(1));
|
|
|
|
assert!(reader.tell() == 1);
|
|
|
|
assert!(buf == [0]);
|
|
|
|
let mut buf = [0, ..4];
|
|
|
|
assert!(reader.read(buf) == Some(4));
|
|
|
|
assert!(reader.tell() == 5);
|
|
|
|
assert!(buf == [1, 2, 3, 4]);
|
|
|
|
assert!(reader.read(buf) == Some(3));
|
|
|
|
assert!(buf.slice(0, 3) == [5, 6, 7]);
|
|
|
|
assert!(reader.eof());
|
|
|
|
assert!(reader.read(buf) == None);
|
|
|
|
assert!(reader.eof());
|
|
|
|
}
|
|
|
|
}
|