Skip to content

Implement Show for types in std::io::{buffered,util} #20910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/libstd/io/buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//! Buffering wrappers for I/O traits

use cmp;
use fmt;
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
use iter::{IteratorExt, ExactSizeIterator};
use ops::Drop;
Expand Down Expand Up @@ -51,6 +52,13 @@ pub struct BufferedReader<R> {
cap: uint,
}

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())
}
}

impl<R: Reader> BufferedReader<R> {
/// Creates a new `BufferedReader` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
Expand Down Expand Up @@ -148,6 +156,13 @@ pub struct BufferedWriter<W> {
pos: uint
}

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())
}
}

impl<W: Writer> BufferedWriter<W> {
/// Creates a new `BufferedWriter` with the specified buffer capacity
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
Expand Down Expand Up @@ -235,6 +250,13 @@ pub struct LineBufferedWriter<W> {
inner: BufferedWriter<W>,
}

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())
}
}

impl<W: Writer> LineBufferedWriter<W> {
/// Creates a new `LineBufferedWriter`
pub fn new(inner: W) -> LineBufferedWriter<W> {
Expand Down Expand Up @@ -318,6 +340,17 @@ pub struct BufferedStream<S> {
inner: BufferedReader<InternalBufferedWriter<S>>
}

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())
}
}

impl<S: Stream> BufferedStream<S> {
/// Creates a new buffered stream with explicitly listed capacities for the
/// reader/writer buffer.
Expand Down
23 changes: 13 additions & 10 deletions src/libstd/io/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use io;
use slice::bytes::MutableByteVector;

/// Wraps a `Reader`, limiting the number of bytes that can be read from it.
#[derive(Show)]
pub struct LimitReader<R> {
limit: uint,
inner: R
Expand Down Expand Up @@ -77,7 +78,7 @@ impl<R: Buffer> Buffer for LimitReader<R> {
}

/// A `Writer` which ignores bytes written to it, like /dev/null.
#[derive(Copy)]
#[derive(Copy, Show)]
pub struct NullWriter;

impl Writer for NullWriter {
Expand All @@ -86,7 +87,7 @@ impl Writer for NullWriter {
}

/// A `Reader` which returns an infinite stream of 0 bytes, like /dev/zero.
#[derive(Copy)]
#[derive(Copy, Show)]
pub struct ZeroReader;

impl Reader for ZeroReader {
Expand All @@ -107,7 +108,7 @@ impl Buffer for ZeroReader {
}

/// A `Reader` which is always at EOF, like /dev/null.
#[derive(Copy)]
#[derive(Copy, Show)]
pub struct NullReader;

impl Reader for NullReader {
Expand All @@ -128,18 +129,19 @@ impl Buffer for NullReader {
///
/// The `Writer`s are delegated to in order. If any `Writer` returns an error,
/// that error is returned immediately and remaining `Writer`s are not called.
pub struct MultiWriter {
writers: Vec<Box<Writer+'static>>
#[derive(Show)]
pub struct MultiWriter<W> {
writers: Vec<W>
}

impl MultiWriter {
impl<W> MultiWriter<W> where W: Writer {
/// Creates a new `MultiWriter`
pub fn new(writers: Vec<Box<Writer+'static>>) -> MultiWriter {
pub fn new(writers: Vec<W>) -> MultiWriter<W> {
MultiWriter { writers: writers }
}
}

impl Writer for MultiWriter {
impl<W> Writer for MultiWriter<W> where W: Writer {
#[inline]
fn write(&mut self, buf: &[u8]) -> io::IoResult<()> {
for writer in self.writers.iter_mut() {
Expand All @@ -159,7 +161,7 @@ impl Writer for MultiWriter {

/// A `Reader` which chains input from multiple `Reader`s, reading each to
/// completion before moving onto the next.
#[derive(Clone)]
#[derive(Clone, Show)]
pub struct ChainedReader<I, R> {
readers: I,
cur_reader: Option<R>,
Expand Down Expand Up @@ -198,6 +200,7 @@ impl<R: Reader, I: Iterator<Item=R>> Reader for ChainedReader<I, R> {

/// A `Reader` which forwards input from another `Reader`, passing it along to
/// a `Writer` as well. Similar to the `tee(1)` command.
#[derive(Show)]
pub struct TeeReader<R, W> {
reader: R,
writer: W,
Expand Down Expand Up @@ -239,7 +242,7 @@ pub fn copy<R: Reader, W: Writer>(r: &mut R, w: &mut W) -> io::IoResult<()> {
}

/// An adaptor converting an `Iterator<u8>` to a `Reader`.
#[derive(Clone)]
#[derive(Clone, Show)]
pub struct IterReader<T> {
iter: T,
}
Expand Down