Nonblocking read in Linux


#1

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?


#2

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: https://doc.rust-lang.org/std/os/unix/io/trait.FromRawFd.html#tymethod.from_raw_fd


#3

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


#4

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:.


#5

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