rust/src/libstd/io/buffered.rs

669 lines
21 KiB
Rust
Raw Normal View History

// 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
//! Buffering wrappers for I/O traits
2014-02-06 01:34:33 -06:00
use cmp;
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
use iter::{IteratorExt, ExactSizeIterator};
use ops::{Drop, Index};
use option::Option;
use option::Option::{Some, None};
use result::Result::Ok;
use slice::{SliceExt};
use slice;
use vec::Vec;
// NOTE: for old macros; remove after the next snapshot
#[cfg(stage0)] use result::Result::Err;
/// Wraps a Reader and buffers input from it
///
/// 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.
///
/// # Example
///
/// ```rust
/// use std::io::{BufferedReader, File};
///
/// let file = File::open(&Path::new("message.txt"));
/// let mut reader = BufferedReader::new(file);
///
/// let mut buf = [0; 100];
2014-11-17 02:39:01 -06:00
/// match reader.read(&mut buf) {
/// Ok(nread) => println!("Read {} bytes", nread),
/// Err(e) => println!("error reading: {}", e)
/// }
/// ```
pub struct BufferedReader<R> {
2014-03-27 17:09:47 -05:00
inner: R,
buf: Vec<u8>,
pos: uint,
cap: uint,
}
impl<R: Reader> BufferedReader<R> {
/// Creates a new `BufferedReader` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
// 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 BufferedReader instances
// to be very cheap (large mallocs are not nearly as expensive as large
// callocs).
let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedReader {
inner: inner,
buf: buf,
pos: 0,
2014-01-07 11:47:53 -06:00
cap: 0,
}
}
/// Creates a new `BufferedReader` with a default buffer capacity
pub fn new(inner: R) -> BufferedReader<R> {
BufferedReader::with_capacity(DEFAULT_BUF_SIZE, inner)
}
/// Gets a reference to the underlying reader.
pub fn get_ref<'a>(&self) -> &R { &self.inner }
/// Gets a mutable reference to the underlying reader.
///
/// # Warning
///
/// It is inadvisable to directly read from the underlying reader.
pub fn get_mut(&mut self) -> &mut R { &mut self.inner }
/// Unwraps this `BufferedReader`, returning the underlying reader.
///
/// Note that any leftover data in the internal buffer is lost.
pub fn into_inner(self) -> R { self.inner }
}
impl<R: Reader> Buffer for BufferedReader<R> {
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()));
self.pos = 0;
}
Ok(self.buf.index(&(self.pos..self.cap)))
}
fn consume(&mut self, amt: uint) {
self.pos += amt;
assert!(self.pos <= self.cap);
}
}
impl<R: Reader> Reader for BufferedReader<R> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
if self.pos == self.cap && buf.len() >= self.buf.capacity() {
return self.inner.read(buf);
}
let nread = {
let available = try!(self.fill_buf());
2014-02-06 01:34:33 -06:00
let nread = cmp::min(available.len(), buf.len());
slice::bytes::copy_memory(buf, available.index(&(0..nread)));
nread
};
self.pos += nread;
Ok(nread)
}
}
/// Wraps a Writer and buffers output to it
///
/// 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.
///
/// This writer will be flushed when it is dropped.
///
/// # Example
///
/// ```rust
/// use std::io::{BufferedWriter, File};
///
/// let file = File::create(&Path::new("message.txt")).unwrap();
/// let mut writer = BufferedWriter::new(file);
///
/// writer.write_str("hello, world").unwrap();
/// writer.flush().unwrap();
/// ```
pub struct BufferedWriter<W> {
2014-03-27 17:09:47 -05:00
inner: Option<W>,
buf: Vec<u8>,
pos: uint
}
impl<W: Writer> BufferedWriter<W> {
/// Creates a new `BufferedWriter` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
// See comments in BufferedReader for why this uses unsafe code.
let mut buf = Vec::with_capacity(cap);
unsafe { buf.set_len(cap); }
BufferedWriter {
inner: Some(inner),
buf: buf,
pos: 0
}
}
/// Creates a new `BufferedWriter` with a default buffer capacity
pub fn new(inner: W) -> BufferedWriter<W> {
BufferedWriter::with_capacity(DEFAULT_BUF_SIZE, inner)
}
fn flush_buf(&mut self) -> IoResult<()> {
if self.pos != 0 {
let ret = self.inner.as_mut().unwrap().write(self.buf.index(&(0..self.pos)));
self.pos = 0;
ret
} else {
Ok(())
}
}
/// Gets a reference to the underlying writer.
pub fn get_ref(&self) -> &W { self.inner.as_ref().unwrap() }
/// Gets a mutable reference to the underlying write.
///
/// # Warning
///
/// It is inadvisable to directly read from the underlying writer.
pub fn get_mut(&mut self) -> &mut W { self.inner.as_mut().unwrap() }
/// Unwraps this `BufferedWriter`, returning the underlying writer.
///
/// The buffer is flushed before returning the writer.
pub fn into_inner(mut self) -> W {
// FIXME(#12628): is panicking the right thing to do if flushing panicks?
self.flush_buf().unwrap();
2014-08-18 19:52:38 -05:00
self.inner.take().unwrap()
}
}
impl<W: Writer> Writer for BufferedWriter<W> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
if self.pos + buf.len() > self.buf.len() {
try!(self.flush_buf());
}
if buf.len() > self.buf.len() {
self.inner.as_mut().unwrap().write(buf)
} else {
2014-12-18 17:44:24 -06:00
let dst = self.buf.slice_from_mut(self.pos);
slice::bytes::copy_memory(dst, buf);
self.pos += buf.len();
Ok(())
}
}
fn flush(&mut self) -> IoResult<()> {
self.flush_buf().and_then(|()| self.inner.as_mut().unwrap().flush())
}
}
#[unsafe_destructor]
impl<W: Writer> Drop for BufferedWriter<W> {
fn drop(&mut self) {
if self.inner.is_some() {
// dtors should not panic, so we ignore a panicked flush
let _ = self.flush_buf();
}
}
}
2013-12-14 23:26:09 -06:00
/// Wraps a Writer and buffers output to it, flushing whenever a newline (`0x0a`,
/// `'\n'`) is detected.
///
/// This writer will be flushed when it is dropped.
pub struct LineBufferedWriter<W> {
2014-03-27 17:09:47 -05:00
inner: BufferedWriter<W>,
}
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)
}
}
/// 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() }
/// Unwraps this `LineBufferedWriter`, returning the underlying writer.
///
/// The internal buffer is flushed before returning the writer.
pub fn into_inner(self) -> W { self.inner.into_inner() }
}
impl<W: Writer> Writer for LineBufferedWriter<W> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
2014-08-06 01:02:50 -05:00
match buf.iter().rposition(|&b| b == b'\n') {
Some(i) => {
try!(self.inner.write(buf.index(&(0..(i + 1)))));
try!(self.inner.flush());
try!(self.inner.write(buf.index(&((i + 1)..))));
Ok(())
}
None => self.inner.write(buf),
}
}
fn flush(&mut self) -> IoResult<()> { self.inner.flush() }
}
struct InternalBufferedWriter<W>(BufferedWriter<W>);
impl<W> InternalBufferedWriter<W> {
fn get_mut<'a>(&'a mut self) -> &'a mut BufferedWriter<W> {
let InternalBufferedWriter(ref mut w) = *self;
return w;
}
}
impl<W: Reader> Reader for InternalBufferedWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
self.get_mut().inner.as_mut().unwrap().read(buf)
}
}
/// Wraps a Stream and buffers input and output to and from it.
///
/// 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`.
///
/// The output half will be flushed when this stream is dropped.
///
/// # Example
///
/// ```rust
/// # #![allow(unused_must_use)]
/// 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();
///
/// let mut buf = [0; 100];
2014-11-17 02:39:01 -06:00
/// match stream.read(&mut buf) {
/// Ok(nread) => println!("Read {} bytes", nread),
/// Err(e) => println!("error reading: {}", e)
/// }
/// ```
pub struct BufferedStream<S> {
2014-03-27 17:09:47 -05:00
inner: BufferedReader<InternalBufferedWriter<S>>
}
impl<S: Stream> BufferedStream<S> {
/// Creates a new buffered stream with explicitly listed capacities for the
/// reader/writer buffer.
pub fn with_capacities(reader_cap: uint, writer_cap: uint, inner: S)
-> BufferedStream<S> {
let writer = BufferedWriter::with_capacity(writer_cap, inner);
let internal_writer = InternalBufferedWriter(writer);
let reader = BufferedReader::with_capacity(reader_cap,
internal_writer);
BufferedStream { inner: reader }
}
/// Creates a new buffered stream with the default reader/writer buffer
/// capacities.
pub fn new(inner: S) -> BufferedStream<S> {
BufferedStream::with_capacities(DEFAULT_BUF_SIZE, DEFAULT_BUF_SIZE,
inner)
}
/// Gets a reference to the underlying stream.
pub fn get_ref(&self) -> &S {
let InternalBufferedWriter(ref w) = self.inner.inner;
w.get_ref()
}
/// Gets a mutable reference to the underlying stream.
///
/// # Warning
///
/// 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()
}
/// Unwraps this `BufferedStream`, returning the underlying stream.
///
/// The internal buffer is flushed before returning the stream. Any leftover
/// data in the read buffer is lost.
pub fn into_inner(self) -> S {
let InternalBufferedWriter(w) = self.inner.inner;
w.into_inner()
}
}
impl<S: Stream> Buffer for BufferedStream<S> {
fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]> { self.inner.fill_buf() }
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
}
impl<S: Stream> Reader for BufferedStream<S> {
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
self.inner.read(buf)
}
}
impl<S: Stream> Writer for BufferedStream<S> {
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
2014-09-14 22:27:36 -05:00
self.inner.inner.get_mut().write(buf)
}
fn flush(&mut self) -> IoResult<()> {
2014-09-14 22:27:36 -05:00
self.inner.inner.get_mut().flush()
}
}
#[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;
use prelude::v1::*;
use super::*;
use super::super::{IoResult, EndOfFile};
2014-11-30 18:55:53 -06:00
use super::super::mem::MemReader;
use self::test::Bencher;
/// 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.
#[derive(Clone,PartialEq,PartialOrd)]
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))
}
}
impl Writer for NullStream {
2014-01-30 16:10:53 -06:00
fn write(&mut self, _: &[u8]) -> io::IoResult<()> { Ok(()) }
}
/// A dummy reader intended at testing short-reads propagation.
pub struct ShortReader {
lengths: Vec<uint>,
}
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
}
}
}
#[test]
fn test_buffered_reader() {
let inner = MemReader::new(vec!(5, 6, 7, 0, 1, 2, 3, 4));
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);
assert_eq!(Ok(3), nread);
let b: &[_] = &[5, 6, 7];
assert_eq!(buf, b);
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);
let b: &[_] = &[0, 1];
assert_eq!(buf, b);
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);
let b: &[_] = &[2];
assert_eq!(buf, b);
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);
let b: &[_] = &[3, 0, 0];
assert_eq!(buf, b);
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);
let b: &[_] = &[4, 0, 0];
assert_eq!(buf, b);
2014-11-17 02:39:01 -06:00
assert!(reader.read(&mut buf).is_err());
}
#[test]
fn test_buffered_writer() {
let inner = Vec::new();
let mut writer = BufferedWriter::with_capacity(2, inner);
2014-11-17 02:39:01 -06:00
writer.write(&[0, 1]).unwrap();
let b: &[_] = &[];
assert_eq!(writer.get_ref()[], b);
2014-11-17 02:39:01 -06:00
writer.write(&[2]).unwrap();
let b: &[_] = &[0, 1];
assert_eq!(writer.get_ref()[], b);
2014-11-17 02:39:01 -06:00
writer.write(&[3]).unwrap();
assert_eq!(writer.get_ref()[], b);
2014-01-30 16:10:53 -06:00
writer.flush().unwrap();
let a: &[_] = &[0, 1, 2, 3];
assert_eq!(a, writer.get_ref()[]);
2014-11-17 02:39:01 -06:00
writer.write(&[4]).unwrap();
writer.write(&[5]).unwrap();
assert_eq!(a, writer.get_ref()[]);
2014-11-17 02:39:01 -06:00
writer.write(&[6]).unwrap();
let a: &[_] = &[0, 1, 2, 3, 4, 5];
assert_eq!(a,
writer.get_ref()[]);
2014-11-17 02:39:01 -06:00
writer.write(&[7, 8]).unwrap();
let a: &[_] = &[0, 1, 2, 3, 4, 5, 6];
assert_eq!(a,
writer.get_ref()[]);
2014-11-17 02:39:01 -06:00
writer.write(&[9, 10, 11]).unwrap();
let a: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
assert_eq!(a,
writer.get_ref()[]);
2014-01-30 16:10:53 -06:00
writer.flush().unwrap();
assert_eq!(a,
writer.get_ref()[]);
}
#[test]
fn test_buffered_writer_inner_flushes() {
let mut w = BufferedWriter::with_capacity(3, Vec::new());
2014-11-17 02:39:01 -06:00
w.write(&[0, 1]).unwrap();
let a: &[_] = &[];
assert_eq!(a, w.get_ref()[]);
std: Second pass stabilization for `comm` This commit is a second pass stabilization for the `std::comm` module, performing the following actions: * The entire `std::comm` module was moved under `std::sync::mpsc`. This movement reflects that channels are just yet another synchronization primitive, and they don't necessarily deserve a special place outside of the other concurrency primitives that the standard library offers. * The `send` and `recv` methods have all been removed. * The `send_opt` and `recv_opt` methods have been renamed to `send` and `recv`. This means that all send/receive operations return a `Result` now indicating whether the operation was successful or not. * The error type of `send` is now a `SendError` to implement a custom error message and allow for `unwrap()`. The error type contains an `into_inner` method to extract the value. * The error type of `recv` is now `RecvError` for the same reasons as `send`. * The `TryRecvError` and `TrySendError` types have had public reexports removed of their variants and the variant names have been tweaked with enum namespacing rules. * The `Messages` iterator is renamed to `Iter` This functionality is now all `#[stable]`: * `Sender` * `SyncSender` * `Receiver` * `std::sync::mpsc` * `channel` * `sync_channel` * `Iter` * `Sender::send` * `Sender::clone` * `SyncSender::send` * `SyncSender::try_send` * `SyncSender::clone` * `Receiver::recv` * `Receiver::try_recv` * `Receiver::iter` * `SendError` * `RecvError` * `TrySendError::{mod, Full, Disconnected}` * `TryRecvError::{mod, Empty, Disconnected}` * `SendError::into_inner` * `TrySendError::into_inner` This is a breaking change due to the modification of where this module is located, as well as the changing of the semantics of `send` and `recv`. Most programs just need to rename imports of `std::comm` to `std::sync::mpsc` and add calls to `unwrap` after a send or a receive operation. [breaking-change]
2014-12-23 13:53:35 -06:00
let w = w.into_inner();
let a: &[_] = &[0, 1];
assert_eq!(a, w.index(&FullRange));
}
// 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-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))
}
}
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();
}
#[test]
fn test_read_until() {
2014-03-27 00:46:25 -05:00
let inner = MemReader::new(vec!(0, 1, 2, 1, 0));
let mut reader = BufferedReader::with_capacity(2, inner);
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());
}
#[test]
fn test_line_buffer() {
let mut writer = LineBufferedWriter::new(Vec::new());
2014-11-17 02:39:01 -06:00
writer.write(&[0]).unwrap();
let b: &[_] = &[];
assert_eq!(writer.get_ref()[], b);
2014-11-17 02:39:01 -06:00
writer.write(&[1]).unwrap();
assert_eq!(writer.get_ref()[], b);
2014-01-30 16:10:53 -06:00
writer.flush().unwrap();
let b: &[_] = &[0, 1];
assert_eq!(writer.get_ref()[], b);
2014-11-17 02:39:01 -06:00
writer.write(&[0, b'\n', 1, b'\n', 2]).unwrap();
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n'];
assert_eq!(writer.get_ref()[], b);
2014-01-30 16:10:53 -06:00
writer.flush().unwrap();
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n', 2];
assert_eq!(writer.get_ref()[], b);
2014-11-17 02:39:01 -06:00
writer.write(&[3, b'\n']).unwrap();
let b: &[_] = &[0, 1, 0, b'\n', 1, b'\n', 2, 3, b'\n'];
assert_eq!(writer.get_ref()[], b);
}
#[test]
fn test_read_line() {
Clean up rustc warnings. compiletest: compact "linux" "macos" etc.as "unix". liballoc: remove a superfluous "use". libcollections: remove invocations of deprecated methods in favor of their suggested replacements and use "_" for a loop counter. libcoretest: remove invocations of deprecated methods; also add "allow(deprecated)" for testing a deprecated method itself. libglob: use "cfg_attr". libgraphviz: add a test for one of data constructors. libgreen: remove a superfluous "use". libnum: "allow(type_overflow)" for type cast into u8 in a test code. librustc: names of static variables should be in upper case. libserialize: v[i] instead of get(). libstd/ascii: to_lowercase() instead of to_lower(). libstd/bitflags: modify AnotherSetOfFlags to use i8 as its backend. It will serve better for testing various aspects of bitflags!. libstd/collections: "allow(deprecated)" for testing a deprecated method itself. libstd/io: remove invocations of deprecated methods and superfluous "use". Also add #[test] where it was missing. libstd/num: introduce a helper function to effectively remove invocations of a deprecated method. libstd/path and rand: remove invocations of deprecated methods and superfluous "use". libstd/task and libsync/comm: "allow(deprecated)" for testing a deprecated method itself. libsync/deque: remove superfluous "unsafe". libsync/mutex and once: names of static variables should be in upper case. libterm: introduce a helper function to effectively remove invocations of a deprecated method. We still see a few warnings about using obsoleted native::task::spawn() in the test modules for libsync. I'm not sure how I should replace them with std::task::TaksBuilder and native::task::NativeTaskBuilder (dependency to libstd?) Signed-off-by: NODA, Kai <nodakai@gmail.com>
2014-10-05 05:11:17 -05:00
let in_buf = MemReader::new(b"a\nb\nc".to_vec());
let mut reader = BufferedReader::with_capacity(2, in_buf);
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());
}
#[test]
fn test_lines() {
Clean up rustc warnings. compiletest: compact "linux" "macos" etc.as "unix". liballoc: remove a superfluous "use". libcollections: remove invocations of deprecated methods in favor of their suggested replacements and use "_" for a loop counter. libcoretest: remove invocations of deprecated methods; also add "allow(deprecated)" for testing a deprecated method itself. libglob: use "cfg_attr". libgraphviz: add a test for one of data constructors. libgreen: remove a superfluous "use". libnum: "allow(type_overflow)" for type cast into u8 in a test code. librustc: names of static variables should be in upper case. libserialize: v[i] instead of get(). libstd/ascii: to_lowercase() instead of to_lower(). libstd/bitflags: modify AnotherSetOfFlags to use i8 as its backend. It will serve better for testing various aspects of bitflags!. libstd/collections: "allow(deprecated)" for testing a deprecated method itself. libstd/io: remove invocations of deprecated methods and superfluous "use". Also add #[test] where it was missing. libstd/num: introduce a helper function to effectively remove invocations of a deprecated method. libstd/path and rand: remove invocations of deprecated methods and superfluous "use". libstd/task and libsync/comm: "allow(deprecated)" for testing a deprecated method itself. libsync/deque: remove superfluous "unsafe". libsync/mutex and once: names of static variables should be in upper case. libterm: introduce a helper function to effectively remove invocations of a deprecated method. We still see a few warnings about using obsoleted native::task::spawn() in the test modules for libsync. I'm not sure how I should replace them with std::task::TaksBuilder and native::task::NativeTaskBuilder (dependency to libstd?) Signed-off-by: NODA, Kai <nodakai@gmail.com>
2014-10-05 05:11:17 -05:00
let in_buf = MemReader::new(b"a\nb\nc".to_vec());
let mut reader = BufferedReader::with_capacity(2, in_buf);
let mut it = reader.lines();
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())));
assert_eq!(it.next(), None);
}
#[test]
fn test_short_reads() {
let inner = ShortReader{lengths: vec![0, 1, 2, 0, 1, 0]};
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());
}
#[test]
fn read_char_buffered() {
let buf = [195u8, 159u8];
let mut reader = BufferedReader::with_capacity(1, buf.index(&FullRange));
2014-01-30 16:10:53 -06:00
assert_eq!(reader.read_char(), Ok('ß'));
}
#[test]
fn test_chars() {
2014-08-06 01:02:50 -05:00
let buf = [195u8, 159u8, b'a'];
let mut reader = BufferedReader::with_capacity(1, buf.index(&FullRange));
let mut it = reader.chars();
assert_eq!(it.next(), Some(Ok('ß')));
assert_eq!(it.next(), Some(Ok('a')));
assert_eq!(it.next(), None);
}
#[test]
#[should_fail]
fn dont_panic_in_drop_on_panicked_flush() {
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);
// If writer panics *again* due to the flush error then the process will abort.
panic!();
}
#[bench]
fn bench_buffered_reader(b: &mut Bencher) {
b.iter(|| {
BufferedReader::new(NullStream)
});
}
#[bench]
fn bench_buffered_writer(b: &mut Bencher) {
b.iter(|| {
BufferedWriter::new(NullStream)
});
}
#[bench]
fn bench_buffered_stream(b: &mut Bencher) {
b.iter(|| {
BufferedStream::new(NullStream);
});
}
}