2013-09-10 00:38:43 -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 #15883
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
//! Buffering wrappers for I/O traits
|
|
|
|
|
2014-02-06 01:34:33 -06:00
|
|
|
use cmp;
|
2015-01-10 19:32:54 -06:00
|
|
|
use fmt;
|
2014-01-29 18:33:57 -06:00
|
|
|
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
|
2015-01-12 21:50:33 -06:00
|
|
|
use iter::{IteratorExt, ExactSizeIterator, repeat};
|
2015-01-07 10:58:31 -06:00
|
|
|
use ops::Drop;
|
2014-11-28 10:57:41 -06:00
|
|
|
use option::Option;
|
|
|
|
use option::Option::{Some, None};
|
2014-09-15 21:29:47 -05:00
|
|
|
use result::Result::Ok;
|
2014-12-11 11:44:17 -06:00
|
|
|
use slice::{SliceExt};
|
2014-03-08 17:11:52 -06:00
|
|
|
use slice;
|
2014-03-22 19:18:27 -05:00
|
|
|
use vec::Vec;
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
/// Wraps a Reader and buffers input from it
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// It can be excessively inefficient to work directly with a `Reader`. For
|
|
|
|
/// example, every call to `read` on `TcpStream` results in a system call. A
|
|
|
|
/// `BufferedReader` performs large, infrequent reads on the underlying
|
|
|
|
/// `Reader` and maintains an in-memory buffer of the results.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::{BufferedReader, File};
|
|
|
|
///
|
|
|
|
/// let file = File::open(&Path::new("message.txt"));
|
|
|
|
/// let mut reader = BufferedReader::new(file);
|
|
|
|
///
|
2014-12-30 02:19:41 -06:00
|
|
|
/// let mut buf = [0; 100];
|
2014-11-17 02:39:01 -06:00
|
|
|
/// match reader.read(&mut buf) {
|
2014-01-30 18:55:20 -06:00
|
|
|
/// Ok(nread) => println!("Read {} bytes", nread),
|
|
|
|
/// Err(e) => println!("error reading: {}", e)
|
2014-01-15 15:25:09 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-09-10 00:38:43 -05:00
|
|
|
pub struct BufferedReader<R> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: R,
|
|
|
|
buf: Vec<u8>,
|
|
|
|
pos: uint,
|
|
|
|
cap: uint,
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2015-01-10 19:32:54 -06:00
|
|
|
impl<R> fmt::Show for BufferedReader<R> where R: fmt::Show {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "BufferedReader {{ reader: {:?}, buffer: {}/{} }}",
|
|
|
|
self.inner, self.cap - self.pos, self.buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
impl<R: Reader> BufferedReader<R> {
|
2014-03-06 01:35:12 -06:00
|
|
|
/// Creates a new `BufferedReader` with the specified buffer capacity
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
|
|
|
|
BufferedReader {
|
|
|
|
inner: inner,
|
2015-01-12 21:50:33 -06:00
|
|
|
// We can't use the same trick here as we do for BufferedWriter,
|
|
|
|
// since this memory is visible to the inner Reader.
|
|
|
|
buf: repeat(0).take(cap).collect(),
|
2013-09-10 00:38:43 -05:00
|
|
|
pos: 0,
|
2014-01-07 11:47:53 -06:00
|
|
|
cap: 0,
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `BufferedReader` with a default buffer capacity
|
|
|
|
pub fn new(inner: R) -> BufferedReader<R> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedReader::with_capacity(DEFAULT_BUF_SIZE, inner)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying reader.
|
2014-11-25 19:37:13 -06:00
|
|
|
pub fn get_ref<'a>(&self) -> &R { &self.inner }
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the underlying reader.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
2014-12-10 12:54:56 -06:00
|
|
|
/// # Warning
|
2014-11-25 19:37:13 -06:00
|
|
|
///
|
|
|
|
/// It is inadvisable to directly read from the underlying reader.
|
|
|
|
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
|
2014-01-07 22:05:33 -06:00
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedReader`, returning the underlying reader.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// Note that any leftover data in the internal buffer is lost.
|
2014-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(self) -> R { self.inner }
|
2013-11-13 13:17:58 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<R: Reader> Buffer for BufferedReader<R> {
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> {
|
2014-01-30 16:10:53 -06:00
|
|
|
if self.pos == self.cap {
|
2014-12-18 17:44:24 -06:00
|
|
|
self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
|
2014-01-29 18:33:57 -06:00
|
|
|
self.pos = 0;
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2015-01-07 10:58:31 -06:00
|
|
|
Ok(&self.buf[self.pos..self.cap])
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-11-13 13:17:58 -06:00
|
|
|
|
|
|
|
fn consume(&mut self, amt: uint) {
|
|
|
|
self.pos += amt;
|
|
|
|
assert!(self.pos <= self.cap);
|
|
|
|
}
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-10-06 17:39:42 -05:00
|
|
|
impl<R: Reader> Reader for BufferedReader<R> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-10-16 19:48:29 -05:00
|
|
|
if self.pos == self.cap && buf.len() >= self.buf.capacity() {
|
|
|
|
return self.inner.read(buf);
|
|
|
|
}
|
2013-10-06 17:39:42 -05:00
|
|
|
let nread = {
|
2014-03-20 20:52:00 -05:00
|
|
|
let available = try!(self.fill_buf());
|
2014-02-06 01:34:33 -06:00
|
|
|
let nread = cmp::min(available.len(), buf.len());
|
2015-01-12 15:59:18 -06:00
|
|
|
slice::bytes::copy_memory(buf, &available[..nread]);
|
2013-10-06 17:39:42 -05:00
|
|
|
nread
|
|
|
|
};
|
2013-09-10 00:38:43 -05:00
|
|
|
self.pos += nread;
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(nread)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps a Writer and buffers output to it
|
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// It can be excessively inefficient to work directly with a `Writer`. For
|
|
|
|
/// example, every call to `write` on `TcpStream` results in a system call. A
|
|
|
|
/// `BufferedWriter` keeps an in memory buffer of data and writes it to the
|
|
|
|
/// underlying `Writer` in large, infrequent batches.
|
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// This writer will be flushed when it is dropped.
|
2014-01-15 15:25:09 -06:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use std::io::{BufferedWriter, File};
|
|
|
|
///
|
2014-10-27 13:34:47 -05:00
|
|
|
/// let file = File::create(&Path::new("message.txt")).unwrap();
|
2014-01-15 15:25:09 -06:00
|
|
|
/// let mut writer = BufferedWriter::new(file);
|
|
|
|
///
|
2014-10-27 13:34:47 -05:00
|
|
|
/// writer.write_str("hello, world").unwrap();
|
|
|
|
/// writer.flush().unwrap();
|
2014-01-15 15:25:09 -06:00
|
|
|
/// ```
|
2013-09-10 00:38:43 -05:00
|
|
|
pub struct BufferedWriter<W> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: Option<W>,
|
|
|
|
buf: Vec<u8>,
|
|
|
|
pos: uint
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2015-01-10 19:32:54 -06:00
|
|
|
impl<W> fmt::Show for BufferedWriter<W> where W: fmt::Show {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "BufferedWriter {{ writer: {:?}, buffer: {}/{} }}",
|
|
|
|
self.inner.as_ref().unwrap(), self.pos, self.buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
impl<W: Writer> BufferedWriter<W> {
|
2014-03-06 01:35:12 -06:00
|
|
|
/// Creates a new `BufferedWriter` with the specified buffer capacity
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
|
2015-01-12 21:50:33 -06:00
|
|
|
// It's *much* faster to create an uninitialized buffer than it is to
|
|
|
|
// fill everything in with 0. This buffer is entirely an implementation
|
|
|
|
// detail and is never exposed, so we're safe to not initialize
|
|
|
|
// everything up-front. This allows creation of BufferedWriter instances
|
|
|
|
// to be very cheap (large mallocs are not nearly as expensive as large
|
|
|
|
// callocs).
|
2014-03-22 19:18:27 -05:00
|
|
|
let mut buf = Vec::with_capacity(cap);
|
2013-12-15 06:05:30 -06:00
|
|
|
unsafe { buf.set_len(cap); }
|
2013-09-10 00:38:43 -05:00
|
|
|
BufferedWriter {
|
2014-02-28 14:46:46 -06:00
|
|
|
inner: Some(inner),
|
2013-11-11 12:08:03 -06:00
|
|
|
buf: buf,
|
2013-09-10 00:38:43 -05:00
|
|
|
pos: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new `BufferedWriter` with a default buffer capacity
|
|
|
|
pub fn new(inner: W) -> BufferedWriter<W> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-11-30 10:27:39 -06:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush_buf(&mut self) -> IoResult<()> {
|
2013-11-30 10:27:39 -06:00
|
|
|
if self.pos != 0 {
|
2015-01-12 15:59:18 -06:00
|
|
|
let ret = self.inner.as_mut().unwrap().write(&self.buf[..self.pos]);
|
2013-11-30 10:27:39 -06:00
|
|
|
self.pos = 0;
|
2014-01-29 18:33:57 -06:00
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2013-11-30 10:27:39 -06:00
|
|
|
}
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying writer.
|
2014-11-25 19:37:13 -06:00
|
|
|
pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
|
|
|
|
|
|
|
|
/// Gets a mutable reference to the underlying write.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
2014-12-10 12:54:56 -06:00
|
|
|
/// # Warning
|
2014-11-25 19:37:13 -06:00
|
|
|
///
|
|
|
|
/// It is inadvisable to directly read from the underlying writer.
|
|
|
|
pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
|
2014-01-07 22:05:33 -06:00
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedWriter`, returning the underlying writer.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The buffer is flushed before returning the writer.
|
2014-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(mut self) -> W {
|
2014-10-09 14:17:22 -05:00
|
|
|
// FIXME(#12628): is panicking the right thing to do if flushing panicks?
|
2014-01-29 18:33:57 -06:00
|
|
|
self.flush_buf().unwrap();
|
2014-08-18 19:52:38 -05:00
|
|
|
self.inner.take().unwrap()
|
2014-01-07 22:05:33 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<W: Writer> Writer for BufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2013-09-10 00:38:43 -05:00
|
|
|
if self.pos + buf.len() > self.buf.len() {
|
2014-02-19 12:07:49 -06:00
|
|
|
try!(self.flush_buf());
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if buf.len() > self.buf.len() {
|
2014-09-17 12:47:05 -05:00
|
|
|
self.inner.as_mut().unwrap().write(buf)
|
2013-09-10 00:38:43 -05:00
|
|
|
} else {
|
2014-12-18 17:44:24 -06:00
|
|
|
let dst = self.buf.slice_from_mut(self.pos);
|
2014-03-08 17:11:52 -06:00
|
|
|
slice::bytes::copy_memory(dst, buf);
|
2013-09-10 00:38:43 -05:00
|
|
|
self.pos += buf.len();
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
2014-09-17 12:47:05 -05:00
|
|
|
self.flush_buf().and_then(|()| self.inner.as_mut().unwrap().flush())
|
2014-02-28 14:46:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<W: Writer> Drop for BufferedWriter<W> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
if self.inner.is_some() {
|
2014-10-09 14:17:22 -05:00
|
|
|
// dtors should not panic, so we ignore a panicked flush
|
2014-02-28 14:46:46 -06:00
|
|
|
let _ = self.flush_buf();
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-14 23:26:09 -06:00
|
|
|
/// Wraps a Writer and buffers output to it, flushing whenever a newline (`0x0a`,
|
|
|
|
/// `'\n'`) is detected.
|
2013-10-24 19:30:36 -05:00
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// This writer will be flushed when it is dropped.
|
2013-10-24 19:30:36 -05:00
|
|
|
pub struct LineBufferedWriter<W> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: BufferedWriter<W>,
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
|
2015-01-10 19:32:54 -06:00
|
|
|
impl<W> fmt::Show for LineBufferedWriter<W> where W: fmt::Show {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "LineBufferedWriter {{ writer: {:?}, buffer: {}/{} }}",
|
|
|
|
self.inner.inner, self.inner.pos, self.inner.buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-24 19:30:36 -05:00
|
|
|
impl<W: Writer> LineBufferedWriter<W> {
|
|
|
|
/// Creates a new `LineBufferedWriter`
|
|
|
|
pub fn new(inner: W) -> LineBufferedWriter<W> {
|
|
|
|
// Lines typically aren't that long, don't use a giant buffer
|
|
|
|
LineBufferedWriter {
|
|
|
|
inner: BufferedWriter::with_capacity(1024, inner)
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying writer.
|
|
|
|
///
|
|
|
|
/// This type does not expose the ability to get a mutable reference to the
|
|
|
|
/// underlying reader because that could possibly corrupt the buffer.
|
|
|
|
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
|
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `LineBufferedWriter`, returning the underlying writer.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The internal buffer is flushed before returning the writer.
|
2014-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(self) -> W { self.inner.into_inner() }
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-10-24 19:30:36 -05:00
|
|
|
impl<W: Writer> Writer for LineBufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-08-06 01:02:50 -05:00
|
|
|
match buf.iter().rposition(|&b| b == b'\n') {
|
2013-10-24 19:30:36 -05:00
|
|
|
Some(i) => {
|
2015-01-12 15:59:18 -06:00
|
|
|
try!(self.inner.write(&buf[..(i + 1)]));
|
2014-02-19 12:07:49 -06:00
|
|
|
try!(self.inner.flush());
|
2015-01-07 10:58:31 -06:00
|
|
|
try!(self.inner.write(&buf[(i + 1)..]));
|
2014-01-29 18:33:57 -06:00
|
|
|
Ok(())
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
None => self.inner.write(buf),
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-10-24 19:30:36 -05:00
|
|
|
|
2014-01-29 18:33:57 -06:00
|
|
|
fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
|
|
|
|
2013-10-03 02:15:54 -05:00
|
|
|
struct InternalBufferedWriter<W>(BufferedWriter<W>);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-01 20:06:31 -05:00
|
|
|
impl<W> InternalBufferedWriter<W> {
|
2014-09-14 17:57:55 -05:00
|
|
|
fn get_mut<'a>(&'a mut self) -> &'a mut BufferedWriter<W> {
|
2013-11-01 20:06:31 -05:00
|
|
|
let InternalBufferedWriter(ref mut w) = *self;
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
impl<W: Reader> Reader for InternalBufferedWriter<W> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
2014-09-17 12:47:05 -05:00
|
|
|
self.get_mut().inner.as_mut().unwrap().read(buf)
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2014-02-28 14:46:46 -06:00
|
|
|
/// Wraps a Stream and buffers input and output to and from it.
|
2013-09-10 00:38:43 -05:00
|
|
|
///
|
2014-03-22 19:18:27 -05:00
|
|
|
/// It can be excessively inefficient to work directly with a `Stream`. For
|
|
|
|
/// example, every call to `read` or `write` on `TcpStream` results in a system
|
|
|
|
/// call. A `BufferedStream` keeps in memory buffers of data, making large,
|
|
|
|
/// infrequent calls to `read` and `write` on the underlying `Stream`.
|
|
|
|
///
|
2014-02-28 14:46:46 -06:00
|
|
|
/// The output half will be flushed when this stream is dropped.
|
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::{BufferedStream, File};
|
|
|
|
///
|
|
|
|
/// let file = File::open(&Path::new("message.txt"));
|
|
|
|
/// let mut stream = BufferedStream::new(file);
|
|
|
|
///
|
|
|
|
/// stream.write("hello, world".as_bytes());
|
|
|
|
/// stream.flush();
|
|
|
|
///
|
2014-12-30 02:19:41 -06:00
|
|
|
/// let mut buf = [0; 100];
|
2014-11-17 02:39:01 -06:00
|
|
|
/// match stream.read(&mut buf) {
|
2014-01-30 18:55:20 -06:00
|
|
|
/// Ok(nread) => println!("Read {} bytes", nread),
|
|
|
|
/// Err(e) => println!("error reading: {}", e)
|
2014-01-15 15:25:09 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2013-09-12 23:48:47 -05:00
|
|
|
pub struct BufferedStream<S> {
|
2014-03-27 17:09:47 -05:00
|
|
|
inner: BufferedReader<InternalBufferedWriter<S>>
|
2013-09-12 23:48:47 -05:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2015-01-10 19:32:54 -06:00
|
|
|
impl<S> fmt::Show for BufferedStream<S> where S: fmt::Show {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let reader = &self.inner;
|
|
|
|
let writer = &self.inner.inner.0;
|
|
|
|
write!(fmt, "BufferedStream {{ stream: {:?}, write_buffer: {}/{}, read_buffer: {}/{} }}",
|
|
|
|
writer.inner,
|
|
|
|
writer.pos, writer.buf.len(),
|
|
|
|
reader.cap - reader.pos, reader.buf.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
impl<S: Stream> BufferedStream<S> {
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new buffered stream with explicitly listed capacities for the
|
|
|
|
/// reader/writer buffer.
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn with_capacities(reader_cap: uint, writer_cap: uint, inner: S)
|
|
|
|
-> BufferedStream<S> {
|
|
|
|
let writer = BufferedWriter::with_capacity(writer_cap, inner);
|
2013-10-03 02:15:54 -05:00
|
|
|
let internal_writer = InternalBufferedWriter(writer);
|
2013-09-10 00:38:43 -05:00
|
|
|
let reader = BufferedReader::with_capacity(reader_cap,
|
|
|
|
internal_writer);
|
2013-09-12 23:48:47 -05:00
|
|
|
BufferedStream { inner: reader }
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2014-01-07 22:05:33 -06:00
|
|
|
/// Creates a new buffered stream with the default reader/writer buffer
|
|
|
|
/// capacities.
|
2013-09-10 00:38:43 -05:00
|
|
|
pub fn new(inner: S) -> BufferedStream<S> {
|
2014-01-15 15:25:09 -06:00
|
|
|
BufferedStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE,
|
2013-09-10 00:38:43 -05:00
|
|
|
inner)
|
|
|
|
}
|
2014-01-07 22:05:33 -06:00
|
|
|
|
|
|
|
/// Gets a reference to the underlying stream.
|
2014-11-25 19:37:13 -06:00
|
|
|
pub fn get_ref(&self) -> &S {
|
2014-01-07 22:05:33 -06:00
|
|
|
let InternalBufferedWriter(ref w) = self.inner.inner;
|
|
|
|
w.get_ref()
|
|
|
|
}
|
|
|
|
|
2014-11-25 19:37:13 -06:00
|
|
|
/// Gets a mutable reference to the underlying stream.
|
|
|
|
///
|
2014-12-10 12:54:56 -06:00
|
|
|
/// # Warning
|
2014-11-25 19:37:13 -06:00
|
|
|
///
|
|
|
|
/// It is inadvisable to read directly from or write directly to the
|
|
|
|
/// underlying stream.
|
|
|
|
pub fn get_mut(&mut self) -> &mut S {
|
|
|
|
let InternalBufferedWriter(ref mut w) = self.inner.inner;
|
|
|
|
w.get_mut()
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:18:27 -05:00
|
|
|
/// Unwraps this `BufferedStream`, returning the underlying stream.
|
2014-01-07 22:05:33 -06:00
|
|
|
///
|
|
|
|
/// The internal buffer is flushed before returning the stream. Any leftover
|
|
|
|
/// data in the read buffer is lost.
|
2014-11-20 11:23:43 -06:00
|
|
|
pub fn into_inner(self) -> S {
|
2014-01-07 22:05:33 -06:00
|
|
|
let InternalBufferedWriter(w) = self.inner.inner;
|
2014-11-20 11:23:43 -06:00
|
|
|
w.into_inner()
|
2014-01-07 22:05:33 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<S: Stream> Buffer for BufferedStream<S> {
|
2014-03-20 20:52:00 -05:00
|
|
|
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> { self.inner.fill_buf() }
|
2013-11-13 13:17:58 -06:00
|
|
|
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-11-13 13:17:58 -06:00
|
|
|
impl<S: Stream> Reader for BufferedStream<S> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
self.inner.read(buf)
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Stream> Writer for BufferedStream<S> {
|
2014-01-29 18:33:57 -06:00
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
2014-09-14 22:27:36 -05:00
|
|
|
self.inner.inner.get_mut().write(buf)
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
|
|
|
fn flush(&mut self) -> IoResult<()> {
|
2014-09-14 22:27:36 -05:00
|
|
|
self.inner.inner.get_mut().flush()
|
2014-01-29 18:33:57 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-02-13 19:49:11 -06:00
|
|
|
extern crate test;
|
2014-01-07 00:33:37 -06:00
|
|
|
use io;
|
2014-12-22 11:04:23 -06:00
|
|
|
use prelude::v1::*;
|
2013-09-10 00:38:43 -05:00
|
|
|
use super::*;
|
2014-05-14 19:49:14 -05:00
|
|
|
use super::super::{IoResult, EndOfFile};
|
2014-11-30 18:55:53 -06:00
|
|
|
use super::super::mem::MemReader;
|
2014-03-31 20:16:35 -05:00
|
|
|
use self::test::Bencher;
|
2013-11-12 11:38:54 -06:00
|
|
|
|
2014-01-15 15:25:09 -06:00
|
|
|
/// A type, free to create, primarily intended for benchmarking creation of
|
|
|
|
/// wrappers that, just for construction, don't need a Reader/Writer that
|
|
|
|
/// does anything useful. Is equivalent to `/dev/null` in semantics.
|
2015-01-03 21:54:18 -06:00
|
|
|
#[derive(Clone,PartialEq,PartialOrd)]
|
2013-11-12 11:38:54 -06:00
|
|
|
pub struct NullStream;
|
|
|
|
|
|
|
|
impl Reader for NullStream {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for NullStream {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2013-12-22 12:07:08 -06:00
|
|
|
/// A dummy reader intended at testing short-reads propagation.
|
|
|
|
pub struct ShortReader {
|
2014-04-17 17:28:14 -05:00
|
|
|
lengths: Vec<uint>,
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for ShortReader {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
2014-12-30 12:51:18 -06:00
|
|
|
if self.lengths.is_empty() {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
|
|
|
} else {
|
|
|
|
Ok(self.lengths.remove(0))
|
2014-01-30 16:10:53 -06:00
|
|
|
}
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_buffered_reader() {
|
2014-10-16 19:48:29 -05:00
|
|
|
let inner = MemReader::new(vec!(5, 6, 7, 0, 1, 2, 3, 4));
|
2013-09-10 00:38:43 -05:00
|
|
|
let mut reader = BufferedReader::with_capacity(2, inner);
|
|
|
|
|
|
|
|
let mut buf = [0, 0, 0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let nread = reader.read(&mut buf);
|
2014-10-16 19:48:29 -05:00
|
|
|
assert_eq!(Ok(3), nread);
|
|
|
|
let b: &[_] = &[5, 6, 7];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2014-10-16 19:48:29 -05:00
|
|
|
|
|
|
|
let mut buf = [0, 0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let nread = reader.read(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(2), nread);
|
2014-10-16 19:48:29 -05:00
|
|
|
let b: &[_] = &[0, 1];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
let mut buf = [0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let nread = reader.read(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[2];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
|
|
|
let mut buf = [0, 0, 0];
|
2014-11-17 02:39:01 -06:00
|
|
|
let nread = reader.read(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[3, 0, 0];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
let nread = reader.read(&mut buf);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(Ok(1), nread);
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[4, 0, 0];
|
2014-11-27 13:43:55 -06:00
|
|
|
assert_eq!(buf, b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_buffered_writer() {
|
2014-11-11 15:01:29 -06:00
|
|
|
let inner = Vec::new();
|
2013-09-10 00:38:43 -05:00
|
|
|
let mut writer = BufferedWriter::with_capacity(2, inner);
|
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[0, 1]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[2]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[3]).unwrap();
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[0, 1, 2, 3];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[4]).unwrap();
|
|
|
|
writer.write(&[5]).unwrap();
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[6]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[0, 1, 2, 3, 4, 5];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[7, 8]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[0, 1, 2, 3, 4, 5, 6];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[9, 10, 11]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &writer.get_ref()[]);
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-30 10:27:39 -06:00
|
|
|
#[test]
|
|
|
|
fn test_buffered_writer_inner_flushes() {
|
2014-11-11 15:01:29 -06:00
|
|
|
let mut w = BufferedWriter::with_capacity(3, Vec::new());
|
2014-11-17 02:39:01 -06:00
|
|
|
w.write(&[0, 1]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(a, &w.get_ref()[]);
|
2014-12-23 13:53:35 -06:00
|
|
|
let w = w.into_inner();
|
2014-08-04 07:19:02 -05:00
|
|
|
let a: &[_] = &[0, 1];
|
2015-01-07 10:58:31 -06:00
|
|
|
assert_eq!(a, &w[]);
|
2013-11-30 10:27:39 -06:00
|
|
|
}
|
|
|
|
|
2013-09-10 00:38:43 -05:00
|
|
|
// This is just here to make sure that we don't infinite loop in the
|
|
|
|
// newtype struct autoderef weirdness
|
|
|
|
#[test]
|
|
|
|
fn test_buffered_stream() {
|
|
|
|
struct S;
|
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
impl io::Writer for S {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
impl io::Reader for S {
|
2014-01-30 16:10:53 -06:00
|
|
|
fn read(&mut self, _: &mut [u8]) -> io::IoResult<uint> {
|
|
|
|
Err(io::standard_error(io::EndOfFile))
|
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut stream = BufferedStream::new(S);
|
|
|
|
let mut buf = [];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert!(stream.read(&mut buf).is_err());
|
|
|
|
stream.write(&buf).unwrap();
|
2014-01-30 16:10:53 -06:00
|
|
|
stream.flush().unwrap();
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|
2013-10-06 17:39:42 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_read_until() {
|
2014-03-27 00:46:25 -05:00
|
|
|
let inner = MemReader::new(vec!(0, 1, 2, 1, 0));
|
2013-10-06 17:39:42 -05:00
|
|
|
let mut reader = BufferedReader::with_capacity(2, inner);
|
2014-03-26 11:24:16 -05:00
|
|
|
assert_eq!(reader.read_until(0), Ok(vec!(0)));
|
|
|
|
assert_eq!(reader.read_until(2), Ok(vec!(1, 2)));
|
|
|
|
assert_eq!(reader.read_until(1), Ok(vec!(1)));
|
|
|
|
assert_eq!(reader.read_until(8), Ok(vec!(0)));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(reader.read_until(9).is_err());
|
2013-10-06 17:39:42 -05:00
|
|
|
}
|
2013-10-24 19:30:36 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_line_buffer() {
|
2014-11-11 15:01:29 -06:00
|
|
|
let mut writer = LineBufferedWriter::new(Vec::new());
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[0]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[1]).unwrap();
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n'];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2014-01-30 16:10:53 -06:00
|
|
|
writer.flush().unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n', 2];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2014-11-17 02:39:01 -06:00
|
|
|
writer.write(&[3, b'\n']).unwrap();
|
2014-08-04 07:19:02 -05:00
|
|
|
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n'];
|
2015-01-03 22:43:24 -06:00
|
|
|
assert_eq!(&writer.get_ref()[], b);
|
2013-10-24 19:30:36 -05:00
|
|
|
}
|
2013-11-12 11:38:54 -06:00
|
|
|
|
2013-12-08 01:12:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_read_line() {
|
2014-10-05 05:11:17 -05:00
|
|
|
let in_buf = MemReader::new(b"a\nb\nc".to_vec());
|
2013-12-08 01:12:07 -06:00
|
|
|
let mut reader = BufferedReader::with_capacity(2, in_buf);
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(reader.read_line(), Ok("a\n".to_string()));
|
|
|
|
assert_eq!(reader.read_line(), Ok("b\n".to_string()));
|
|
|
|
assert_eq!(reader.read_line(), Ok("c".to_string()));
|
2014-01-30 16:10:53 -06:00
|
|
|
assert!(reader.read_line().is_err());
|
2013-12-08 01:12:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lines() {
|
2014-10-05 05:11:17 -05:00
|
|
|
let in_buf = MemReader::new(b"a\nb\nc".to_vec());
|
2013-12-08 01:12:07 -06:00
|
|
|
let mut reader = BufferedReader::with_capacity(2, in_buf);
|
|
|
|
let mut it = reader.lines();
|
2014-05-25 05:17:19 -05:00
|
|
|
assert_eq!(it.next(), Some(Ok("a\n".to_string())));
|
|
|
|
assert_eq!(it.next(), Some(Ok("b\n".to_string())));
|
|
|
|
assert_eq!(it.next(), Some(Ok("c".to_string())));
|
2013-12-08 01:12:07 -06:00
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-12-22 12:07:08 -06:00
|
|
|
#[test]
|
|
|
|
fn test_short_reads() {
|
2014-04-17 17:28:14 -05:00
|
|
|
let inner = ShortReader{lengths: vec![0, 1, 2, 0, 1, 0]};
|
2013-12-22 12:07:08 -06:00
|
|
|
let mut reader = BufferedReader::new(inner);
|
|
|
|
let mut buf = [0, 0];
|
2014-11-17 02:39:01 -06:00
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(1));
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(2));
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(1));
|
|
|
|
assert_eq!(reader.read(&mut buf), Ok(0));
|
|
|
|
assert!(reader.read(&mut buf).is_err());
|
2013-12-22 12:07:08 -06:00
|
|
|
}
|
|
|
|
|
2014-01-07 10:48:44 -06:00
|
|
|
#[test]
|
|
|
|
fn read_char_buffered() {
|
|
|
|
let buf = [195u8, 159u8];
|
2015-01-07 10:58:31 -06:00
|
|
|
let mut reader = BufferedReader::with_capacity(1, &buf[]);
|
2014-01-30 16:10:53 -06:00
|
|
|
assert_eq!(reader.read_char(), Ok('ß'));
|
2014-01-07 10:48:44 -06:00
|
|
|
}
|
2013-12-08 01:12:07 -06:00
|
|
|
|
2014-02-08 23:40:38 -06:00
|
|
|
#[test]
|
|
|
|
fn test_chars() {
|
2014-08-06 01:02:50 -05:00
|
|
|
let buf = [195u8, 159u8, b'a'];
|
2015-01-07 10:58:31 -06:00
|
|
|
let mut reader = BufferedReader::with_capacity(1, &buf[]);
|
2014-02-08 23:40:38 -06:00
|
|
|
let mut it = reader.chars();
|
2014-02-19 20:53:46 -06:00
|
|
|
assert_eq!(it.next(), Some(Ok('ß')));
|
|
|
|
assert_eq!(it.next(), Some(Ok('a')));
|
2014-02-08 23:40:38 -06:00
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2014-05-14 19:49:14 -05:00
|
|
|
#[test]
|
|
|
|
#[should_fail]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn dont_panic_in_drop_on_panicked_flush() {
|
2014-05-14 19:49:14 -05:00
|
|
|
struct FailFlushWriter;
|
|
|
|
|
|
|
|
impl Writer for FailFlushWriter {
|
|
|
|
fn write(&mut self, _buf: &[u8]) -> IoResult<()> { Ok(()) }
|
|
|
|
fn flush(&mut self) -> IoResult<()> { Err(io::standard_error(EndOfFile)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
let writer = FailFlushWriter;
|
|
|
|
let _writer = BufferedWriter::new(writer);
|
|
|
|
|
2014-10-09 14:17:22 -05:00
|
|
|
// If writer panics *again* due to the flush error then the process will abort.
|
|
|
|
panic!();
|
2014-05-14 19:49:14 -05:00
|
|
|
}
|
|
|
|
|
2013-11-12 11:38:54 -06:00
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_buffered_reader(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-02-12 09:39:21 -06:00
|
|
|
BufferedReader::new(NullStream)
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_buffered_writer(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-02-12 09:39:21 -06:00
|
|
|
BufferedWriter::new(NullStream)
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2014-03-31 20:16:35 -05:00
|
|
|
fn bench_buffered_stream(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2013-11-12 11:38:54 -06:00
|
|
|
BufferedStream::new(NullStream);
|
2013-11-21 19:23:21 -06:00
|
|
|
});
|
2013-11-12 11:38:54 -06:00
|
|
|
}
|
2013-09-10 00:38:43 -05:00
|
|
|
}
|