10 releases (5 breaking)

0.6.3 Sep 20, 2023
0.6.1 Oct 11, 2022
0.5.0 Jul 3, 2021
0.3.0 Oct 5, 2020
0.2.0 Jun 27, 2020

#285 in Unix APIs

Download history 7646/week @ 2024-03-14 6977/week @ 2024-03-21 7676/week @ 2024-03-28 7944/week @ 2024-04-04 6848/week @ 2024-04-11 7351/week @ 2024-04-18 8555/week @ 2024-04-25 8223/week @ 2024-05-02 6781/week @ 2024-05-09 8279/week @ 2024-05-16 5098/week @ 2024-05-23 8081/week @ 2024-05-30 6315/week @ 2024-06-06 6931/week @ 2024-06-13 7640/week @ 2024-06-20 6283/week @ 2024-06-27

28,504 downloads per month
Used in 5 crates (4 directly)

BSD-2-Clause

15KB
141 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

~43KB