Skip to content

Commit 7bc092f

Browse files
committed
Introduce an io::Buffer trait
This trait is meant to abstract whether a reader is actually implemented with an underlying buffer. For all readers which are implemented as such, we can efficiently implement things like read_char, read_line, read_until, etc. There are two required methods for managing the internal buffer, and otherwise read_line and friends can all become default methods. Closes #10334
1 parent 825b127 commit 7bc092f

File tree

4 files changed

+114
-102
lines changed

4 files changed

+114
-102
lines changed

src/libstd/io/buffered.rs

Lines changed: 26 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ use prelude::*;
5555

5656
use num;
5757
use vec;
58-
use str;
59-
use super::{Reader, Writer, Stream, Decorator};
58+
use super::{Stream, Decorator};
6059

6160
// libuv recommends 64k buffers to maximize throughput
6261
// https://groups.google.com/forum/#!topic/libuv/oQO1HJAIDdA
@@ -93,45 +92,10 @@ impl<R: Reader> BufferedReader<R> {
9392
pub fn new(inner: R) -> BufferedReader<R> {
9493
BufferedReader::with_capacity(DEFAULT_CAPACITY, inner)
9594
}
95+
}
9696

97-
/// Reads the next line of input, interpreted as a sequence of utf-8
98-
/// encoded unicode codepoints. If a newline is encountered, then the
99-
/// newline is contained in the returned string.
100-
pub fn read_line(&mut self) -> Option<~str> {
101-
self.read_until('\n' as u8).map(str::from_utf8_owned)
102-
}
103-
104-
/// Reads a sequence of bytes leading up to a specified delimeter. Once the
105-
/// specified byte is encountered, reading ceases and the bytes up to and
106-
/// including the delimiter are returned.
107-
pub fn read_until(&mut self, byte: u8) -> Option<~[u8]> {
108-
let mut res = ~[];
109-
let mut used;
110-
loop {
111-
{
112-
let available = self.fill_buffer();
113-
match available.iter().position(|&b| b == byte) {
114-
Some(i) => {
115-
res.push_all(available.slice_to(i + 1));
116-
used = i + 1;
117-
break
118-
}
119-
None => {
120-
res.push_all(available);
121-
used = available.len();
122-
}
123-
}
124-
}
125-
if used == 0 {
126-
break
127-
}
128-
self.pos += used;
129-
}
130-
self.pos += used;
131-
return if res.len() == 0 {None} else {Some(res)};
132-
}
133-
134-
fn fill_buffer<'a>(&'a mut self) -> &'a [u8] {
97+
impl<R: Reader> Buffer for BufferedReader<R> {
98+
fn fill<'a>(&'a mut self) -> &'a [u8] {
13599
if self.pos == self.cap {
136100
match self.inner.read(self.buf) {
137101
Some(cap) => {
@@ -143,12 +107,17 @@ impl<R: Reader> BufferedReader<R> {
143107
}
144108
return self.buf.slice(self.pos, self.cap);
145109
}
110+
111+
fn consume(&mut self, amt: uint) {
112+
self.pos += amt;
113+
assert!(self.pos <= self.cap);
114+
}
146115
}
147116

148117
impl<R: Reader> Reader for BufferedReader<R> {
149118
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
150119
let nread = {
151-
let available = self.fill_buffer();
120+
let available = self.fill();
152121
if available.len() == 0 {
153122
return None;
154123
}
@@ -166,17 +135,9 @@ impl<R: Reader> Reader for BufferedReader<R> {
166135
}
167136

168137
impl<R: Reader> Decorator<R> for BufferedReader<R> {
169-
fn inner(self) -> R {
170-
self.inner
171-
}
172-
173-
fn inner_ref<'a>(&'a self) -> &'a R {
174-
&self.inner
175-
}
176-
177-
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R {
178-
&mut self.inner
179-
}
138+
fn inner(self) -> R { self.inner }
139+
fn inner_ref<'a>(&'a self) -> &'a R { &self.inner }
140+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut R { &mut self.inner }
180141
}
181142

