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

pub trait Reader {
    fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>;

    fn read_at_least(&mut self, min: usize, buf: &mut [u8]) -> IoResult<usize> { ... }
    fn read_byte(&mut self) -> IoResult<u8> { ... }
    fn push(&mut self, len: usize, buf: &mut Vec<u8>) -> IoResult<usize> { ... }
    fn push_at_least(&mut self, min: usize, len: usize, buf: &mut Vec<u8>) -> IoResult<usize> { ... }
    fn read_exact(&mut self, len: usize) -> IoResult<Vec<u8>> { ... }
    fn read_to_end(&mut self) -> IoResult<Vec<u8>> { ... }
    fn read_to_string(&mut self) -> IoResult<String> { ... }
    fn read_le_uint_n(&mut self, nbytes: usize) -> IoResult<u64> { ... }
    fn read_le_int_n(&mut self, nbytes: usize) -> IoResult<i64> { ... }
    fn read_be_uint_n(&mut self, nbytes: usize) -> IoResult<u64> { ... }
    fn read_be_int_n(&mut self, nbytes: usize) -> IoResult<i64> { ... }
    fn read_le_uint(&mut self) -> IoResult<usize> { ... }
    fn read_le_int(&mut self) -> IoResult<isize> { ... }
    fn read_be_uint(&mut self) -> IoResult<usize> { ... }
    fn read_be_int(&mut self) -> IoResult<isize> { ... }
    fn read_be_u64(&mut self) -> IoResult<u64> { ... }
    fn read_be_u32(&mut self) -> IoResult<u32> { ... }
    fn read_be_u16(&mut self) -> IoResult<u16> { ... }
    fn read_be_i64(&mut self) -> IoResult<i64> { ... }
    fn read_be_i32(&mut self) -> IoResult<i32> { ... }
    fn read_be_i16(&mut self) -> IoResult<i16> { ... }
    fn read_be_f64(&mut self) -> IoResult<f64> { ... }
    fn read_be_f32(&mut self) -> IoResult<f32> { ... }
    fn read_le_u64(&mut self) -> IoResult<u64> { ... }
    fn read_le_u32(&mut self) -> IoResult<u32> { ... }
    fn read_le_u16(&mut self) -> IoResult<u16> { ... }
    fn read_le_i64(&mut self) -> IoResult<i64> { ... }
    fn read_le_i32(&mut self) -> IoResult<i32> { ... }
    fn read_le_i16(&mut self) -> IoResult<i16> { ... }
    fn read_le_f64(&mut self) -> IoResult<f64> { ... }
    fn read_le_f32(&mut self) -> IoResult<f32> { ... }
    fn read_u8(&mut self) -> IoResult<u8> { ... }
    fn read_i8(&mut self) -> IoResult<i8> { ... }
}

A trait for objects which are byte-oriented streams. Readers are defined by one method, read. This function will block until data is available, filling in the provided buffer with any data read.

Readers are intended to be composable with one another. Many objects throughout the I/O and related libraries take and provide types which implement the Reader trait.

Required Methods

fn read(&mut self, buf: &mut [u8]) -> IoResult<usize>

Read bytes, up to the length of buf and place them in buf. Returns the number of bytes read. The number of bytes read may be less than the number requested, even 0. Returns Err on EOF.

Error

If an error occurs during this I/O operation, then it is returned as Err(IoError). Note that end-of-file is considered an error, and can be inspected for in the error's kind field. Also note that reading 0 bytes is not considered an error in all circumstances

Implementation Note

When implementing this method on a new Reader, you are strongly encouraged not to return 0 if you can avoid it.

Provided Methods

fn read_at_least(&mut self, min: usize, buf: &mut [u8]) -> IoResult<usize>

Reads at least min bytes and places them in buf. Returns the number of bytes read.

This will continue to call read until at least min bytes have been read. If read returns 0 too many times, NoProgress will be returned.

Error

If an error occurs at any point, that error is returned, and no further bytes are read.

fn read_byte(&mut self) -> IoResult<u8>

Reads a single byte. Returns Err on EOF.

