[][src]Function close_fds::iter_possible_fds

pub fn iter_possible_fds(minfd: c_int) -> FdIter

Notable traits for FdIter

impl Iterator for FdIter type Item = c_int;

Identical to iter_open_fds(), but may -- for efficiency -- yield invalid file descriptors.

With this function, the caller is responsible for checking if the file descriptors are valid.

Proper usage

You should only use this function instead of iter_open_fds() if you immediately perform an operation that implicitly checks if the file descriptor is valid. For example:

use std::os::unix::io::FromRawFd;

for fd in close_fds::iter_possible_fds(0) {
    let file = unsafe { std::fs::File::from_raw_fd(fd) };
    let _meta = match file.metadata() {
        Ok(m) => m,
        Err(e) if e.raw_os_error() == Some(libc::EBADF) => {
            std::mem::forget(file);  // Don't try to close() it
            continue;
        }
        Err(e) => panic!(e),
    };

    // ...
}

Note that this example is NOT intended to imply that you should be calling metadata() (or any other methods) on random file descriptors. Most of the warnings about iter_open_fds() apply to this function as well.