Struct nonblock::NonBlockingReader [] [src]

pub struct NonBlockingReader<R: AsRawFd + Read> {
    // some fields omitted
}

Simple non-blocking wrapper for reader types that implement AsRawFd

Methods

impl<R: AsRawFd + Read> NonBlockingReader<R>

fn from_fd(reader: R) -> Result<NonBlockingReader<R>>

Initialize a NonBlockingReader from the reader's file descriptor.

The reader will be managed internally, and O_NONBLOCK will be set the file descriptor.

fn into_blocking(self) -> Result<R>

Consume this NonBlockingReader and return the blocking version of the internally managed reader.

This will disable O_NONBLOCK on the file descriptor, and any data read from the NonBlockingReader before calling into_blocking will already have been consumed from the reader.

fn is_eof(&self) -> bool

Indicates if EOF has been reached for the reader.

Currently this defaults to false until one of the read_available methods is called, but this may change in the future if I stumble on a compelling way to check for EOF without consuming any of the internal reader.

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

Reads any available data from the reader without blocking, placing them into buf.

If successful, this function will return the total number of bytes read. 0 bytes read may indicate the EOF has been reached or that reading would block because there is not any data immediately available. Call is_eof() after this method to determine if EOF was reached.

Errors

If this function encounters an error of the kind ErrorKind::Interrupted then the error is ignored and the operation will continue. If it encounters ErrorKind::WouldBlock, then this function immediately returns the total number of bytes read so far.

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

Examples

let client = TcpStream::connect("127.0.0.1:34567").unwrap();
let mut noblock_stdout = NonBlockingReader::from_fd(client).unwrap();
let mut buf = Vec::new();
noblock_stdout.read_available(&mut buf).unwrap();

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

Reads any available data from the reader without blocking, placing them into buf.

If successful, this function returns the number of bytes which were read and appended to buf.

Errors

This function inherits all the possible errors of read_available(). In the case of errors that occur after successfully reading some data, the successfully read data will still be parsed and appended to buf.

Additionally, if the read data cannot be parsed as UTF-8, then buf will remain unmodified, and this method will return ErrorKind::InvalidData with the FromUtf8Error containing any data that was read.

Examples

let mut child = Command::new("foo").stdout(Stdio::piped()).spawn().unwrap();
let stdout = child.stdout.take().unwrap();
let mut noblock_stdout = NonBlockingReader::from_fd(stdout).unwrap();
let mut buf = String::new();
noblock_stdout.read_available_to_string(&mut buf).unwrap();

In theory, since this function only reads immediately available data, There may not be any guarantee that the data immediately available ends on a UTF-8 alignment, so it might be worth a bufferred wrapper that manages the captures a final non-UTF-8 character and prepends it to the next call, but in practice, this has worked as expected.