Remove eof() from io::Reader

This commit is contained in:
Alex Crichton 2014-01-07 09:47:53 -08:00
parent dd11fe17c7
commit a18282c3d0
16 changed files with 19 additions and 144 deletions

View File

@ -788,7 +788,7 @@ fn parse_type(s: &str, pos: uint, ch: char, tm: &mut Tm)
}
}
if pos == len && rdr.eof() {
if pos == len && rdr.tell() as uint == format.len() {
Ok(Tm {
tm_sec: tm.tm_sec,
tm_min: tm.tm_min,

View File

@ -114,7 +114,6 @@ impl io::Reader for FileDesc {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
match self.inner_read(buf) { Ok(n) => Some(n), Err(..) => None }
}
fn eof(&mut self) -> bool { false }
}
impl io::Writer for FileDesc {

View File

@ -66,7 +66,8 @@ pub struct BufferedReader<R> {
priv inner: R,
priv buf: ~[u8],
priv pos: uint,
priv cap: uint
priv cap: uint,
priv eof: bool,
}
impl<R: Reader> BufferedReader<R> {
@ -84,7 +85,8 @@ pub fn with_capacity(cap: uint, inner: R) -> BufferedReader<R> {
inner: inner,
buf: buf,
pos: 0,
cap: 0
cap: 0,
eof: false,
}
}
@ -113,7 +115,7 @@ fn fill<'a>(&'a mut self) -> &'a [u8] {
self.pos = 0;
self.cap = cap;
}
None => {}
None => { self.eof = true; }
}
}
return self.buf.slice(self.pos, self.cap);
@ -134,15 +136,11 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
nread
};
self.pos += nread;
if nread == 0 && self.inner.eof() && buf.len() != 0 {
if nread == 0 && buf.len() != 0 && self.eof {
return None;
}
Some(nread)
}
fn eof(&mut self) -> bool {
self.pos == self.cap && self.inner.eof()
}
}
/// Wraps a Writer and buffers output to it
@ -270,7 +268,6 @@ fn get_mut_ref<'a>(&'a mut self) -> &'a mut BufferedWriter<W> {
impl<W: Reader> Reader for InternalBufferedWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.get_mut_ref().inner.read(buf) }
fn eof(&mut self) -> bool { self.get_mut_ref().inner.eof() }
}
/// Wraps a Stream and buffers input and output to and from it
@ -325,7 +322,6 @@ fn consume(&mut self, amt: uint) { self.inner.consume(amt) }
impl<S: Stream> Reader for BufferedStream<S> {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.inner.read(buf) }
fn eof(&mut self) -> bool { self.inner.eof() }
}
impl<S: Stream> Writer for BufferedStream<S> {
@ -351,10 +347,6 @@ impl Reader for NullStream {
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
None
}
fn eof(&mut self) -> bool {
true
}
}
impl Writer for NullStream {
@ -370,10 +362,6 @@ impl Reader for ShortReader {
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
self.lengths.shift_opt()
}
fn eof(&mut self) -> bool {
self.lengths.len() == 0
}
}
#[test]
@ -385,24 +373,20 @@ fn test_buffered_reader() {
let nread = reader.read(buf);
assert_eq!(Some(2), nread);
assert_eq!([0, 1, 0], buf);
assert!(!reader.eof());
let mut buf = [0];
let nread = reader.read(buf);
assert_eq!(Some(1), nread);
assert_eq!([2], buf);
assert!(!reader.eof());
let mut buf = [0, 0, 0];
let nread = reader.read(buf);
assert_eq!(Some(1), nread);
assert_eq!([3, 0, 0], buf);
assert!(!reader.eof());
let nread = reader.read(buf);
assert_eq!(Some(1), nread);
assert_eq!([4, 0, 0], buf);
assert!(reader.eof());
assert_eq!(None, reader.read(buf));
}
@ -466,13 +450,11 @@ fn write(&mut self, _: &[u8]) {}
impl io::Reader for S {
fn read(&mut self, _: &mut [u8]) -> Option<uint> { None }
fn eof(&mut self) -> bool { true }
}
let mut stream = BufferedStream::new(S);
let mut buf = [];
stream.read(buf);
stream.eof();
stream.write(buf);
stream.flush();
}
@ -539,6 +521,7 @@ fn test_short_reads() {
assert_eq!(reader.read(buf), Some(2));
assert_eq!(reader.read(buf), Some(0));
assert_eq!(reader.read(buf), Some(1));
assert_eq!(reader.read(buf), Some(0));
assert_eq!(reader.read(buf), None);
}

View File

