Nonblocking read in Linux

Hello,

I am reading from a character special file in Linux and I'd like to make the reads non-blocking. Is there an existing Rust way to do this?

I don’t think there’s anything builtin, so to speak. But you can call into the appropriate libc functions, and then create a File from the raw fd: FromRawFd in std::os::unix::io - Rust

A more portable way ist to use TcpStreamExt::set_nonblocking from the net2 crate.

This is for sockets only and will error on Windows if it’s not a socket. So I wouldn’t say it’s portable :slight_smile:.

Yeah, you're right of course.
I saw "nonblocking" and immediately thought "networking" without reading the question properly.
Sorry for the noise.

1 Like