Trait std::old_io::BufferUnstable [-] [+] [src]

pub trait Buffer: Reader {
    fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>;
    fn consume(&mut self, amt: usize);

    fn read_line(&mut self) -> IoResult<String> { ... }
    fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>> { ... }
    fn read_char(&mut self) -> IoResult<char> { ... }
}

A Buffer is a type of reader which has some form of internal buffering to allow certain kinds of reading operations to be more optimized than others. This type extends the Reader trait with a few methods that are not possible to reasonably implement with purely a read interface.

Required Methods

fn fill_buf<'a>(&'a mut self) -> IoResult<&'a [u8]>

Fills the internal buffer of this object, returning the buffer contents. Note that none of the contents will be "read" in the sense that later calling read may return the same contents.

The consume function must be called with the number of bytes that are consumed from this buffer returned to ensure that the bytes are never returned twice.

Error

This function will return an I/O error if the underlying reader was read, but returned an error. Note that it is not an error to return a 0-length buffer.

fn consume(&mut self, amt: usize)

Tells this buffer that amt bytes have been consumed from the buffer, so they should no longer be returned in calls to read.

Provided Methods

fn read_line(&mut self) -> IoResult<String>

Reads the next line of input, interpreted as a sequence of UTF-8 encoded Unicode codepoints. If a newline is encountered, then the newline is contained in the returned string.

Example

fn main() { use std::old_io::BufReader; let mut reader = BufReader::new(b"hello\nworld"); assert_eq!("hello\n", &*reader.read_line().unwrap()); }
use std::old_io::BufReader;

let mut reader = BufReader::new(b"hello\nworld");
assert_eq!("hello\n", &*reader.read_line().unwrap());

Error

This function has the same error semantics as read_until:

  • All non-EOF errors will be returned immediately
  • If an error is returned previously consumed bytes are lost
  • EOF is only returned if no bytes have been read
  • Reach EOF may mean that the delimiter is not present in the return value

Additionally, this function can fail if the line of input read is not a valid UTF-8 sequence of bytes.

fn read_until(&mut self, byte: u8) -> IoResult<Vec<u8>>

Reads a sequence of bytes leading up to a specified delimiter. Once the specified byte is encountered, reading ceases and the bytes up to and including the delimiter are returned.

Error

If any I/O error is encountered other than EOF, the error is immediately returned. Note that this may discard bytes which have already been read, and those bytes will not be returned. It is recommended to use other methods if this case is worrying.

If EOF is encountered, then this function will return EOF if 0 bytes have been read, otherwise the pending byte buffer is returned. This is the reason that the byte buffer returned may not always contain the delimiter.

fn read_char(&mut self) -> IoResult<char>

Reads the next utf8-encoded character from the underlying stream.

Error

If an I/O error occurs, or EOF, then this function will return Err. This function will also return error if the stream does not contain a valid utf-8 encoded codepoint as the next few bytes in the stream.

Implementors