|
32 | 32 | #![stable(feature = "rust1", since = "1.0.0")]
|
33 | 33 |
|
34 | 34 | /// Unix-specific extensions to general I/O primitives
|
35 |
| -#[unstable(feature = "io_ext", |
36 |
| - reason = "may want a slightly different organization or a more \ |
37 |
| - general file descriptor primitive")] |
| 35 | +#[stable(feature = "rust1", since = "1.0.0")] |
38 | 36 | pub mod io {
|
39 | 37 | #[allow(deprecated)] use old_io;
|
40 | 38 | use fs;
|
41 | 39 | use libc;
|
42 | 40 | use net;
|
43 |
| - use sys_common::AsInner; |
| 41 | + use sys_common::{net2, AsInner, FromInner}; |
| 42 | + use sys; |
44 | 43 |
|
45 | 44 | /// Raw file descriptors.
|
46 |
| - pub type Fd = libc::c_int; |
47 |
| - |
48 |
| - /// Extract raw file descriptor |
| 45 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 46 | + pub type RawFd = libc::c_int; |
| 47 | + |
| 48 | + /// A trait to extract the raw unix file descriptor from an underlying |
| 49 | + /// object. |
| 50 | + /// |
| 51 | + /// This is only available on unix platforms and must be imported in order |
| 52 | + /// to call the method. Windows platforms have a corresponding `AsRawHandle` |
| 53 | + /// and `AsRawSocket` set of traits. |
| 54 | + #[stable(feature = "rust1", since = "1.0.0")] |
49 | 55 | pub trait AsRawFd {
|
50 |
| - /// Extract the raw file descriptor, without taking any ownership. |
51 |
| - fn as_raw_fd(&self) -> Fd; |
| 56 | + /// Extract the raw file descriptor. |
| 57 | + /// |
| 58 | + /// This method does **not** pass ownership of the raw file descriptor |
| 59 | + /// to the caller. The descriptor is only guarantee to be valid while |
| 60 | + /// the original object has not yet been destroyed. |
| 61 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 62 | + fn as_raw_fd(&self) -> RawFd; |
| 63 | + } |
| 64 | + |
| 65 | + /// A trait to express the ability to construct an object from a raw file |
| 66 | + /// descriptor. |
| 67 | + #[unstable(feature = "from_raw_os", |
| 68 | + reason = "recent addition to std::os::unix::io")] |
| 69 | + pub trait FromRawFd { |
| 70 | + /// Constructs a new instances of `Self` from the given raw file |
| 71 | + /// descriptor. |
| 72 | + /// |
| 73 | + /// This function **consumes ownership** of the specified file |
| 74 | + /// descriptor. The returned object will take responsibility for closing |
| 75 | + /// it when the object goes out of scope. |
| 76 | + /// |
| 77 | + /// Callers should normally only pass in a valid file descriptor to this |
| 78 | + /// method or otherwise methods will return errors. |
| 79 | + fn from_raw_fd(fd: RawFd) -> Self; |
52 | 80 | }
|
53 | 81 |
|
54 | 82 | #[allow(deprecated)]
|
| 83 | + #[stable(feature = "rust1", since = "1.0.0")] |
55 | 84 | impl AsRawFd for old_io::fs::File {
|
56 |
| - fn as_raw_fd(&self) -> Fd { |
| 85 | + fn as_raw_fd(&self) -> RawFd { |
57 | 86 | self.as_inner().fd()
|
58 | 87 | }
|
59 | 88 | }
|
60 | 89 |
|
| 90 | + #[stable(feature = "rust1", since = "1.0.0")] |
61 | 91 | impl AsRawFd for fs::File {
|
62 |
| - fn as_raw_fd(&self) -> Fd { |
| 92 | + fn as_raw_fd(&self) -> RawFd { |
63 | 93 | self.as_inner().fd().raw()
|
64 | 94 | }
|
65 | 95 | }
|
| 96 | + #[unstable(feature = "from_raw_os", reason = "trait is unstable")] |
| 97 | + impl FromRawFd for fs::File { |
| 98 | + fn from_raw_fd(fd: RawFd) -> fs::File { |
| 99 | + fs::File::from_inner(sys::fs2::File::from_inner(fd)) |
| 100 | + } |
| 101 | + } |
66 | 102 |
|
67 | 103 | #[allow(deprecated)]
|
| 104 | + #[stable(feature = "rust1", since = "1.0.0")] |
68 | 105 | impl AsRawFd for old_io::pipe::PipeStream {
|
69 |
| - fn as_raw_fd(&self) -> Fd { |
| 106 | + fn as_raw_fd(&self) -> RawFd { |
70 | 107 | self.as_inner().fd()
|
71 | 108 | }
|
72 | 109 | }
|
73 | 110 |
|
74 | 111 | #[allow(deprecated)]
|
| 112 | + #[stable(feature = "rust1", since = "1.0.0")] |
75 | 113 | impl AsRawFd for old_io::net::pipe::UnixStream {
|
76 |
| - fn as_raw_fd(&self) -> Fd { |
| 114 | + fn as_raw_fd(&self) -> RawFd { |
77 | 115 | self.as_inner().fd()
|
78 | 116 | }
|
79 | 117 | }
|
80 | 118 |
|
81 | 119 | #[allow(deprecated)]
|
| 120 | + #[stable(feature = "rust1", since = "1.0.0")] |
82 | 121 | impl AsRawFd for old_io::net::pipe::UnixListener {
|
83 |
| - fn as_raw_fd(&self) -> Fd { |
| 122 | + fn as_raw_fd(&self) -> RawFd { |
84 | 123 | self.as_inner().fd()
|
85 | 124 | }
|
86 | 125 | }
|
87 | 126 |
|
88 | 127 | #[allow(deprecated)]
|
| 128 | + #[stable(feature = "rust1", since = "1.0.0")] |
89 | 129 | impl AsRawFd for old_io::net::pipe::UnixAcceptor {
|
90 |
| - fn as_raw_fd(&self) -> Fd { |
| 130 | + fn as_raw_fd(&self) -> RawFd { |
91 | 131 | self.as_inner().fd()
|
92 | 132 | }
|
93 | 133 | }
|
94 | 134 |
|
| 135 | + #[stable(feature = "rust1", since = "1.0.0")] |
95 | 136 | #[allow(deprecated)]
|
96 | 137 | impl AsRawFd for old_io::net::tcp::TcpStream {
|
97 |
| - fn as_raw_fd(&self) -> Fd { |
| 138 | + fn as_raw_fd(&self) -> RawFd { |
98 | 139 | self.as_inner().fd()
|
99 | 140 | }
|
100 | 141 | }
|
101 | 142 |
|
| 143 | + #[stable(feature = "rust1", since = "1.0.0")] |
102 | 144 | #[allow(deprecated)]
|
103 | 145 | impl AsRawFd for old_io::net::tcp::TcpListener {
|
104 |
| - fn as_raw_fd(&self) -> Fd { |
| 146 | + fn as_raw_fd(&self) -> RawFd { |
105 | 147 | self.as_inner().fd()
|
106 | 148 | }
|
107 | 149 | }
|
108 | 150 |
|
| 151 | + #[stable(feature = "rust1", since = "1.0.0")] |
109 | 152 | #[allow(deprecated)]
|
110 | 153 | impl AsRawFd for old_io::net::tcp::TcpAcceptor {
|
111 |
| - fn as_raw_fd(&self) -> Fd { |
| 154 | + fn as_raw_fd(&self) -> RawFd { |
112 | 155 | self.as_inner().fd()
|
113 | 156 | }
|
114 | 157 | }
|
115 | 158 |
|
116 | 159 | #[allow(deprecated)]
|
| 160 | + #[stable(feature = "rust1", since = "1.0.0")] |
117 | 161 | impl AsRawFd for old_io::net::udp::UdpSocket {
|
118 |
| - fn as_raw_fd(&self) -> Fd { |
| 162 | + fn as_raw_fd(&self) -> RawFd { |
119 | 163 | self.as_inner().fd()
|
120 | 164 | }
|
121 | 165 | }
|
122 | 166 |
|
| 167 | + #[stable(feature = "rust1", since = "1.0.0")] |
123 | 168 | impl AsRawFd for net::TcpStream {
|
124 |
| - fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() } |
| 169 | + fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() } |
125 | 170 | }
|
| 171 | + #[stable(feature = "rust1", since = "1.0.0")] |
126 | 172 | impl AsRawFd for net::TcpListener {
|
127 |
| - fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() } |
| 173 | + fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() } |
128 | 174 | }
|
| 175 | + #[stable(feature = "rust1", since = "1.0.0")] |
129 | 176 | impl AsRawFd for net::UdpSocket {
|
130 |
| - fn as_raw_fd(&self) -> Fd { *self.as_inner().socket().as_inner() } |
| 177 | + fn as_raw_fd(&self) -> RawFd { *self.as_inner().socket().as_inner() } |
| 178 | + } |
| 179 | + |
| 180 | + #[unstable(feature = "from_raw_os", reason = "trait is unstable")] |
| 181 | + impl FromRawFd for net::TcpStream { |
| 182 | + fn from_raw_fd(fd: RawFd) -> net::TcpStream { |
| 183 | + let socket = sys::net::Socket::from_inner(fd); |
| 184 | + net::TcpStream::from_inner(net2::TcpStream::from_inner(socket)) |
| 185 | + } |
| 186 | + } |
| 187 | + #[unstable(feature = "from_raw_os", reason = "trait is unstable")] |
| 188 | + impl FromRawFd for net::TcpListener { |
| 189 | + fn from_raw_fd(fd: RawFd) -> net::TcpListener { |
| 190 | + let socket = sys::net::Socket::from_inner(fd); |
| 191 | + net::TcpListener::from_inner(net2::TcpListener::from_inner(socket)) |
| 192 | + } |
| 193 | + } |
| 194 | + #[unstable(feature = "from_raw_os", reason = "trait is unstable")] |
| 195 | + impl FromRawFd for net::UdpSocket { |
| 196 | + fn from_raw_fd(fd: RawFd) -> net::UdpSocket { |
| 197 | + let socket = sys::net::Socket::from_inner(fd); |
| 198 | + net::UdpSocket::from_inner(net2::UdpSocket::from_inner(socket)) |
| 199 | + } |
131 | 200 | }
|
132 | 201 | }
|
133 | 202 |
|
@@ -302,7 +371,7 @@ pub mod process {
|
302 | 371 | #[stable(feature = "rust1", since = "1.0.0")]
|
303 | 372 | pub mod prelude {
|
304 | 373 | #[doc(no_inline)]
|
305 |
| - pub use super::io::{Fd, AsRawFd}; |
| 374 | + pub use super::io::{RawFd, AsRawFd}; |
306 | 375 | #[doc(no_inline)] #[stable(feature = "rust1", since = "1.0.0")]
|
307 | 376 | pub use super::ffi::{OsStrExt, OsStringExt};
|
308 | 377 | #[doc(no_inline)]
|
|
0 commit comments