lev.fd_reader

lev

fd_reader(fd) -> reader

Wrap an arbitrary fd for cooperative reads on the event loop

Returns a reader for fd (e.g. stdin, fd 0) that integrates with the event loop. :read(maxlen, opts) yields the current coroutine until at least one byte is available, returning the bytes, or nil,"closed" on EOF, or nil,"cancelled"/err.

A zero timeout reads without a wait: :read(maxlen, 0) returns the bytes the fd holds, or nil,"timeout" when it holds none, and does not yield.

The fd must be in non-blocking mode (see set_nonblocking) and must be epoll-able (a TTY or pipe, not a regular file). Only one coroutine may wait on a given fd at a time.

read reports EOF only from a non-blocking fd. On a fd flipped to blocking, a raw-mode tty (VMIN=0, VTIME>0) returns zero bytes on idle -- a phantom EOF -- so read re-asserts non-blocking and keeps reading. A caller that flips the flag mid-read gets it flipped back.