use std::os::fd::AsFd;
use nix::poll;
const NUMFILES: usize = 16;
fn make_nonblock(fd: impl AsFd) {
use nix::fcntl::FcntlArg;
use nix::fcntl::OFlag;
use nix::fcntl::fcntl;
let arg = FcntlArg::F_SETFL(OFlag::O_NONBLOCK);
fcntl(fd, arg).expect("failed to set fd nonblocking");
}
fn main() -> std::io::Result<()> {
let mut files = Vec::with_capacity(NUMFILES);
for c in "0123456789ABCDEF".chars() {
let name = format!("{c}");
let f = std::fs::File::open(name)?;
make_nonblock(&f);
files.push(f);
}
let mut readbuf = vec![0u8; 4096];
let mut pollfds = files
.iter()
.map(|f| poll::PollFd::new(f.as_fd(), poll::PollFlags::POLLIN))
.collect::<Vec<_>>();
for _ in 0..16 {
poll::poll(pollfds.as_mut_slice(), poll::PollTimeout::NONE)?;
for (i, pfd) in pollfds.iter().enumerate() {
let revents = pfd.revents().expect("got unknown poll flag");
if !revents.intersects(poll::PollFlags::POLLIN) {
print!("_: ");
continue;
}
print!("{i:X}:");
let nbytes = nix::unistd::read(pfd.as_fd(), readbuf.as_mut_slice())?;
print!("{nbytes:02} ");
}
println!();
}
Ok(())
}