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-11-28 10:57:41 -06:00
|
|
|
use option::Option::None;
|
|
|
|
use result::Result::{Err, Ok};
|
2015-01-22 18:27:48 -06:00
|
|
|
use old_io;
|
|
|
|
use old_io::{Reader, Writer, Seek, Buffer, IoError, SeekStyle, IoResult};
|
2015-01-03 21:42:21 -06:00
|
|
|
use slice::{self, AsSlice, SliceExt};
|
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 {
|
2015-01-22 18:27:48 -06:00
|
|
|
old_io::SeekSet => 0,
|
|
|
|
old_io::SeekEnd => end,
|
|
|
|
old_io::SeekCur => cur,
|
2014-02-11 22:13:46 -06:00
|
|
|
} as i64;
|
|
|
|
|
|
|
|
if offset + pos < 0 {
|
|
|
|
Err(IoError {
|
2015-01-22 18:27:48 -06:00
|
|
|
kind: old_io::InvalidInput,
|
2014-02-11 22:13:46 -06:00
|
|
|
desc: "invalid seek to a negative offset",
|
|
|
|
detail: None
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok((offset + pos) as u64)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 15:01:29 -06:00
|
|
|
impl Writer for Vec<u8> {
|
|
|
|
#[inline]
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
|
|
|
self.push_all(buf);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)]
|
2015-01-22 18:27:48 -06:00
|
|
|
/// use std::old_io::MemWriter;
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// 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-11-20 11:23:43 -06:00
|
|
|
/// assert_eq!(w.into_inner(), vec!(0, 1, 2));
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2014-11-11 15:01:29 -06:00
|
|
|
#[deprecated = "use the Vec<u8> Writer implementation directly"]
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(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
|
|
|
}
|
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
#[allow(deprecated)]
|
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-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(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)]
|
2015-01-22 18:27:48 -06:00
|
|
|
/// use std::old_io::MemReader;
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
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-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(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> {
|
2015-01-22 18:27:48 -06:00
|
|
|
if self.eof() { return Err(old_io::standard_error(old_io::EndOfFile)) }
|
2013-04-20 19:25:00 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
2015-01-19 10:07:13 -06:00
|
|
|
let input = &self.buf[self.pos.. self.pos + write_len];
|
2015-01-21 13:53:49 -06:00
|
|
|
let output = &mut buf[..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() {
|
2015-01-07 10:58:31 -06:00
|
|
|
Ok(&self.buf[self.pos..])
|
2014-02-07 23:50:49 -06:00
|
|
|
} else {
|
2015-01-22 18:27:48 -06:00
|
|
|
Err(old_io::standard_error(old_io::EndOfFile))
|
2014-02-07 23:50:49 -06:00
|
|
|
}
|
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
|
|
|
|
2014-11-30 18:55:53 -06:00
|
|
|
impl<'a> Reader for &'a [u8] {
|
|
|
|
#[inline]
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2015-01-22 18:27:48 -06:00
|
|
|
if self.is_empty() { return Err(old_io::standard_error(old_io::EndOfFile)); }
|
2014-11-30 18:55:53 -06:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.len());
|
|
|
|
{
|
2015-01-12 15:59:18 -06:00
|
|
|
let input = &self[..write_len];
|
2015-01-17 18:15:52 -06:00
|
|
|
let output = &mut buf[.. write_len];
|
2014-11-30 18:55:53 -06:00
|
|
|
slice::bytes::copy_memory(output, input);
|
|
|
|
}
|
|
|
|
|
2015-01-17 18:15:52 -06:00
|
|
|
*self = &self[write_len..];
|
2014-11-30 18:55:53 -06:00
|
|
|
|
|
|
|
Ok(write_len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Buffer for &'a [u8] {
|
|
|
|
#[inline]
|
2014-12-12 10:09:32 -06:00
|
|
|
fn fill_buf(&mut self) -> IoResult<&[u8]> {
|
2014-11-30 18:55:53 -06:00
|
|
|
if self.is_empty() {
|
2015-01-22 18:27:48 -06:00
|
|
|
Err(old_io::standard_error(old_io::EndOfFile))
|
2014-11-30 18:55:53 -06:00
|
|
|
} else {
|
|
|
|
Ok(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn consume(&mut self, amt: uint) {
|
2015-01-07 10:58:31 -06:00
|
|
|
*self = &self[amt..];
|
2014-11-30 18:55:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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)]
|
2015-01-22 18:27:48 -06:00
|
|
|
/// use std::old_io::BufWriter;
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-12-30 02:19:41 -06:00
|
|
|
/// let mut buf = [0; 4];
|
2014-01-15 15:25:09 -06:00
|
|
|
/// {
|
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]
|
2014-12-12 10:09:32 -06:00
|
|
|
pub fn new(buf: &'a mut [u8]) -> BufWriter<'a> {
|
2013-04-17 19:55:21 -05:00
|
|
|
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-12-05 11:42:48 -06:00
|
|
|
fn write(&mut self, src: &[u8]) -> IoResult<()> {
|
2015-01-17 18:15:52 -06:00
|
|
|
let dst = &mut self.buf[self.pos..];
|
2014-12-05 11:42:48 -06:00
|
|
|
let dst_len = dst.len();
|
|
|
|
|
|
|
|
if dst_len == 0 {
|
2015-01-22 18:27:48 -06:00
|
|
|
return Err(old_io::standard_error(old_io::EndOfFile));
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2013-04-17 19:55:21 -05:00
|
|
|
|
2014-12-05 11:42:48 -06:00
|
|
|
let src_len = src.len();
|
|
|
|
|
|
|
|
if dst_len >= src_len {
|
|
|
|
slice::bytes::copy_memory(dst, src);
|
|
|
|
|
|
|
|
self.pos += src_len;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2015-01-12 15:59:18 -06:00
|
|
|
slice::bytes::copy_memory(dst, &src[..dst_len]);
|
2014-12-05 11:42:48 -06:00
|
|
|
|
|
|
|
self.pos += dst_len;
|
|
|
|
|
2015-01-22 18:27:48 -06:00
|
|
|
Err(old_io::standard_error(old_io::ShortWrite(dst_len)))
|
2014-12-05 11:42:48 -06:00
|
|
|
}
|
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-12-05 11:42:48 -06:00
|
|
|
self.pos = min(new as uint, self.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-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)]
|
2015-01-22 18:27:48 -06:00
|
|
|
/// use std::old_io::BufReader;
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-11-27 13:31:54 -06:00
|
|
|
/// let buf = [0, 1, 2, 3];
|
|
|
|
/// let mut r = BufReader::new(&buf);
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-11-27 13:31:54 -06: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]
|
2014-12-12 10:09:32 -06:00
|
|
|
pub fn new(buf: &'a [u8]) -> BufReader<'a> {
|
2013-04-17 19:55:21 -05:00
|
|
|
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> {
|
2015-01-22 18:27:48 -06:00
|
|
|
if self.eof() { return Err(old_io::standard_error(old_io::EndOfFile)) }
|
2013-09-02 13:23:57 -05:00
|
|
|
|
|
|
|
let write_len = min(buf.len(), self.buf.len() - self.pos);
|
|
|
|
{
|
2015-01-19 10:07:13 -06:00
|
|
|
let input = &self.buf[self.pos.. self.pos + write_len];
|
2015-01-21 13:56:52 -06:00
|
|
|
let output = &mut buf[..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-12-12 10:09:32 -06:00
|
|
|
fn fill_buf(&mut self) -> IoResult<&[u8]> {
|
2014-02-08 19:53:27 -06:00
|
|
|
if self.pos < self.buf.len() {
|
2015-01-07 10:58:31 -06:00
|
|
|
Ok(&self.buf[self.pos..])
|
2014-02-08 19:53:27 -06:00
|
|
|
} else {
|
2015-01-22 18:27:48 -06:00
|
|
|
Err(old_io::standard_error(old_io::EndOfFile))
|
2014-02-08 19:53:27 -06:00
|
|
|
}
|
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-12-19 06:02:22 -06:00
|
|
|
extern crate "test" as test_crate;
|
2015-01-22 18:27:48 -06:00
|
|
|
use old_io::{SeekSet, SeekCur, SeekEnd, Reader, Writer, Seek};
|
2015-01-03 22:43:24 -06:00
|
|
|
use prelude::v1::{Ok, Err, range, Vec, Buffer, AsSlice, SliceExt};
|
2015-01-07 10:58:31 -06:00
|
|
|
use prelude::v1::IteratorExt;
|
2014-01-30 16:10:53 -06:00
|
|
|
use io;
|
2015-01-02 01:53:35 -06:00
|
|
|
use iter::repeat;
|
2014-12-19 06:02:22 -06:00
|
|
|
use self::test_crate::Bencher;
|
2015-01-02 01:53:35 -06:00
|
|
|
use super::*;
|
2013-04-20 19:25:00 -05:00
|
|
|
|
2014-11-15 00:50:47 -06:00
|
|
|
#[test]
|
|
|
|
fn test_vec_writer() {
|
|
|
|
let mut writer = Vec::new();
|
|
|
|
writer.write(&[0]).unwrap();
|
|
|
|
writer.write(&[1, 2, 3]).unwrap();
|
|
|
|
writer.write(&[4, 5, 6, 7]).unwrap();
|
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
assert_eq!(writer.as_slice(), b);
|
|
|
|
}
|
|
|
|
|
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() {
|
2014-12-30 02:19:41 -06:00
|
|
|
let mut buf = [0 as u8; 9];
|
2013-11-12 19:14:22 -06:00
|
|
|
{
|
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));
|
2014-11-15 00:50:47 -06:00
|
|
|
|
2015-01-22 18:27:48 -06:00
|
|
|
assert_eq!(writer.write(&[8, 9]).err().unwrap().kind, old_io::ShortWrite(1));
|
|
|
|
assert_eq!(writer.write(&[10]).err().unwrap().kind, old_io::EndOfFile);
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
2014-12-05 11:42:48 -06:00
|
|
|
let b: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_seek() {
|
2014-12-30 02:19:41 -06:00
|
|
|
let mut buf = [0 as u8; 8];
|
2013-11-12 19:14:22 -06:00
|
|
|
{
|
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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-11-12 19:14:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buf_writer_error() {
|
2014-12-30 02:19:41 -06:00
|
|
|
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!(),
|
2015-01-22 18:27:48 -06:00
|
|
|
Err(e) => assert_eq!(e.kind, old_io::ShortWrite(1)),
|
2014-01-30 16:10:53 -06:00
|
|
|
}
|
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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2014-12-30 02:19:41 -06: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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, 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];
|
2015-01-12 15:59:18 -06:00
|
|
|
assert_eq!(&buf[..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
|
|
|
|
2014-11-30 18:55:53 -06:00
|
|
|
#[test]
|
|
|
|
fn test_slice_reader() {
|
|
|
|
let in_buf = vec![0, 1, 2, 3, 4, 5, 6, 7];
|
|
|
|
let mut reader = &mut in_buf.as_slice();
|
|
|
|
let mut buf = [];
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
|
|
|
let mut buf = [0];
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(1));
|
|
|
|
assert_eq!(reader.len(), 7);
|
|
|
|
let b: &[_] = &[0];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
2014-12-30 02:19:41 -06:00
|
|
|
let mut buf = [0; 4];
|
2014-11-30 18:55:53 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(4));
|
|
|
|
assert_eq!(reader.len(), 3);
|
|
|
|
let b: &[_] = &[1, 2, 3, 4];
|
|
|
|
assert_eq!(buf.as_slice(), b);
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(3));
|
|
|
|
let b: &[_] = &[5, 6, 7];
|
2015-01-12 15:59:18 -06:00
|
|
|
assert_eq!(&buf[..3], b);
|
2014-11-30 18:55:53 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
|
|
|
let mut reader = &mut in_buf.as_slice();
|
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(0, 1, 2, 3));
|
|
|
|
assert_eq!(reader.read_until(3).unwrap(), vec!(4, 5, 6, 7));
|
|
|
|
assert!(reader.read(&mut buf).is_err());
|
|
|
|
}
|
|
|
|
|
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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2014-12-30 02:19:41 -06: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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, 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];
|
2015-01-12 15:59:18 -06:00
|
|
|
assert_eq!(&buf[..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-11-27 18:45:47 -06:00
|
|
|
assert_eq!(r.read_to_string().unwrap(), "testingtesting\ntesting");
|
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-11-27 18:45:47 -06:00
|
|
|
assert_eq!(r.read_to_string().unwrap(), "a\nệ");
|
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-12-30 02:19:41 -06: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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2014-12-18 17:44:24 -06:00
|
|
|
assert!(r.read_at_least(0, buf.slice_to_mut(0)).is_ok());
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, 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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, 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];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, 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) {
|
2015-01-02 01:53:35 -06:00
|
|
|
let src: Vec<u8> = repeat(5).take(len).collect();
|
2014-07-06 00:31:57 -05:00
|
|
|
|
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
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
let v = wr.into_inner();
|
2014-07-06 00:31:57 -05:00
|
|
|
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-12-30 02:19:41 -06: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-12-31 22:40:24 -06:00
|
|
|
let mut buf = [0 as u8; 10];
|
2014-11-17 02:39:01 -06:00
|
|
|
rdr.read(&mut buf).unwrap();
|
2014-12-31 22:40:24 -06:00
|
|
|
assert_eq!(buf.as_slice(), [5; 10].as_slice());
|
2014-06-20 22:05:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buf_writer(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-12-30 02:19:41 -06:00
|
|
|
let mut buf = [0 as u8; 100];
|
2014-06-20 22:05:42 -05:00
|
|
|
{
|
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-12-31 22:40:24 -06:00
|
|
|
wr.write(&[5; 10]).unwrap();
|
2014-06-20 22:05:42 -05:00
|
|
|
}
|
|
|
|
}
|
2014-12-31 22:40:24 -06:00
|
|
|
assert_eq!(buf.as_slice(), [5; 100].as_slice());
|
2014-06-20 22:05:42 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_buf_reader(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-12-30 02:19:41 -06:00
|
|
|
let buf = [5 as u8; 100];
|
2014-06-20 22:05:42 -05:00
|
|
|
{
|
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-12-31 22:40:24 -06:00
|
|
|
let mut buf = [0 as u8; 10];
|
2014-11-17 02:39:01 -06:00
|
|
|
rdr.read(&mut buf).unwrap();
|
2014-12-31 22:40:24 -06:00
|
|
|
assert_eq!(buf, [5; 10]);
|
2014-06-20 22:05:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-04-20 19:25:00 -05:00
|
|
|
}
|