Trait std::io::ReadUnstable [-] [+] [src]

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

    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<()> { ... }
    fn read_to_string(&mut self, buf: &mut String) -> Result<()> { ... }
}

A trait for objects which are byte-oriented sources.

Readers are defined by one method, read. Each call to read will attempt to pull bytes from this source into a provided buffer.

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 Read trait.

Required Methods

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

Pull some bytes from this source into the specified buffer, returning how many bytes were read.

This function does not provide any guarantees about whether it blocks waiting for data, but if an object needs to block for a read but cannot it will typically signal this via an Err return value.

If the return value of this method is Ok(n), then it must be guaranteed that 0 <= n <= buf.len(). A nonzero n value indicates that the buffer buf has ben filled in with n bytes of data from this source. If n is 0, then it can indicate one of two scenarios:

  1. This reader has reached its "end of file" and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
  2. The buffer specified was 0 bytes in length.

No guarantees are provided about the contents of buf when this function is called, implementations cannot rely on any property of the contents of buf being true. It is recommended that implementations only write data to buf instead of reading its contents.

Errors

If this function encounters any form of I/O or other error, an error variant will be returned. If an error is returned then it must be guaranteed that no bytes were read.

Provided Methods

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<()>

Read all bytes until EOF in this source, placing them into buf.

All bytes read from this source will be appended to the specified buffer buf. This function will return a call to read either:

  1. Returns Ok(0).
  2. Returns an error which is not of the kind ErrorKind::Interrupted.

Until one of these conditions is met the function will continuously invoke read to append more data to buf.

Errors

If this function encounters an error of the kind ErrorKind::Interrupted then the error is ignored and the operation will continue.

If any other read error is encountered then this function immediately returns. Any bytes which have already been read will be appended to buf.

fn read_to_string(&mut self, buf: &mut String) -> Result<()>

Read all bytes until EOF in this source, placing them into buf.

Errors

If the data in this stream is not valid UTF-8 then an error is returned and buf is unchanged.

See read_to_end for other error semantics.

Implementors