@ -77,8 +77,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
Some(num_read)
}
}
fn eof(&mut self) -> bool { self.closed }
}
/// Allows writing to a chan.
@ -133,22 +131,17 @@ fn test_port_reader() {
let mut reader = PortReader::new(port);
let mut buf = ~[0u8, ..3];
assert_eq!(false, reader.eof());
assert_eq!(Some(0), reader.read([]));
assert_eq!(false, reader.eof());
assert_eq!(Some(3), reader.read(buf));
assert_eq!(false, reader.eof());
assert_eq!(~[1,2,3], buf);
assert_eq!(Some(3), reader.read(buf));
assert_eq!(false, reader.eof());
assert_eq!(~[4,5,6], buf);
assert_eq!(Some(2), reader.read(buf));
assert_eq!(~[7,8,6], buf);
assert_eq!(true, reader.eof());
let mut err = None;
let result = io::io_error::cond.trap(|io::standard_error(k, _, _)| {
@ -158,7 +151,6 @@ fn test_port_reader() {
});
assert_eq!(Some(io::EndOfFile), err);
assert_eq!(None, result);
assert_eq!(true, reader.eof());
assert_eq!(~[7,8,6], buf);
// Ensure it continues to fail in the same way.
@ -170,7 +162,6 @@ fn test_port_reader() {
});
assert_eq!(Some(io::EndOfFile), err);
assert_eq!(None, result);
assert_eq!(true, reader.eof());
assert_eq!(~[7,8,6], buf);
}

View File

@ -150,9 +150,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
Some(1)
}
}
fn eof(&mut self) -> bool {
false
}
}
struct EofReader;
@ -161,9 +158,6 @@ impl Reader for EofReader {
fn read(&mut self, _: &mut [u8]) -> Option<uint> {
None
}
fn eof(&mut self) -> bool {
false
}
}
struct ErroringReader;
@ -173,9 +167,6 @@ fn read(&mut self, _: &mut [u8]) -> Option<uint> {
io_error::cond.raise(placeholder_error());
None
}
fn eof(&mut self) -> bool {
false
}
}
struct PartialReader {
@ -195,9 +186,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
Some(2)
}
}
fn eof(&mut self) -> bool {
false
}
}
struct ErroringLaterReader {
@ -215,9 +203,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
None
}
}
fn eof(&mut self) -> bool {
false
}
}
struct ThreeChunkReader {
@ -240,9 +225,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
None
}
}
fn eof(&mut self) -> bool {
false
}
}
#[test]

View File

