Struct nonblock::NonBlockingReader [] [src]

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

Simple non-blocking wrapper for reader types that implement AsRawFd


impl<R: AsRawFd + Read> 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.

Consume this NonBlockingReader and return the blocking version of the interanally 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.

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.

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.


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.

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.

This function calls read_available() and attempts to convert the data read to a UTF-8 string. If the any data read is parsed successfully as a UTF-8 string it is appended to buf. If it cannot be parsed as UTF-8, then buf will remain unmodified, returning ErrorKind::InvalidData with the FromUtf8Error containing any data that was read.

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.