I have a std::net::TcpStream
. I want to determine if there is data available to be read without actually reading it yet.
The only relevant API I can find on TcpStream itself is read
which
does not provide any guarantees about whether it blocks waiting for data
which does not sound encouraging for this problem.
A related question seems to drop down to file descriptors and read(2)
to force an nonblocking read. However I cannot figure out how to use read(2)
to peek at an fd without actually reading it.
I suppose this is a job for select(2)
, but constructing the fd_set
s for the C arguments seems rather hairy. There certainly is not a Rust type for that, and it's not immediately clear how I would invent one.