1 //! Unix-specific extensions to general I/O primitives
3 #![stable(feature = "rust1", since = "1.0.0")]
9 use crate::sys_common::{self, AsInner, FromInner, IntoInner};
11 /// Raw file descriptors.
12 #[stable(feature = "rust1", since = "1.0.0")]
13 pub type RawFd = usize;
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 instances 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 = "rust1", since = "1.0.0")]
85 impl AsRawFd for net::TcpStream {
86 fn as_raw_fd(&self) -> RawFd {
87 self.as_inner().as_inner().fd().raw()
90 #[stable(feature = "rust1", since = "1.0.0")]
91 impl AsRawFd for net::TcpListener {
92 fn as_raw_fd(&self) -> RawFd {
93 self.as_inner().as_inner().fd().raw()
96 #[stable(feature = "rust1", since = "1.0.0")]
97 impl AsRawFd for net::UdpSocket {
98 fn as_raw_fd(&self) -> RawFd {
99 self.as_inner().as_inner().fd().raw()
103 #[stable(feature = "asraw_stdio", since = "1.21.0")]
104 impl AsRawFd for io::Stdin {
105 fn as_raw_fd(&self) -> RawFd { 0 }
108 #[stable(feature = "asraw_stdio", since = "1.21.0")]
109 impl AsRawFd for io::Stdout {
110 fn as_raw_fd(&self) -> RawFd { 1 }
113 #[stable(feature = "asraw_stdio", since = "1.21.0")]
114 impl AsRawFd for io::Stderr {
115 fn as_raw_fd(&self) -> RawFd { 2 }
118 #[stable(feature = "from_raw_os", since = "1.1.0")]
119 impl FromRawFd for net::TcpStream {
120 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
121 let file = sys::fs::File::from_inner(fd);
122 net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(file))
125 #[stable(feature = "from_raw_os", since = "1.1.0")]
126 impl FromRawFd for net::TcpListener {
127 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
128 let file = sys::fs::File::from_inner(fd);
129 net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(file))
132 #[stable(feature = "from_raw_os", since = "1.1.0")]
133 impl FromRawFd for net::UdpSocket {
134 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
135 let file = sys::fs::File::from_inner(fd);
136 net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(file))
140 #[stable(feature = "into_raw_os", since = "1.4.0")]
141 impl IntoRawFd for net::TcpStream {
142 fn into_raw_fd(self) -> RawFd {
143 self.into_inner().into_inner().into_fd().into_raw()
146 #[stable(feature = "into_raw_os", since = "1.4.0")]
147 impl IntoRawFd for net::TcpListener {
148 fn into_raw_fd(self) -> RawFd {
149 self.into_inner().into_inner().into_fd().into_raw()
152 #[stable(feature = "into_raw_os", since = "1.4.0")]
153 impl IntoRawFd for net::UdpSocket {
154 fn into_raw_fd(self) -> RawFd {
155 self.into_inner().into_inner().into_fd().into_raw()