]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | //! Windows-specific extensions to general I/O primitives. |
2 | ||
d9579d0f AL |
3 | #![stable(feature = "rust1", since = "1.0.0")] |
4 | ||
532ac7d7 | 5 | use crate::fs; |
60c5eb7d | 6 | use crate::io; |
532ac7d7 | 7 | use crate::net; |
60c5eb7d | 8 | use crate::os::windows::raw; |
532ac7d7 XL |
9 | use crate::sys; |
10 | use crate::sys::c; | |
60c5eb7d | 11 | use crate::sys_common::{self, AsInner, FromInner, IntoInner}; |
d9579d0f AL |
12 | |
13 | /// Raw HANDLEs. | |
14 | #[stable(feature = "rust1", since = "1.0.0")] | |
15 | pub type RawHandle = raw::HANDLE; | |
16 | ||
17 | /// Raw SOCKETs. | |
18 | #[stable(feature = "rust1", since = "1.0.0")] | |
19 | pub type RawSocket = raw::SOCKET; | |
20 | ||
9fa01778 | 21 | /// Extracts raw handles. |
d9579d0f AL |
22 | #[stable(feature = "rust1", since = "1.0.0")] |
23 | pub trait AsRawHandle { | |
24 | /// Extracts the raw handle, without taking any ownership. | |
25 | #[stable(feature = "rust1", since = "1.0.0")] | |
26 | fn as_raw_handle(&self) -> RawHandle; | |
27 | } | |
28 | ||
29 | /// Construct I/O objects from raw handles. | |
30 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
31 | pub trait FromRawHandle { | |
32 | /// Constructs a new I/O object from the specified raw handle. | |
33 | /// | |
34 | /// This function will **consume ownership** of the handle given, | |
35 | /// passing responsibility for closing the handle to the returned | |
36 | /// object. | |
37 | /// | |
38 | /// This function is also unsafe as the primitives currently returned | |
39 | /// have the contract that they are the sole owner of the file | |
40 | /// descriptor they are wrapping. Usage of this function could | |
41 | /// accidentally allow violating this contract which can cause memory | |
42 | /// unsafety in code that relies on it being true. | |
43 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
44 | unsafe fn from_raw_handle(handle: RawHandle) -> Self; | |
45 | } | |
46 | ||
c1a9b12d SL |
47 | /// A trait to express the ability to consume an object and acquire ownership of |
48 | /// its raw `HANDLE`. | |
e9174d1e | 49 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
50 | pub trait IntoRawHandle { |
51 | /// Consumes this object, returning the raw underlying handle. | |
52 | /// | |
53 | /// This function **transfers ownership** of the underlying handle to the | |
54 | /// caller. Callers are then the unique owners of the handle and must close | |
55 | /// it once it's no longer needed. | |
e9174d1e | 56 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
57 | fn into_raw_handle(self) -> RawHandle; |
58 | } | |
59 | ||
d9579d0f AL |
60 | #[stable(feature = "rust1", since = "1.0.0")] |
61 | impl AsRawHandle for fs::File { | |
62 | fn as_raw_handle(&self) -> RawHandle { | |
63 | self.as_inner().handle().raw() as RawHandle | |
64 | } | |
65 | } | |
66 | ||
3b2f2976 XL |
67 | #[stable(feature = "asraw_stdio", since = "1.21.0")] |
68 | impl AsRawHandle for io::Stdin { | |
69 | fn as_raw_handle(&self) -> RawHandle { | |
70 | unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle } | |
71 | } | |
72 | } | |
73 | ||
74 | #[stable(feature = "asraw_stdio", since = "1.21.0")] | |
75 | impl AsRawHandle for io::Stdout { | |
76 | fn as_raw_handle(&self) -> RawHandle { | |
77 | unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle } | |
78 | } | |
79 | } | |
80 | ||
81 | #[stable(feature = "asraw_stdio", since = "1.21.0")] | |
82 | impl AsRawHandle for io::Stderr { | |
83 | fn as_raw_handle(&self) -> RawHandle { | |
84 | unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle } | |
85 | } | |
86 | } | |
87 | ||
532ac7d7 XL |
88 | #[stable(feature = "asraw_stdio_locks", since = "1.35.0")] |
89 | impl<'a> AsRawHandle for io::StdinLock<'a> { | |
90 | fn as_raw_handle(&self) -> RawHandle { | |
91 | unsafe { c::GetStdHandle(c::STD_INPUT_HANDLE) as RawHandle } | |
92 | } | |
93 | } | |
94 | ||
95 | #[stable(feature = "asraw_stdio_locks", since = "1.35.0")] | |
96 | impl<'a> AsRawHandle for io::StdoutLock<'a> { | |
97 | fn as_raw_handle(&self) -> RawHandle { | |
98 | unsafe { c::GetStdHandle(c::STD_OUTPUT_HANDLE) as RawHandle } | |
99 | } | |
100 | } | |
101 | ||
102 | #[stable(feature = "asraw_stdio_locks", since = "1.35.0")] | |
103 | impl<'a> AsRawHandle for io::StderrLock<'a> { | |
104 | fn as_raw_handle(&self) -> RawHandle { | |
105 | unsafe { c::GetStdHandle(c::STD_ERROR_HANDLE) as RawHandle } | |
106 | } | |
107 | } | |
108 | ||
d9579d0f AL |
109 | #[stable(feature = "from_raw_os", since = "1.1.0")] |
110 | impl FromRawHandle for fs::File { | |
111 | unsafe fn from_raw_handle(handle: RawHandle) -> fs::File { | |
92a42be0 | 112 | let handle = handle as c::HANDLE; |
d9579d0f AL |
113 | fs::File::from_inner(sys::fs::File::from_inner(handle)) |
114 | } | |
115 | } | |
116 | ||
e9174d1e | 117 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
118 | impl IntoRawHandle for fs::File { |
119 | fn into_raw_handle(self) -> RawHandle { | |
120 | self.into_inner().into_handle().into_raw() as *mut _ | |
121 | } | |
122 | } | |
123 | ||
9fa01778 | 124 | /// Extracts raw sockets. |
d9579d0f AL |
125 | #[stable(feature = "rust1", since = "1.0.0")] |
126 | pub trait AsRawSocket { | |
127 | /// Extracts the underlying raw socket from this object. | |
128 | #[stable(feature = "rust1", since = "1.0.0")] | |
129 | fn as_raw_socket(&self) -> RawSocket; | |
130 | } | |
131 | ||
9fa01778 | 132 | /// Creates I/O objects from raw sockets. |
d9579d0f AL |
133 | #[stable(feature = "from_raw_os", since = "1.1.0")] |
134 | pub trait FromRawSocket { | |
135 | /// Creates a new I/O object from the given raw socket. | |
136 | /// | |
137 | /// This function will **consume ownership** of the socket provided and | |
138 | /// it will be closed when the returned object goes out of scope. | |
139 | /// | |
140 | /// This function is also unsafe as the primitives currently returned | |
141 | /// have the contract that they are the sole owner of the file | |
142 | /// descriptor they are wrapping. Usage of this function could | |
143 | /// accidentally allow violating this contract which can cause memory | |
144 | /// unsafety in code that relies on it being true. | |
145 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
146 | unsafe fn from_raw_socket(sock: RawSocket) -> Self; | |
147 | } | |
148 | ||
c1a9b12d SL |
149 | /// A trait to express the ability to consume an object and acquire ownership of |
150 | /// its raw `SOCKET`. | |
e9174d1e | 151 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
152 | pub trait IntoRawSocket { |
153 | /// Consumes this object, returning the raw underlying socket. | |
154 | /// | |
155 | /// This function **transfers ownership** of the underlying socket to the | |
156 | /// caller. Callers are then the unique owners of the socket and must close | |
157 | /// it once it's no longer needed. | |
e9174d1e | 158 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
159 | fn into_raw_socket(self) -> RawSocket; |
160 | } | |
161 | ||
d9579d0f AL |
162 | #[stable(feature = "rust1", since = "1.0.0")] |
163 | impl AsRawSocket for net::TcpStream { | |
164 | fn as_raw_socket(&self) -> RawSocket { | |
165 | *self.as_inner().socket().as_inner() | |
166 | } | |
167 | } | |
168 | #[stable(feature = "rust1", since = "1.0.0")] | |
169 | impl AsRawSocket for net::TcpListener { | |
170 | fn as_raw_socket(&self) -> RawSocket { | |
171 | *self.as_inner().socket().as_inner() | |
172 | } | |
173 | } | |
174 | #[stable(feature = "rust1", since = "1.0.0")] | |
175 | impl AsRawSocket for net::UdpSocket { | |
176 | fn as_raw_socket(&self) -> RawSocket { | |
177 | *self.as_inner().socket().as_inner() | |
178 | } | |
179 | } | |
180 | ||
181 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
182 | impl FromRawSocket for net::TcpStream { | |
183 | unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpStream { | |
184 | let sock = sys::net::Socket::from_inner(sock); | |
185 | net::TcpStream::from_inner(sys_common::net::TcpStream::from_inner(sock)) | |
186 | } | |
187 | } | |
188 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
189 | impl FromRawSocket for net::TcpListener { | |
190 | unsafe fn from_raw_socket(sock: RawSocket) -> net::TcpListener { | |
191 | let sock = sys::net::Socket::from_inner(sock); | |
192 | net::TcpListener::from_inner(sys_common::net::TcpListener::from_inner(sock)) | |
193 | } | |
194 | } | |
195 | #[stable(feature = "from_raw_os", since = "1.1.0")] | |
196 | impl FromRawSocket for net::UdpSocket { | |
197 | unsafe fn from_raw_socket(sock: RawSocket) -> net::UdpSocket { | |
198 | let sock = sys::net::Socket::from_inner(sock); | |
199 | net::UdpSocket::from_inner(sys_common::net::UdpSocket::from_inner(sock)) | |
200 | } | |
201 | } | |
c1a9b12d | 202 | |
e9174d1e | 203 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
204 | impl IntoRawSocket for net::TcpStream { |
205 | fn into_raw_socket(self) -> RawSocket { | |
206 | self.into_inner().into_socket().into_inner() | |
207 | } | |
208 | } | |
209 | ||
e9174d1e | 210 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
211 | impl IntoRawSocket for net::TcpListener { |
212 | fn into_raw_socket(self) -> RawSocket { | |
213 | self.into_inner().into_socket().into_inner() | |
214 | } | |
215 | } | |
216 | ||
e9174d1e | 217 | #[stable(feature = "into_raw_os", since = "1.4.0")] |
c1a9b12d SL |
218 | impl IntoRawSocket for net::UdpSocket { |
219 | fn into_raw_socket(self) -> RawSocket { | |
220 | self.into_inner().into_socket().into_inner() | |
221 | } | |
222 | } |