1 //! Unix-specific extensions to general I/O primitives
3 #![stable(feature = "rust1", since = "1.0.0")]
9 use crate::sys_common::{AsInner, FromInner, IntoInner};
11 /// Raw file descriptors.
12 #[stable(feature = "rust1", since = "1.0.0")]
13 pub type RawFd = raw::c_int;
15 /// A trait to extract the raw unix file descriptor from an underlying
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 #[stable(feature = "rust1", since = "1.0.0")]
23 /// Extracts the raw file descriptor.
25 /// This method does **not** pass ownership of the raw file descriptor
26 /// to the caller. The descriptor is only guaranteed to be valid while
27 /// the original object has not yet been destroyed.
28 #[stable(feature = "rust1", since = "1.0.0")]
29 fn as_raw_fd(&self) -> RawFd;
32 /// A trait to express the ability to construct an object from a raw file
34 #[stable(feature = "from_raw_os", since = "1.1.0")]
36 /// Constructs a new instance of `Self` from the given raw file
39 /// This function **consumes ownership** of the specified file
40 /// descriptor. The returned object will take responsibility for closing
41 /// it when the object goes out of scope.
43 /// This function is also unsafe as the primitives currently returned
44 /// have the contract that they are the sole owner of the file
45 /// descriptor they are wrapping. Usage of this function could
46 /// accidentally allow violating this contract which can cause memory
47 /// unsafety in code that relies on it being true.
48 #[stable(feature = "from_raw_os", since = "1.1.0")]
49 unsafe fn from_raw_fd(fd: RawFd) -> Self;
52 /// A trait to express the ability to consume an object and acquire ownership of
53 /// its raw file descriptor.
54 #[stable(feature = "into_raw_os", since = "1.4.0")]
56 /// Consumes this object, returning the raw underlying file descriptor.
58 /// This function **transfers ownership** of the underlying file descriptor
59 /// to the caller. Callers are then the unique owners of the file descriptor
60 /// and must close the descriptor once it's no longer needed.
61 #[stable(feature = "into_raw_os", since = "1.4.0")]
62 fn into_raw_fd(self) -> RawFd;
65 #[stable(feature = "rust1", since = "1.0.0")]
66 impl AsRawFd for fs::File {
67 fn as_raw_fd(&self) -> RawFd {
68 self.as_inner().fd().raw()
71 #[stable(feature = "from_raw_os", since = "1.1.0")]
72 impl FromRawFd for fs::File {
73 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
74 fs::File::from_inner(sys::fs::File::from_inner(fd))
77 #[stable(feature = "into_raw_os", since = "1.4.0")]
78 impl IntoRawFd for fs::File {
79 fn into_raw_fd(self) -> RawFd {
80 self.into_inner().into_fd().into_raw()
84 #[stable(feature = "asraw_stdio", since = "1.21.0")]
85 impl AsRawFd for io::Stdin {
86 fn as_raw_fd(&self) -> RawFd { libc::STDIN_FILENO }
89 #[stable(feature = "asraw_stdio", since = "1.21.0")]
90 impl AsRawFd for io::Stdout {
91 fn as_raw_fd(&self) -> RawFd { libc::STDOUT_FILENO }
94 #[stable(feature = "asraw_stdio", since = "1.21.0")]
95 impl AsRawFd for io::Stderr {
96 fn as_raw_fd(&self) -> RawFd { libc::STDERR_FILENO }