182143
/// Wraps a Writer and buffers output to it
@@ -279,13 +240,8 @@ impl<W: Writer> Decorator<W> for LineBufferedWriter<W> {
279240
struct InternalBufferedWriter<W>(BufferedWriter<W>);
280241

281242
impl<W: Reader> Reader for InternalBufferedWriter<W> {
282-
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
283-
self.inner.read(buf)
284-
}
285-
286-
fn eof(&mut self) -> bool {
287-
self.inner.eof()
288-
}
243+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.inner.read(buf) }
244+
fn eof(&mut self) -> bool { self.inner.eof() }
289245
}
290246

291247
/// Wraps a Stream and buffers input and output to and from it
@@ -311,35 +267,24 @@ impl<S: Stream> BufferedStream<S> {
311267
}
312268
}
313269

314-
impl<S: Stream> Reader for BufferedStream<S> {
315-
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
316-
self.inner.read(buf)
317-
}
270+
impl<S: Stream> Buffer for BufferedStream<S> {
271+
fn fill<'a>(&'a mut self) -> &'a [u8] { self.inner.fill() }
272+
fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
273+
}
318274

319-
fn eof(&mut self) -> bool {
320-
self.inner.eof()
321-
}
275+
impl<S: Stream> Reader for BufferedStream<S> {
276+
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.inner.read(buf) }
277+
fn eof(&mut self) -> bool { self.inner.eof() }
322278
}
323279

324280
impl<S: Stream> Writer for BufferedStream<S> {
325-
fn write(&mut self, buf: &[u8]) {
326-
self.inner.inner.write(buf)
327-
}
328-
329-
fn flush(&mut self) {
330-
self.inner.inner.flush()
331-
}
281+
fn write(&mut self, buf: &[u8]) { self.inner.inner.write(buf) }
282+
fn flush(&mut self) { self.inner.inner.flush() }
332283
}
333284

334285
impl<S: Stream> Decorator<S> for BufferedStream<S> {
335-
fn inner(self) -> S {
336-
self.inner.inner.inner()
337-
}
338-
339-
fn inner_ref<'a>(&'a self) -> &'a S {
340-
self.inner.inner.inner_ref()
341-
}
342-
286+
fn inner(self) -> S { self.inner.inner.inner() }
287+
fn inner_ref<'a>(&'a self) -> &'a S { self.inner.inner.inner_ref() }
343288
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut S {
344289
self.inner.inner.inner_mut_ref()
345290
}

src/libstd/io/mem.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,29 +119,18 @@ impl Reader for MemReader {
119119

120120
impl Seek for MemReader {
121121
fn tell(&self) -> u64 { self.pos as u64 }
122-
123122
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
124123
}
125124

126-
impl Decorator<~[u8]> for MemReader {
127-
128-
fn inner(self) -> ~[u8] {
129-
match self {
130-
MemReader { buf: buf, _ } => buf
131-
}
132-
}
133-
134-
fn inner_ref<'a>(&'a self) -> &'a ~[u8] {
135-
match *self {
136-
MemReader { buf: ref buf, _ } => buf
137-
}
138-
}
125+
impl Buffer for MemReader {
126+
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
127+
fn consume(&mut self, amt: uint) { self.pos += amt; }
128+
}
139129

140-
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] {
141-
match *self {
142-
MemReader { buf: ref mut buf, _ } => buf
143-
}
144-
}
130+
impl Decorator<~[u8]> for MemReader {
131+
fn inner(self) -> ~[u8] { self.buf }
132+
fn inner_ref<'a>(&'a self) -> &'a ~[u8] { &self.buf }
133+
fn inner_mut_ref<'a>(&'a mut self) -> &'a mut ~[u8] { &mut self.buf }
145134
}
146135

147136

@@ -214,6 +203,11 @@ impl<'self> Seek for BufReader<'self> {
214203
fn seek(&mut self, _pos: i64, _style: SeekStyle) { fail!() }
215204
}
216205

