Sometimes read, sometimes poll_read, but without async

Hi

My project is not async in nature but I can save myself some processing work if I read as much from stdin as possible, act on all input then display all changes together.

what i'd like is something like

stdin.read_everything_ready(buf)

the current read seems to wait until either the buf is full, or EOI to return.

I would like to wait until there is something to read, then read everything available and return immediately.

Something like

stdin.read(&mut buf[..1]) //this one waits
stdin.try_read(&mut buf[1..]) // this one doesn't

I've already tried converting the entire project to async. while, technically, it solved this issue (really awkwardly) it created many bigger issues.

Is there a function that exists already to do a try/poll read available somewhere?

Thanks

You could temporarily transition the file descriptor to non-blocking mode and do an ordinary read, but this would involve unsafe platform-specific code, since the standard library doesn't provide a method for this on stdin.

Alternatively you could spawn a separate thread for reading from stdin and communicate with that thread.

1 Like

Thanks. This sets me on a path that could be quite interesting,

the following crate looks like it could simplify the unsafeness/multi-OS issues.

https://docs.rs/filedescriptor/0.8.0/filedescriptor