Skip to content

Commit 56cf09c

Browse files
committed
Some cleanup in std::io::buffered
`Vec` is now used for the internal buffer instead of `~[]`. Some module level documentation somehow ended up attached to `BufferedReader` so I fixed that as well.
1 parent 6f430c4 commit 56cf09c

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/libstd/io/buffered.rs

+24-15
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ use option::{Some, None, Option};
1919
use result::{Ok, Err};
2020
use slice::{OwnedVector, ImmutableVector, MutableVector};
2121
use slice;
22+
use vec::Vec;
2223

2324
/// Wraps a Reader and buffers input from it
2425
///
25-
/// It can be excessively inefficient to work directly with a `Reader` or
26-
/// `Writer`. Every call to `read` or `write` on `TcpStream` results in a
27-
/// system call, for example. This module provides structures that wrap
28-
/// `Readers`, `Writers`, and `Streams` and buffer input and output to them.
26+
/// It can be excessively inefficient to work directly with a `Reader`. For
27+
/// example, every call to `read` on `TcpStream` results in a system call. A
28+
/// `BufferedReader` performs large, infrequent reads on the underlying
29+
/// `Reader` and maintains an in-memory buffer of the results.
2930
///
3031
/// # Example
3132
///
@@ -43,10 +44,9 @@ use slice;
4344
/// ```
4445
pub struct BufferedReader<R> {
4546
priv inner: R,
46-
priv buf: ~[u8],
47+
priv buf: Vec<u8>,
4748
priv pos: uint,
4849
priv cap: uint,
49-
priv eof: bool,
5050
}
5151

5252
impl<R: Reader> BufferedReader<R> {
@@ -58,14 +58,13 @@ impl<R: Reader> BufferedReader<R> {
5858
// everything up-front. This allows creation of BufferedReader instances
5959
// to be very cheap (large mallocs are not nearly as expensive as large
6060
// callocs).
61-
let mut buf = slice::with_capacity(cap);
61+
let mut buf = Vec::with_capacity(cap);
6262
unsafe { buf.set_len(cap); }
6363
BufferedReader {
6464
inner: inner,
6565
buf: buf,
6666
pos: 0,
6767
cap: 0,
68-
eof: false,
6968
}
7069
}
7170

@@ -80,7 +79,7 @@ impl<R: Reader> BufferedReader<R> {
8079
/// underlying reader because that could possibly corrupt the buffer.
8180
pub fn get_ref<'a>(&'a self) -> &'a R { &self.inner }
8281

83-
/// Unwraps this buffer, returning the underlying reader.
82+
/// Unwraps this `BufferedReader`, returning the underlying reader.
8483
///
8584
/// Note that any leftover data in the internal buffer is lost.
8685
pub fn unwrap(self) -> R { self.inner }
@@ -89,7 +88,7 @@ impl<R: Reader> BufferedReader<R> {
8988
impl<R: Reader> Buffer for BufferedReader<R> {
9089
fn fill<'a>(&'a mut self) -> IoResult<&'a [u8]> {
9190
if self.pos == self.cap {
92-
self.cap = try!(self.inner.read(self.buf));
91+
self.cap = try!(self.inner.read(self.buf.as_mut_slice()));
9392
self.pos = 0;
9493
}
9594
Ok(self.buf.slice(self.pos, self.cap))
@@ -116,6 +115,11 @@ impl<R: Reader> Reader for BufferedReader<R> {
116115

117116
/// Wraps a Writer and buffers output to it
118117
///
118+
/// It can be excessively inefficient to work directly with a `Writer`. For
119+
/// example, every call to `write` on `TcpStream` results in a system call. A
120+
/// `BufferedWriter` keeps an in memory buffer of data and writes it to the
121+
/// underlying `Writer` in large, infrequent batches.
122+
///
119123
/// This writer will be flushed when it is dropped.
120124
///
121125
/// # Example
@@ -132,15 +136,15 @@ impl<R: Reader> Reader for BufferedReader<R> {
132136
/// ```
133137
pub struct BufferedWriter<W> {
134138
priv inner: Option<W>,
135-
priv buf: ~[u8],
139+
priv buf: Vec<u8>,
136140
priv pos: uint
137141
}
138142

139143
impl<W: Writer> BufferedWriter<W> {
140144
/// Creates a new `BufferedWriter` with the specified buffer capacity
141145
pub fn with_capacity(cap: uint, inner: W) -> BufferedWriter<W> {
142146
// See comments in BufferedReader for why this uses unsafe code.
143-
let mut buf = slice::with_capacity(cap);
147+
let mut buf = Vec::with_capacity(cap);
144148
unsafe { buf.set_len(cap); }
145149
BufferedWriter {
146150
inner: Some(inner),
@@ -170,7 +174,7 @@ impl<W: Writer> BufferedWriter<W> {
170174
/// underlying reader because that could possibly corrupt the buffer.
171175
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
172176

173-
/// Unwraps this buffer, returning the underlying writer.
177+
/// Unwraps this `BufferedWriter`, returning the underlying writer.
174178
///
175179
/// The buffer is flushed before returning the writer.
176180
pub fn unwrap(mut self) -> W {
@@ -234,7 +238,7 @@ impl<W: Writer> LineBufferedWriter<W> {
234238
/// underlying reader because that could possibly corrupt the buffer.
235239
pub fn get_ref<'a>(&'a self) -> &'a W { self.inner.get_ref() }
236240

237-
/// Unwraps this buffer, returning the underlying writer.
241+
/// Unwraps this `LineBufferedWriter`, returning the underlying writer.
238242
///
239243
/// The internal buffer is flushed before returning the writer.
240244
pub fn unwrap(self) -> W { self.inner.unwrap() }
@@ -273,6 +277,11 @@ impl<W: Reader> Reader for InternalBufferedWriter<W> {
273277

274278
/// Wraps a Stream and buffers input and output to and from it.
275279
///
280+
/// It can be excessively inefficient to work directly with a `Stream`. For
281+
/// example, every call to `read` or `write` on `TcpStream` results in a system
282+
/// call. A `BufferedStream` keeps in memory buffers of data, making large,
283+
/// infrequent calls to `read` and `write` on the underlying `Stream`.
284+
///
276285
/// The output half will be flushed when this stream is dropped.
277286
///
278287
/// # Example
@@ -325,7 +334,7 @@ impl<S: Stream> BufferedStream<S> {
325334
w.get_ref()
326335
}
327336

328-
/// Unwraps this buffer, returning the underlying stream.
337+
/// Unwraps this `BufferedStream`, returning the underlying stream.
329338
///
330339
/// The internal buffer is flushed before returning the stream. Any leftover
331340
/// data in the read buffer is lost.

0 commit comments

Comments
 (0)