fn push(&mut self, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

Reads up to len bytes and appends them to a vector. Returns the number of bytes read. The number of bytes read may be less than the number requested, even 0. Returns Err on EOF.

Error

If an error occurs during this I/O operation, then it is returned as Err(IoError). See read() for more details.

fn push_at_least(&mut self, min: usize, len: usize, buf: &mut Vec<u8>) -> IoResult<usize>

Reads at least min bytes, but no more than len, and appends them to a vector. Returns the number of bytes read.

This will continue to call read until at least min bytes have been read. If read returns 0 too many times, NoProgress will be returned.

Error

If an error occurs at any point, that error is returned, and no further bytes are read.

fn read_exact(&mut self, len: usize) -> IoResult<Vec<u8>>

Reads exactly len bytes and gives you back a new vector of length len

Error

Fails with the same conditions as read. Additionally returns error on EOF. Note that if an error is returned, then some number of bytes may have already been consumed from the underlying reader, and they are lost (not returned as part of the error). If this is unacceptable, then it is recommended to use the push_at_least or read methods.

fn read_to_end(&mut self) -> IoResult<Vec<u8>>

Reads all remaining bytes from the stream.

Error

Returns any non-EOF error immediately. Previously read bytes are discarded when an error is returned.

When EOF is encountered, all bytes read up to that point are returned.

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

Reads all of the remaining bytes of this stream, interpreting them as a UTF-8 encoded stream. The corresponding string is returned.

Error

This function returns all of the same errors as read_to_end with an additional error if the reader's contents are not a valid sequence of UTF-8 bytes.

fn read_le_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

Reads n little-endian unsigned integer bytes.

n must be between 1 and 8, inclusive.

fn read_le_int_n(&mut self, nbytes: usize) -> IoResult<i64>

Reads n little-endian signed integer bytes.

n must be between 1 and 8, inclusive.

fn read_be_uint_n(&mut self, nbytes: usize) -> IoResult<u64>

Reads n big-endian unsigned integer bytes.

n must be between 1 and 8, inclusive.

fn read_be_int_n(&mut self, nbytes: usize) -> IoResult<i64>

Reads n big-endian signed integer bytes.

n must be between 1 and 8, inclusive.

fn read_le_uint(&mut self) -> IoResult<usize>

Reads a little-endian unsigned integer.

The number of bytes returned is system-dependent.

fn read_le_int(&mut self) -> IoResult<isize>

Reads a little-endian integer.

The number of bytes returned is system-dependent.

fn read_be_uint(&mut self) -> IoResult<usize>

Reads a big-endian unsigned integer.

The number of bytes returned is system-dependent.

fn read_be_int(&mut self) -> IoResult<isize>

Reads a big-endian integer.

The number of bytes returned is system-dependent.

fn read_be_u64(&mut self) -> IoResult<u64>

Reads a big-endian u64.

u64s are 8 bytes long.

fn read_be_u32(&mut self) -> IoResult<u32>

Reads a big-endian u32.

u32s are 4 bytes long.

fn read_be_u16(&mut self) -> IoResult<u16>

Reads a big-endian u16.

u16s are 2 bytes long.

fn read_be_i64(&mut self) -> IoResult<i64>

Reads a big-endian i64.

i64s are 8 bytes long.

fn read_be_i32(&mut self) -> IoResult<i32>

Reads a big-endian i32.

i32s are 4 bytes long.

fn read_be_i16(&mut self) -> IoResult<i16>

Reads a big-endian i16.

i16s are 2 bytes long.

fn read_be_f64(&mut self) -> IoResult<f64>

Reads a big-endian f64.

f64s are 8 byte, IEEE754 double-precision floating point numbers.

fn read_be_f32(&mut self) -> IoResult<f32>

Reads a big-endian f32.

f32s are 4 byte, IEEE754 single-precision floating point numbers.

fn read_le_u64(&mut self) -> IoResult<u64>

Reads a little-endian u64.

u64s are 8 bytes long.

fn read_le_u32(&mut self) -> IoResult<u32>

Reads a little-endian u32.

u32s are 4 bytes long.

fn read_le_u16(&mut self) -> IoResult<u16>

Reads a little-endian u16.

u16s are 2 bytes long.

fn read_le_i64(&mut self) -> IoResult<i64>

Reads a little-endian i64.

i64s are 8 bytes long.

fn read_le_i32(&mut self) -> IoResult<i32>

Reads a little-endian i32.

i32s are 4 bytes long.

fn read_le_i16(&mut self) -> IoResult<i16>

Reads a little-endian i16.

i16s are 2 bytes long.

fn read_le_f64(&mut self) -> IoResult<f64>

Reads a little-endian f64.

f64s are 8 byte, IEEE754 double-precision floating point numbers.

fn read_le_f32(&mut self) -> IoResult<f32>

Reads a little-endian f32.

f32s are 4 byte, IEEE754 single-precision floating point numbers.

fn read_u8(&mut self) -> IoResult<u8>

Read a u8.

u8s are 1 byte.

fn read_i8(&mut self) -> IoResult<i8>

Read an i8.

i8s are 1 byte.

Implementors