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.
|
2014-07-21 17:57:14 -05:00
|
|
|
//
|
|
|
|
// ignore-lexer-test FIXME #15679
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
//! Readers and Writers for in-memory buffers
|
2014-01-15 15:25:09 -06:00
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use cmp::min;
|
2014-01-29 18:33:57 -06:00
|
|
|
use option::None;
|
|
|
|
use result::{Err, Ok};
|
|
|
|
use io;
|
|
|
|
use io::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, IoResult};
|
2014-11-02 19:04:32 -06:00
|
|
|
use slice::{mod, AsSlice, SlicePrelude};
|
2014-03-27 00:46:25 -05:00
|
|
|
use vec::Vec;
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2014-10-25 20:33:56 -05:00
|
|
|
const BUF_CAPACITY: uint = 128;
|
2014-07-29 18:31:39 -05:00
|
|
|
|
2014-02-11 22:13:46 -06:00
|
|
|
fn combine(seek: SeekStyle, cur: uint, end: uint, offset: i64) -> IoResult<u64> {
|
|
|
|
// compute offset as signed and clamp to prevent overflow
|
|
|
|
let pos = match seek {
|
2014-05-16 17:44:14 -05:00
|
|
|
io::SeekSet => 0,
|
|
|
|
io::SeekEnd => end,
|
|
|
|
io::SeekCur => cur,
|
2014-02-11 22:13:46 -06:00
|
|
|
} as i64;
|
|
|
|
|
|
|
|
if offset + pos < 0 {
|
|
|
|
Err(IoError {
|
|
|
|
kind: io::InvalidInput,
|
|
|
|
desc: "invalid seek to a negative offset",
|
|
|
|
detail: None
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok((offset + pos) as u64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-17 19:55:21 -05:00
|
|
|
/// Writes to an owned, growable byte vector
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-15 15:25:09 -06:00
|
|
|
/// use std::io::MemWriter;
|
|
|
|
///
|
|
|
|
/// let mut w = MemWriter::new();
|
2014-11-17 02:39:01 -06:00
|
|
|
/// w.write(&[0, 1, 2]);
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-03-27 00:46:25 -05:00
|
|
|
/// assert_eq!(w.unwrap(), vec!(0, 1, 2));
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2014-07-29 18:32:07 -05:00
|
|
|
#[deriving(Clone)]
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemWriter {
|
2014-03-27 00:46:25 -05:00
|
|
|
buf: Vec<u8>,
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemWriter {
|
2013-11-30 07:58:27 -06:00
|
|
|
/// Create a new `MemWriter`.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2013-10-21 19:11:42 -05:00
|
|
|
pub fn new() -> MemWriter {
|
2014-07-29 18:31:39 -05:00
|
|
|
MemWriter::with_capacity(BUF_CAPACITY)
|
2013-11-30 07:58:27 -06:00
|
|
|
}
|
|
|
|
/// Create a new `MemWriter`, allocating at least `n` bytes for
|
|
|
|
/// the internal buffer.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2013-11-30 07:58:27 -06:00
|
|
|
pub fn with_capacity(n: uint) -> MemWriter {
|
2014-10-25 20:33:56 -05:00
|
|
|
MemWriter::from_vec(Vec::with_capacity(n))
|
|
|
|
}
|
|
|
|
/// Create a new `MemWriter` that will append to an existing `Vec`.
|
|
|
|
#[inline]
|
|
|
|
pub fn from_vec(buf: Vec<u8>) -> MemWriter {
|
|
|
|
MemWriter { buf: buf }
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Acquires an immutable reference to the underlying buffer of this
|
|
|
|
/// `MemWriter`.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-07 22:05:33 -06:00
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
|
|
|
|
|
|
|
|
/// Unwraps this `MemWriter`, returning the underlying buffer
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-03-27 00:46:25 -05:00
|
|
|
pub fn unwrap(self) -> Vec<u8> { self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for MemWriter {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-07-29 18:31:39 -05:00
|
|
|
self.buf.push_all(buf);
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-10-21 19:11:42 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from an owned byte vector
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-15 15:25:09 -06:00
|
|
|
/// use std::io::MemReader;
|
|
|
|
///
|
2014-03-27 00:46:25 -05:00
|
|
|
/// let mut r = MemReader::new(vec!(0, 1, 2));
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-03-26 11:24:16 -05:00
|
|
|
/// assert_eq!(r.read_to_end().unwrap(), vec!(0, 1, 2));
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2013-04-17 19:55:21 -05:00
|
|
|
pub struct MemReader {
|
2014-03-27 00:46:25 -05:00
|
|
|
buf: Vec<u8>,
|
2014-03-27 17:09:47 -05:00
|
|
|
pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MemReader {
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new `MemReader` which will read the buffer given. The buffer
|
|
|
|
/// can be re-acquired through `unwrap`
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-03-27 00:46:25 -05:00
|
|
|
pub fn new(buf: Vec<u8>) -> MemReader {
|
2013-04-17 19:55:21 -05:00
|
|
|
MemReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
2014-01-09 23:03:18 -06:00
|
|
|
/// Tests whether this reader has read all bytes in its buffer.
|
|
|
|
///
|
|
|
|
/// If `true`, then this will no longer return bytes from `read`.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-02-11 22:13:46 -06:00
|
|
|
pub fn eof(&self) -> bool { self.pos >= self.buf.len() }
|
2014-01-09 23:03:18 -06:00
|
|
|
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Acquires an immutable reference to the underlying buffer of this
|
|
|
|
/// `MemReader`.
|
|
|
|
///
|
|
|
|
/// No method is exposed for acquiring a mutable reference to the buffer
|
|
|
|
/// because it could corrupt the state of this `MemReader`.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-07 22:05:33 -06:00
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a [u8] { self.buf.as_slice() }
|
|
|
|
|
|
|
|
/// Unwraps this `MemReader`, returning the underlying buffer
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-03-27 00:46:25 -05:00
|
|
|
pub fn unwrap(self) -> Vec<u8> { self.buf }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for MemReader {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
if self.eof() { return Err(io::standard_error(io::EndOfFile)) }
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
2014-09-24 06:41:09 -05:00
|
|
|
let input = self.buf[self.pos.. self.pos + write_len];
|
|
|
|
let output = buf[mut ..write_len];
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(input.len(), output.len());
|
2014-03-08 17:11:52 -06:00
|
|
|
slice::bytes::copy_memory(output, input);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
return Ok(write_len);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 16:58:21 -05:00
|
|
|
impl Seek for MemReader {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn tell(&self) -> IoResult<u64> { Ok(self.pos as u64) }
|
2014-06-20 22:05:42 -05:00
|
|
|
|
|
|
|
#[inline]
|
2014-02-11 22:13:46 -06:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
|
2014-02-19 12:07:49 -06:00
|
|
|
let new = try!(combine(style, self.pos, self.buf.len(), pos));
|
2014-02-11 22:13:46 -06:00
|
|
|
self.pos = new as uint;
|
|
|
|
Ok(())
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl Buffer for MemReader {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
|
2014-02-07 23:50:49 -06:00
|
|
|
if self.pos < self.buf.len() {
|
2014-09-24 06:41:09 -05:00
|
|
|
Ok(self.buf[self.pos..])
|
2014-02-07 23:50:49 -06:00
|
|
|
} else {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
|
|
|
}
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
2014-06-20 22:05:42 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-11-13 13:17:58 -06:00
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
|
|
|
/// Writes to a fixed-size byte slice
|
2013-11-12 19:14:22 -06:00
|
|
|
///
|
2014-01-30 18:55:20 -06:00
|
|
|
/// If a write will not fit in the buffer, it returns an error and does not
|
|
|
|
/// write any data.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-15 15:25:09 -06:00
|
|
|
/// use std::io::BufWriter;
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, ..4];
|
|
|
|
/// {
|
2014-11-17 02:39:01 -06:00
|
|
|
/// let mut w = BufWriter::new(&mut buf);
|
|
|
|
/// w.write(&[0, 1, 2]);
|
2014-01-15 15:25:09 -06:00
|
|
|
/// }
|
2014-02-28 03:23:06 -06:00
|
|
|
/// assert!(buf == [0, 1, 2, 0]);
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct BufWriter<'a> {
|
2014-03-27 17:09:47 -05:00
|
|
|
buf: &'a mut [u8],
|
|
|
|
pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
2013-04-19 14:04:19 -05:00
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> BufWriter<'a> {
|
2014-02-24 02:31:08 -06:00
|
|
|
/// Creates a new `BufWriter` which will wrap the specified buffer. The
|
|
|
|
/// writer initially starts at position 0.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2013-04-17 19:55:21 -05:00
|
|
|
pub fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> {
|
|
|
|
BufWriter {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Writer for BufWriter<'a> {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-04-08 04:56:48 -05:00
|
|
|
// return an error if the entire write does not fit in the buffer
|
2014-07-10 17:51:14 -05:00
|
|
|
let cap = if self.pos >= self.buf.len() { 0 } else { self.buf.len() - self.pos };
|
|
|
|
if buf.len() > cap {
|
2014-01-29 18:33:57 -06:00
|
|
|
return Err(IoError {
|
|
|
|
kind: io::OtherIoError,
|
2013-11-12 19:14:22 -06:00
|
|
|
desc: "Trying to write past end of buffer",
|
|
|
|
detail: None
|
2014-01-29 18:33:57 -06:00
|
|
|
})
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2014-09-24 06:41:09 -05:00
|
|
|
slice::bytes::copy_memory(self.buf[mut self.pos..], buf);
|
2013-11-12 19:14:22 -06:00
|
|
|
self.pos += buf.len();
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Seek for BufWriter<'a> {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn tell(&self) -> IoResult<u64> { Ok(self.pos as u64) }
|
2014-06-20 22:05:42 -05:00
|
|
|
|
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
|
2014-02-19 12:07:49 -06:00
|
|
|
let new = try!(combine(style, self.pos, self.buf.len(), pos));
|
2014-02-11 22:13:46 -06:00
|
|
|
self.pos = new as uint;
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 14:04:19 -05:00
|
|
|
/// Reads from a fixed-size byte slice
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-04-14 10:30:31 -05:00
|
|
|
/// # #![allow(unused_must_use)]
|
2014-01-15 15:25:09 -06:00
|
|
|
/// use std::io::BufReader;
|
|
|
|
///
|
|
|
|
/// let mut buf = [0, 1, 2, 3];
|
2014-11-17 02:39:01 -06:00
|
|
|
/// let mut r = BufReader::new(&mut buf);
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-03-26 11:24:16 -05:00
|
|
|
/// assert_eq!(r.read_to_end().unwrap(), vec!(0, 1, 2, 3));
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2013-12-10 01:16:18 -06:00
|
|
|
pub struct BufReader<'a> {
|
2014-03-27 17:09:47 -05:00
|
|
|
buf: &'a [u8],
|
|
|
|
pos: uint
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> BufReader<'a> {
|
2014-01-15 15:25:09 -06:00
|
|
|
/// Creates a new buffered reader which will read the specified buffer
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2013-04-17 19:55:21 -05:00
|
|
|
pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
|
|
|
|
BufReader {
|
|
|
|
buf: buf,
|
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
2014-01-09 23:03:18 -06:00
|
|
|
|
|
|
|
/// Tests whether this reader has read all bytes in its buffer.
|
|
|
|
///
|
|
|
|
/// If `true`, then this will no longer return bytes from `read`.
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-02-11 22:13:46 -06:00
|
|
|
pub fn eof(&self) -> bool { self.pos >= self.buf.len() }
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Reader for BufReader<'a> {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
if self.eof() { return Err(io::standard_error(io::EndOfFile)) }
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
2014-09-24 06:41:09 -05:00
|
|
|
let input = self.buf[self.pos.. self.pos + write_len];
|
|
|
|
let output = buf[mut ..write_len];
|
2013-09-02 13:23:57 -05:00
|
|
|
assert_eq!(input.len(), output.len());
|
2014-03-08 17:11:52 -06:00
|
|
|
slice::bytes::copy_memory(output, input);
|
2013-09-02 13:23:57 -05:00
|
|
|
}
|
|
|
|
self.pos += write_len;
|
|
|
|
assert!(self.pos <= self.buf.len());
|
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
return Ok(write_len);
|
2013-09-02 13:23:57 -05:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Seek for BufReader<'a> {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-01-29 18:33:57 -06:00
|
|
|
fn tell(&self) -> IoResult<u64> { Ok(self.pos as u64) }
|
2014-06-20 22:05:42 -05:00
|
|
|
|
|
|
|
#[inline]
|
2014-02-11 22:13:46 -06:00
|
|
|
fn seek(&mut self, pos: i64, style: SeekStyle) -> IoResult<()> {
|
2014-02-19 12:07:49 -06:00
|
|
|
let new = try!(combine(style, self.pos, self.buf.len(), pos));
|
2014-02-11 22:13:46 -06:00
|
|
|
self.pos = new as uint;
|
|
|
|
Ok(())
|
|
|
|
}
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
2013-12-10 01:16:18 -06:00
|
|
|
impl<'a> Buffer for BufReader<'a> {
|
2014-06-20 22:05:42 -05:00
|
|
|
#[inline]
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
|
2014-02-08 19:53:27 -06:00
|
|
|
if self.pos < self.buf.len() {
|
2014-09-24 06:41:09 -05:00
|
|
|
Ok(self.buf[self.pos..])
|
2014-02-08 19:53:27 -06:00
|
|
|
} else {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
|
|
|
}
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
2014-06-20 22:05:42 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-11-13 13:17:58 -06:00
|
|
|
fn consume(&mut self, amt: uint) { self.pos += amt; }
|
|
|
|
}
|
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-06-20 22:05:42 -05:00
|
|
|
extern crate test;
|
2013-04-20 19:25:00 -05:00
|
|
|
use prelude::*;
|
|
|
|
use super::*;
|
2013-11-11 00:46:32 -06:00
|
|
|
use io::*;
|
2014-01-30 16:10:53 -06:00
|
|
|
use io;
|
2014-06-20 22:05:42 -05:00
|
|
|
use self::test::Bencher;
|
2014-11-02 19:04:32 -06:00
|
|
|
use str::StrPrelude;
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mem_writer() {
|
|
|
|
let mut writer = MemWriter::new();
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[0]).unwrap();
|
|
|
|
writer.write(&[1, 2, 3]).unwrap();
|
|
|
|
writer.write(&[4, 5, 6, 7]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
assert_eq!(writer.get_ref(), b);
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
|
|
|
|
2013-11-12 19:14:22 -06:00
|
|
|
#[test]
|
|
|
|
fn test_buf_writer() {
|
|
|
|
let mut buf = [0 as u8, ..8];
|
|
|
|
{
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut writer = BufWriter::new(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(0));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[0]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(1));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[1, 2, 3]).unwrap();
|
|
|
|
writer.write(&[4, 5, 6, 7]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(8));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[]).unwrap();
|
2014-07-10 17:51:14 -05:00
|
|
|
assert_eq!(writer.tell(), Ok(8));
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_seek() {
|
|
|
|
let mut buf = [0 as u8, ..8];
|
|
|
|
{
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut writer = BufWriter::new(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(0));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[1]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(1));
|
2013-11-12 19:14:22 -06:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.seek(2, SeekSet).unwrap();
|
|
|
|
assert_eq!(writer.tell(), Ok(2));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[2]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(3));
|
2013-11-12 19:14:22 -06:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.seek(-2, SeekCur).unwrap();
|
|
|
|
assert_eq!(writer.tell(), Ok(1));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[3]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(2));
|
2013-11-12 19:14:22 -06:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.seek(-1, SeekEnd).unwrap();
|
|
|
|
assert_eq!(writer.tell(), Ok(7));
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[4]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(writer.tell(), Ok(8));
|
2013-11-12 19:14:22 -06:00
|
|
|
|
|
|
|
}
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[1, 3, 2, 0, 0, 0, 0, 4];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_error() {
|
|
|
|
let mut buf = [0 as u8, ..2];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut writer = BufWriter::new(&mut buf);
|
|
|
|
writer.write(&[0]).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
match writer.write(&[0, 0]) {
|
2014-10-09 14:17:22 -05:00
|
|
|
Ok(..) => panic!(),
|
2014-01-30 16:10:53 -06:00
|
|
|
Err(e) => assert_eq!(e.kind, io::OtherIoError),
|
|
|
|
}
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
|
|
|
|
2013-04-20 19:25:00 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mem_reader() {
|
2014-03-27 00:46:25 -05:00
|
|
|
let mut reader = MemReader::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(0));
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(1));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(1));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2013-04-20 19:25:00 -05:00
|
|
|
let mut buf = [0, ..4];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(4));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(5));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(3));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[5, 6, 7];
|
2014-09-24 06:41:09 -05:00
|
|
|
assert_eq!(buf[0..3], b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2014-03-27 00:46:25 -05:00
|
|
|
let mut reader = MemReader::new(vec!(0, 1, 2, 3, 4, 5, 6, 7));
|
2014-03-26 11:24:16 -05:00
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
|
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_reader() {
|
2014-06-06 12:27:49 -05:00
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let mut reader = BufReader::new(in_buf.as_slice());
|
2013-09-02 13:23:57 -05:00
|
|
|
let mut buf = [];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(0));
|
2013-09-02 13:23:57 -05:00
|
|
|
let mut buf = [0];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(1));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(1));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2013-09-02 13:23:57 -05:00
|
|
|
let mut buf = [0, ..4];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(4));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.tell(), Ok(5));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(3));
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[5, 6, 7];
|
2014-09-24 06:41:09 -05:00
|
|
|
assert_eq!(buf[0..3], b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2014-06-06 12:27:49 -05:00
|
|
|
let mut reader = BufReader::new(in_buf.as_slice());
|
2014-03-26 11:24:16 -05:00
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
|
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2013-09-02 13:23:57 -05:00
|
|
|
}
|
2013-08-24 02:22:40 -05:00
|
|
|
|
2013-11-13 13:36:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_char() {
|
2014-06-18 13:25:36 -05:00
|
|
|
let b = b"Vi\xE1\xBB\x87t";
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut r = BufReader::new(b);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(r.read_char(), Ok('V'));
|
|
|
|
assert_eq!(r.read_char(), Ok('i'));
|
|
|
|
assert_eq!(r.read_char(), Ok('ệ'));
|
|
|
|
assert_eq!(r.read_char(), Ok('t'));
|
|
|
|
assert!(r.read_char().is_err());
|
2013-11-13 13:36:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_bad_char() {
|
2014-06-18 13:25:36 -05:00
|
|
|
let b = b"\x80";
|
2014-01-15 13:39:08 -06:00
|
|
|
let mut r = BufReader::new(b);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(r.read_char().is_err());
|
2013-11-13 13:36:21 -06:00
|
|
|
}
|
2013-12-08 13:12:25 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_write_strings() {
|
|
|
|
let mut writer = MemWriter::new();
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write_str("testing").unwrap();
|
|
|
|
writer.write_line("testing").unwrap();
|
|
|
|
writer.write_str("testing").unwrap();
|
2014-01-07 22:05:33 -06:00
|
|
|
let mut r = BufReader::new(writer.get_ref());
|
2014-06-21 05:39:03 -05:00
|
|
|
assert_eq!(r.read_to_string().unwrap(), "testingtesting\ntesting".to_string());
|
2013-12-08 13:12:25 -06:00
|
|
|
}
|
|
|
|
|
2014-01-04 07:26:03 -06:00
|
|
|
#[test]
|
|
|
|
fn test_write_char() {
|
|
|
|
let mut writer = MemWriter::new();
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.write_char('a').unwrap();
|
|
|
|
writer.write_char('\n').unwrap();
|
|
|
|
writer.write_char('ệ').unwrap();
|
2014-01-07 22:05:33 -06:00
|
|
|
let mut r = BufReader::new(writer.get_ref());
|
2014-06-21 05:39:03 -05:00
|
|
|
assert_eq!(r.read_to_string().unwrap(), "a\nệ".to_string());
|
2014-01-04 07:26:03 -06:00
|
|
|
}
|
|
|
|
|
2013-12-08 13:12:25 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_whole_string_bad() {
|
|
|
|
let buf = [0xff];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut r = BufReader::new(&buf);
|
2014-06-21 05:39:03 -05:00
|
|
|
match r.read_to_string() {
|
2014-10-09 14:17:22 -05:00
|
|
|
Ok(..) => panic!(),
|
2013-12-08 13:12:25 -06:00
|
|
|
Err(..) => {}
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 22:13:46 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn seek_past_end() {
|
|
|
|
let buf = [0xff];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut r = BufReader::new(&buf);
|
2014-02-11 22:13:46 -06:00
|
|
|
r.seek(10, SeekSet).unwrap();
|
|
|
|
assert!(r.read(&mut []).is_err());
|
|
|
|
|
2014-03-27 00:46:25 -05:00
|
|
|
let mut r = MemReader::new(vec!(10));
|
2014-02-11 22:13:46 -06:00
|
|
|
r.seek(10, SeekSet).unwrap();
|
|
|
|
assert!(r.read(&mut []).is_err());
|
|
|
|
|
|
|
|
let mut buf = [0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut r = BufWriter::new(&mut buf);
|
2014-02-11 22:13:46 -06:00
|
|
|
r.seek(10, SeekSet).unwrap();
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(r.write(&[3]).is_err());
|
2014-02-11 22:13:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn seek_before_0() {
|
|
|
|
let buf = [0xff];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut r = BufReader::new(&buf);
|
2014-02-11 22:13:46 -06:00
|
|
|
assert!(r.seek(-1, SeekSet).is_err());
|
|
|
|
|
2014-03-27 00:46:25 -05:00
|
|
|
let mut r = MemReader::new(vec!(10));
|
2014-02-11 22:13:46 -06:00
|
|
|
assert!(r.seek(-1, SeekSet).is_err());
|
|
|
|
|
|
|
|
let mut buf = [0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut r = BufWriter::new(&mut buf);
|
2014-02-11 22:13:46 -06:00
|
|
|
assert!(r.seek(-1, SeekSet).is_err());
|
|
|
|
}
|
2014-03-20 20:52:00 -05:00
|
|
|
|
|
|
|
#[test]
|
2014-03-25 01:22:23 -05:00
|
|
|
fn io_read_at_least() {
|
|
|
|
let mut r = MemReader::new(vec![1, 2, 3, 4, 5, 6, 7, 8]);
|
2014-03-20 20:52:00 -05:00
|
|
|
let mut buf = [0, ..3];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(r.read_at_least(buf.len(), &mut buf).is_ok());
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[1, 2, 3];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-09-24 06:41:09 -05:00
|
|
|
assert!(r.read_at_least(0, buf[mut ..0]).is_ok());
|
2014-08-04 07:19:02 -05:00
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(r.read_at_least(buf.len(), &mut buf).is_ok());
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[4, 5, 6];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(r.read_at_least(buf.len(), &mut buf).is_err());
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[7, 8, 6];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-03-20 20:52:00 -05:00
|
|
|
}
|
2014-06-20 22:05:42 -05:00
|
|
|
|
2014-07-06 00:31:57 -05:00
|
|
|
fn do_bench_mem_writer(b: &mut Bencher, times: uint, len: uint) {
|
|
|
|
let src: Vec<u8> = Vec::from_elem(len, 5);
|
|
|
|
|
2014-07-29 18:31:39 -05:00
|
|
|
b.bytes = (times * len) as u64;
|
2014-06-20 22:05:42 -05:00
|
|
|
b.iter(|| {
|
|
|
|
let mut wr = MemWriter::new();
|
2014-07-06 00:31:57 -05:00
|
|
|
for _ in range(0, times) {
|
|
|
|
wr.write(src.as_slice()).unwrap();
|
2014-06-20 22:05:42 -05:00
|
|
|
}
|
2014-07-06 00:31:57 -05:00
|
|
|
|
|
|
|
let v = wr.unwrap();
|
|
|
|
assert_eq!(v.len(), times * len);
|
|
|
|
assert!(v.iter().all(|x| *x == 5));
|
2014-06-20 22:05:42 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-06 00:31:57 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_001_0000(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 1, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_001_0010(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 1, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_001_0100(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 1, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_001_1000(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 1, 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_100_0000(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 100, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_100_0010(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 100, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_100_0100(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 100, 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_mem_writer_100_1000(b: &mut Bencher) {
|
|
|
|
do_bench_mem_writer(b, 100, 1000)
|
|
|
|
}
|
|
|
|
|
2014-06-20 22:05:42 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_mem_reader(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-10-05 05:11:17 -05:00
|
|
|
let buf = [5 as u8, ..100].to_vec();
|
2014-06-20 22:05:42 -05:00
|
|
|
{
|
|
|
|
let mut rdr = MemReader::new(buf);
|
2014-04-21 16:58:52 -05:00
|
|
|
for _i in range(0u, 10) {
|
2014-06-20 22:05:42 -05:00
|
|
|
let mut buf = [0 as u8, .. 10];
|
2014-11-17 02:39:01 -06:00
|
|
|
rdr.read(&mut buf).unwrap();
|
2014-06-20 22:05:42 -05:00
|
|
|
assert_eq!(buf.as_slice(), [5, .. 10].as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buf_writer(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let mut buf = [0 as u8, ..100];
|
|
|
|
{
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut wr = BufWriter::new(&mut buf);
|
2014-04-21 16:58:52 -05:00
|
|
|
for _i in range(0u, 10) {
|
2014-11-17 02:39:01 -06:00
|
|
|
wr.write(&[5, .. 10]).unwrap();
|
2014-06-20 22:05:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(buf.as_slice(), [5, .. 100].as_slice());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buf_reader(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
|
|
|
let buf = [5 as u8, ..100];
|
|
|
|
{
|
2014-11-17 02:39:01 -06:00
|
|
|
let mut rdr = BufReader::new(&buf);
|
2014-04-21 16:58:52 -05:00
|
|
|
for _i in range(0u, 10) {
|
2014-06-20 22:05:42 -05:00
|
|
|
let mut buf = [0 as u8, .. 10];
|
2014-11-17 02:39:01 -06:00
|
|
|
rdr.read(&mut buf).unwrap();
|
2014-06-20 22:05:42 -05:00
|
|
|
assert_eq!(buf.as_slice(), [5, .. 10].as_slice());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|