async_std/os/unix/
io.rs

1//! Unix-specific I/O extensions.
2
3cfg_not_docs! {
4    pub use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
5    
6    cfg_io_safety! {
7        pub use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
8    }
9}
10
11cfg_docs! {
12    /// Raw file descriptors.
13    pub type RawFd = std::os::raw::c_int;
14
15    /// A trait to extract the raw unix file descriptor from an underlying
16    /// object.
17    ///
18    /// This is only available on unix platforms and must be imported in order
19    /// to call the method. Windows platforms have a corresponding `AsRawHandle`
20    /// and `AsRawSocket` set of traits.
21    pub trait AsRawFd {
22        /// Extracts the raw file descriptor.
23        ///
24        /// This method does **not** pass ownership of the raw file descriptor
25        /// to the caller. The descriptor is only guaranteed to be valid while
26        /// the original object has not yet been destroyed.
27        fn as_raw_fd(&self) -> RawFd;
28    }
29
30    /// A trait to express the ability to construct an object from a raw file
31    /// descriptor.
32    pub trait FromRawFd {
33        /// Constructs a new instance of `Self` from the given raw file
34        /// descriptor.
35        ///
36        /// This function **consumes ownership** of the specified file
37        /// descriptor. The returned object will take responsibility for closing
38        /// it when the object goes out of scope.
39        ///
40        /// This function is also unsafe as the primitives currently returned
41        /// have the contract that they are the sole owner of the file
42        /// descriptor they are wrapping. Usage of this function could
43        /// accidentally allow violating this contract which can cause memory
44        /// unsafety in code that relies on it being true.
45        unsafe fn from_raw_fd(fd: RawFd) -> Self;
46    }
47
48    /// A trait to express the ability to consume an object and acquire ownership of
49    /// its raw file descriptor.
50    pub trait IntoRawFd {
51        /// Consumes this object, returning the raw underlying file descriptor.
52        ///
53        /// This function **transfers ownership** of the underlying file descriptor
54        /// to the caller. Callers are then the unique owners of the file descriptor
55        /// and must close the descriptor once it's no longer needed.
56        fn into_raw_fd(self) -> RawFd;
57    }
58
59    cfg_io_safety! {
60        #[doc(inline)]
61        pub use std::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
62    }
63}