@ -49,6 +49,4 @@ pub fn new(inner_reader: R) -> InflateReader<R> {
impl<R: Reader> Reader for InflateReader<R> {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> { fail!() }
fn eof(&mut self) -> bool { fail!() }
}

View File

@ -205,6 +205,14 @@ pub fn datasync(&mut self) {
pub fn truncate(&mut self, size: i64) {
self.fd.truncate(size).map_err(|e| io_error::cond.raise(e));
}
/// Tests whether this stream has reached EOF.
///
/// If true, then this file will no longer continue to return data via
/// `read`.
pub fn eof(&self) -> bool {
self.last_nread == 0
}
}
/// Unlink a file from the underlying filesystem.
@ -597,8 +605,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
}
}
fn eof(&mut self) -> bool { self.last_nread == 0 }
}
impl Writer for File {

View File

@ -124,7 +124,7 @@ pub fn unwrap(self) -> ~[u8] { self.buf }
impl Reader for MemReader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
{ if self.eof() { return None; } }
if self.pos == self.buf.len() { return None }
let write_len = min(buf.len(), self.buf.len() - self.pos);
{
@ -138,8 +138,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
return Some(write_len);
}
fn eof(&mut self) -> bool { self.pos == self.buf.len() }
}
impl Seek for MemReader {
@ -222,7 +220,7 @@ pub fn new<'a>(buf: &'a [u8]) -> BufReader<'a> {
impl<'a> Reader for BufReader<'a> {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
{ if self.eof() { return None; } }
if self.pos == self.buf.len() { return None }
let write_len = min(buf.len(), self.buf.len() - self.pos);
{
@ -236,8 +234,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
return Some(write_len);
}
fn eof(&mut self) -> bool { self.pos == self.buf.len() }
}
impl<'a> Seek for BufReader<'a> {
@ -369,9 +365,7 @@ fn test_mem_reader() {
assert_eq!(buf, [1, 2, 3, 4]);
assert_eq!(reader.read(buf), Some(3));
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
assert!(reader.eof());
assert_eq!(reader.read(buf), None);
assert!(reader.eof());
}
#[test]
@ -391,9 +385,7 @@ fn test_buf_reader() {
assert_eq!(buf, [1, 2, 3, 4]);
assert_eq!(reader.read(buf), Some(3));
assert_eq!(buf.slice(0, 3), [5, 6, 7]);
assert!(reader.eof());
assert_eq!(reader.read(buf), None);
assert!(reader.eof());
}
#[test]

View File

@ -502,26 +502,6 @@ pub trait Reader {
/// Is it actually possible for 0 bytes to be read successfully?
fn read(&mut self, buf: &mut [u8]) -> Option<uint>;
/// Return whether the Reader has reached the end of the stream.
///
/// # Example
///
/// ```rust
/// use std::io;
/// # let _g = ::std::io::ignore_io_error();
/// let mut reader = io::stdin();
///
/// let mut bytes = [0, .. 10];
/// reader.read(bytes);
///
/// if reader.eof() { println("stdin() had at most 10 bytes of data."); }
/// ```
///
/// # Failure
///
/// Returns `true` on failure.
fn eof(&mut self) -> bool;
// Convenient helper methods based on the above methods
/// Reads a single byte. Returns `None` on EOF.
@ -864,12 +844,10 @@ fn read_i8(&mut self) -> i8 {
impl Reader for ~Reader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.read(buf) }
fn eof(&mut self) -> bool { self.eof() }
}
impl<'a> Reader for &'a mut Reader {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.read(buf) }
fn eof(&mut self) -> bool { self.eof() }
}
fn extend_sign(val: u64, nbytes: uint) -> i64 {

View File

@ -67,8 +67,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
}
}
fn eof(&mut self) -> bool { fail!() }
}
impl Writer for TcpStream {

View File

@ -86,8 +86,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
})
}
fn eof(&mut self) -> bool { fail!() }
}
impl Writer for UdpStream {

View File

@ -67,7 +67,6 @@ pub fn connect<P: ToCStr>(path: &P) -> Option<UnixStream> {
impl Reader for UnixStream {
fn read(&mut self, buf: &mut [u8]) -> Option<uint> { self.obj.read(buf) }
fn eof(&mut self) -> bool { self.obj.eof() }
}
impl Writer for UnixStream {

View File

@ -48,16 +48,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
}
}
fn eof(&mut self) -> bool {
match *self {
Some(ref mut reader) => reader.eof(),
None => {
io_error::cond.raise(prev_io_error());
true
}
}
}
}
impl<S: Seek> Seek for Option<S> {
@ -145,7 +135,6 @@ fn test_option_reader() {
let mut buf = [0, 0];
reader.read(buf);
assert_eq!(buf, [0, 1]);
assert!(!reader.eof());
}
#[test]
@ -161,14 +150,5 @@ fn test_option_reader_error() {
reader.read(buf);
});
assert!(called);
let mut called = false;
io_error::cond.trap(|err| {
assert_eq!(err.kind, PreviousIoError);
called = true;
}).inside(|| {
assert!(reader.eof());
});
assert!(called);
}
}

View File

@ -66,8 +66,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
}
}
fn eof(&mut self) -> bool { false }
}
impl Writer for PipeStream {

View File

@ -272,8 +272,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
}
}
fn eof(&mut self) -> bool { false }
}
/// Representation of a writer to a standard output stream

View File

@ -36,10 +36,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
len
})
}
fn eof(&mut self) -> bool {
self.limit == 0 || self.inner.eof()
}
}
/// A `Writer` which ignores bytes written to it, like /dev/null.
@ -59,11 +55,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
buf.set_memory(0);
Some(buf.len())
}
#[inline]
fn eof(&mut self) -> bool {
false
}
}
/// A `Reader` which is always at EOF, like /dev/null.
@ -74,11 +65,6 @@ impl Reader for NullReader {
fn read(&mut self, _buf: &mut [u8]) -> Option<uint> {
None
}
#[inline]
fn eof(&mut self) -> bool {
true
}
}
/// A `Writer` which multiplexes writes to a set of `Writers`.
@ -140,10 +126,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
}
None
}
fn eof(&mut self) -> bool {
self.cur_reader.is_none()
}
}
/// A `Reader` which forwards input from another `Reader`, passing it along to
@ -174,10 +156,6 @@ fn read(&mut self, buf: &mut [u8]) -> Option<uint> {
len
})
}
fn eof(&mut self) -> bool {
self.reader.eof()
}
}
/// Copies all data from a `Reader` to a `Writer`.
@ -204,7 +182,6 @@ fn test_bounded_reader_unlimited() {
let mut r = LimitReader::new(&mut r, 4);
assert_eq!(~[0, 1, 2], r.read_to_end());
}
assert!(r.eof());
}
#[test]
@ -238,7 +215,6 @@ fn test_null_reader() {
let mut r = NullReader;
let mut buf = ~[0];
assert_eq!(r.read(buf), None);
assert!(r.eof());
}
#[test]
@ -289,7 +265,6 @@ fn test_copy() {
let mut r = MemReader::new(~[0, 1, 2, 3, 4]);
let mut w = MemWriter::new();
copy(&mut r, &mut w);
assert!(r.eof());
assert_eq!(~[0, 1, 2, 3, 4], w.unwrap());
}
}