206+
impl<'self> Buffer for BufReader<'self> {
207+
fn fill<'a>(&'a mut self) -> &'a [u8] { self.buf.slice_from(self.pos) }
208+
fn consume(&mut self, amt: uint) { self.pos += amt; }
209+
}
210+
217211
///Calls a function with a MemWriter and returns
218212
///the writer's stored vector.
219213
pub fn with_mem_writer(writeFn:&fn(&mut MemWriter)) -> ~[u8] {

src/libstd/io/mod.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,12 @@ use iter::Iterator;
247247
use option::{Option, Some, None};
248248
use path::Path;
249249
use result::{Ok, Err, Result};
250+
use str;
250251
use str::{StrSlice, OwnedStr};
251252
use to_str::ToStr;
252253
use uint;
253254
use unstable::finally::Finally;
254-
use vec::{OwnedVector, MutableVector};
255+
use vec::{OwnedVector, MutableVector, ImmutableVector, OwnedCopyableVector};
255256
use vec;
256257

257258
// Reexports
@@ -977,6 +978,78 @@ pub trait Stream: Reader + Writer { }
977978

978979
impl<T: Reader + Writer> Stream for T {}
979980

981+
/// A Buffer is a type of reader which has some form of internal buffering to
982+
/// allow certain kinds of reading operations to be more optimized than others.
983+
/// This type extends the `Reader` trait with a few methods that are not
984+
/// possible to reasonably implement with purely a read interface.
985+
pub trait Buffer: Reader {
986+
/// Fills the internal buffer of this object, returning the buffer contents.
987+
/// Note that none of the contents will be "read" in the sense that later
988+
/// calling `read` may return the same contents.
989+
///
990+
/// The `consume` function must be called with the number of bytes that are
991+
/// consumed from this buffer returned to ensure that the bytes are never
992+
/// returned twice.
993+
///
994+
/// # Failure
995+
///
996+
/// This function will raise on the `io_error` condition if a read error is
997+
/// encountered.
998+
fn fill<'a>(&'a mut self) -> &'a [u8];
999+
1000+
/// Tells this buffer that `amt` bytes have been consumed from the buffer,
1001+
/// so they should no longer be returned in calls to `fill` or `read`.
1002+
fn consume(&mut self, amt: uint);
1003+
1004+
/// Reads the next line of input, interpreted as a sequence of utf-8
1005+
/// encoded unicode codepoints. If a newline is encountered, then the
1006+
/// newline is contained in the returned string.
1007+
///
1008+
/// # Failure
1009+
///
1010+
/// This function will raise on the `io_error` condition if a read error is
1011+
/// encountered. The task will also fail if sequence of bytes leading up to
1012+
/// the newline character are not valid utf-8.
1013+
fn read_line(&mut self) -> Option<~str> {
1014+
self.read_until('\n' as u8).map(str::from_utf8_owned)
1015+
}
1016+
1017+
/// Reads a sequence of bytes leading up to a specified delimeter. Once the
1018+
/// specified byte is encountered, reading ceases and the bytes up to and
1019+
/// including the delimiter are returned.
1020+
///
1021+
/// # Failure
1022+
///
1023+
/// This function will raise on the `io_error` condition if a read error is
1024+
/// encountered.
1025+
fn read_until(&mut self, byte: u8) -> Option<~[u8]> {
1026+
let mut res = ~[];
1027+
let mut used;
1028+
loop {
1029+
{
1030+
let available = self.fill();
1031+
match available.iter().position(|&b| b == byte) {
1032+
Some(i) => {
1033+
res.push_all(available.slice_to(i + 1));
1034+
used = i + 1;
1035+
break
1036+
}
1037+
None => {
1038+
res.push_all(available);
1039+
used = available.len();
1040+
}
1041+
}
1042+
}
1043+
if used == 0 {
1044+
break
1045+
}
1046+
self.consume(used);
1047+
}
1048+
self.consume(used);
1049+
return if res.len() == 0 {None} else {Some(res)};
1050+
}
1051+
}
1052+
9801053
pub enum SeekStyle {
9811054
/// Seek from the beginning of the stream
9821055
SeekSet,

src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub use num::{Orderable, Signed, Unsigned, Round};
6767
pub use num::{Primitive, Int, Float, ToStrRadix, ToPrimitive, FromPrimitive};
6868
pub use path::{GenericPath, Path, PosixPath, WindowsPath};
6969
pub use ptr::RawPtr;
70-
pub use io::{Writer, Reader, Seek};
70+
pub use io::{Buffer, Writer, Reader, Seek};
7171
pub use send_str::{SendStr, SendStrOwned, SendStrStatic, IntoSendStr};
7272
pub use str::{Str, StrVector, StrSlice, OwnedStr};
7373
pub use to_bytes::IterBytes;

0 commit comments

Comments
 (0)