Skip to content

Commit 2e4cef4

Browse files
committed
auto merge of #20910 : sfackler/rust/show-impls, r=alexcrichton
A derived implementation would not be appropriate for the Buffered types since the buffer is both huge (64k by default) and full of uninitialized memory. Instead of printing the whole thing, we display how full it is. I also altered `MultiWriter` to make it generic over Writers instead of taking `Box<Writer>` trait objects. `Box<Writer>` implements `Writer` so existing use cases should continue to work, and this enables a more useful Show implementation in applicable cases. The change to `MultiWriter` may break code that uses it, but any fixes should be easy. [breaking-change] r? @alexcrichton
2 parents 2127e0d + b4fae2f commit 2e4cef4

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

src/libstd/io/buffered.rs

+33
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! Buffering wrappers for I/O traits
1414
1515
use cmp;
16+
use fmt;
1617
use io::{Reader, Writer, Stream, Buffer, DEFAULT_BUF_SIZE, IoResult};
1718
use iter::{IteratorExt, ExactSizeIterator};
1819
use ops::Drop;
@@ -51,6 +52,13 @@ pub struct BufferedReader<R> {
5152
cap: uint,
5253
}
5354

55+
impl<R> fmt::Show for BufferedReader<R> where R: fmt::Show {
56+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
57+
write!(fmt, "BufferedReader {{ reader: {:?}, buffer: {}/{} }}",
58+
self.inner, self.cap - self.pos, self.buf.len())
59+
}
60+
}
61+
5462
impl<R: Reader> BufferedReader<R> {
5563
/// Creates a new `BufferedReader` with the specified buffer capacity
5664
pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
@@ -148,6 +156,13 @@ pub struct BufferedWriter<W> {
148156
pos: uint
149157
}
150158

159+
impl<W> fmt::Show for BufferedWriter<W> where W: fmt::Show {
160+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
161+
write!(fmt, "BufferedWriter {{ writer: {:?}, buffer: {}/{} }}",
162+
self.inner.as_ref().unwrap(), self.pos, self.buf.len())
163+
}
164+
}
165+
151166
impl<W: Writer> BufferedWriter<W> {
152167
/// Creates a new `BufferedWriter` with the specified buffer capacity
153168
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
@@ -235,6 +250,13 @@ pub struct LineBufferedWriter<W> {
235250
inner: BufferedWriter<W>,
236251
}
237252

253+
impl<W> fmt::Show for LineBufferedWriter<W> where W: fmt::Show {
254+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
255+
write!(fmt, "LineBufferedWriter {{ writer: {:?}, buffer: {}/{} }}",
256+
self.inner.inner, self.inner.pos, self.inner.buf.len())
257+
}
258+
}
259+
238260
impl<W: Writer> LineBufferedWriter<W> {
239261
/// Creates a new `LineBufferedWriter`
240262
pub fn new(inner: W) -> LineBufferedWriter<W> {
@@ -318,6 +340,17 @@ pub struct BufferedStream<S> {
318340
inner: BufferedReader<InternalBufferedWriter<S>>
319341
}
320342

343+
impl<S> fmt::Show for BufferedStream<S> where S: fmt::Show {
344+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
345+
let reader = &self.inner;
346+
let writer = &self.inner.inner.0;
347+
write!(fmt, "BufferedStream {{ stream: {:?}, write_buffer: {}/{}, read_buffer: {}/{} }}",
348+
writer.inner,
349+
writer.pos, writer.buf.len(),
350+
reader.cap - reader.pos, reader.buf.len())
351+
}
352+
}
353+
321354
impl<S: Stream> BufferedStream<S> {
322355
/// Creates a new buffered stream with explicitly listed capacities for the
323356
/// reader/writer buffer.

src/libstd/io/util.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use io;
1616
use slice::bytes::MutableByteVector;
1717

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

7980
/// A `Writer` which ignores bytes written to it, like /dev/null.
80-
#[derive(Copy)]
81+
#[derive(Copy, Show)]
8182
pub struct NullWriter;
8283

8384
impl Writer for NullWriter {
@@ -86,7 +87,7 @@ impl Writer for NullWriter {
8687
}
8788

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

9293
impl Reader for ZeroReader {
@@ -107,7 +108,7 @@ impl Buffer for ZeroReader {
107108
}
108109

109110
/// A `Reader` which is always at EOF, like /dev/null.
110-
#[derive(Copy)]
111+
#[derive(Copy, Show)]
111112
pub struct NullReader;
112113

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

135-
impl MultiWriter {
137+
impl<W> MultiWriter<W> where W: Writer {
136138
/// Creates a new `MultiWriter`
137-
pub fn new(writers: Vec<Box<Writer+'static>>) -> MultiWriter {
139+
pub fn new(writers: Vec<W>) -> MultiWriter<W> {
138140
MultiWriter { writers: writers }
139141
}
140142
}
141143

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

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

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

241244
/// An adaptor converting an `Iterator<u8>` to a `Reader`.
242-
#[derive(Clone)]
245+
#[derive(Clone, Show)]
243246
pub struct IterReader<T> {
244247
iter: T,
245248
}

0 commit comments

Comments
 (0)