6 releases (breaking)

0.5.0 Jul 3, 2021
0.4.0 Jun 30, 2021
0.3.1 Jun 12, 2021
0.3.0 Oct 5, 2020
0.1.0 Jun 27, 2020

#116 in Unix APIs

Download history 103/week @ 2021-07-04 226/week @ 2021-07-11 191/week @ 2021-07-18 333/week @ 2021-07-25 697/week @ 2021-08-01 510/week @ 2021-08-08 1136/week @ 2021-08-15 865/week @ 2021-08-22 710/week @ 2021-08-29 899/week @ 2021-09-05 1678/week @ 2021-09-12 1824/week @ 2021-09-19 1377/week @ 2021-09-26 834/week @ 2021-10-03 1185/week @ 2021-10-10 1495/week @ 2021-10-17

216 downloads per month
Used in 3 crates

BSD-2-Clause

14KB
137 lines

filedesc docs tests

This crate exposes a single type: FileDesc, which acts as a thin wrapper around open file descriptors. The wrapped file descriptor is closed when the wrapper is dropped.

You can call FileDesc::new() with any type that implements IntoRawFd, or duplicate the file descriptor of a type that implements AsRawFd with duplicate_from, or directly from a raw file descriptor with from_raw_fd() and duplicate_raw_fd(). Wrapped file descriptors can also be duplicated with the duplicate() function.

Close-on-exec

Whenever the library duplicates a file descriptor, it tries to set the close-on-exec flag atomically. On platforms where this is not supported, the library falls back to setting the flag non-atomically. When an existing file descriptor is wrapped, the close-on-exec flag is left as it was.

You can also check or set the close-on-exec flag with the get_close_on_exec() and set_close_on_exec functions.

Example

use filedesc::FileDesc;
let fd = unsafe { FileDesc::from_raw_fd(raw_fd) };
let duplicated = unsafe { fd.duplicate()? };
assert_eq!(duplicated.get_close_on_exec()?, true);

duplicated.set_close_on_exec(false)?;
assert_eq!(duplicated.get_close_on_exec()?, false);

Dependencies

~31KB

`