1#![stable(feature = "rust1", since = "1.0.0")]
4
5#[cfg(target_os = "hermit")]
6use hermit_abi as libc;
7
8#[cfg(not(target_os = "trusty"))]
9use crate::fs;
10use crate::io;
11#[cfg(target_os = "hermit")]
12use crate::os::hermit::io::OwnedFd;
13#[cfg(not(target_os = "hermit"))]
14use crate::os::raw;
15#[cfg(all(doc, not(target_arch = "wasm32")))]
16use crate::os::unix::io::AsFd;
17#[cfg(unix)]
18use crate::os::unix::io::OwnedFd;
19#[cfg(target_os = "wasi")]
20use crate::os::wasi::io::OwnedFd;
21#[cfg(not(target_os = "trusty"))]
22use crate::sys_common::{AsInner, FromInner, IntoInner};
23
24#[stable(feature = "rust1", since = "1.0.0")]
26#[cfg(not(target_os = "hermit"))]
27pub type RawFd = raw::c_int;
28#[stable(feature = "rust1", since = "1.0.0")]
29#[cfg(target_os = "hermit")]
30pub type RawFd = i32;
31
32#[stable(feature = "rust1", since = "1.0.0")]
38pub trait AsRawFd {
39 #[stable(feature = "rust1", since = "1.0.0")]
65 fn as_raw_fd(&self) -> RawFd;
66}
67
68#[stable(feature = "from_raw_os", since = "1.1.0")]
71pub trait FromRawFd {
72 #[stable(feature = "from_raw_os", since = "1.1.0")]
109 unsafe fn from_raw_fd(fd: RawFd) -> Self;
110}
111
112#[stable(feature = "into_raw_os", since = "1.4.0")]
115pub trait IntoRawFd {
116 #[must_use = "losing the raw file descriptor may leak resources"]
140 #[stable(feature = "into_raw_os", since = "1.4.0")]
141 fn into_raw_fd(self) -> RawFd;
142}
143
144#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
145impl AsRawFd for RawFd {
146 #[inline]
147 fn as_raw_fd(&self) -> RawFd {
148 *self
149 }
150}
151#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
152impl IntoRawFd for RawFd {
153 #[inline]
154 fn into_raw_fd(self) -> RawFd {
155 self
156 }
157}
158#[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
159impl FromRawFd for RawFd {
160 #[inline]
161 unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
162 fd
163 }
164}
165
166#[stable(feature = "rust1", since = "1.0.0")]
167#[cfg(not(target_os = "trusty"))]
168impl AsRawFd for fs::File {
169 #[inline]
170 fn as_raw_fd(&self) -> RawFd {
171 self.as_inner().as_raw_fd()
172 }
173}
174#[stable(feature = "from_raw_os", since = "1.1.0")]
175#[cfg(not(target_os = "trusty"))]
176impl FromRawFd for fs::File {
177 #[inline]
178 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
179 unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
180 }
181}
182#[stable(feature = "into_raw_os", since = "1.4.0")]
183#[cfg(not(target_os = "trusty"))]
184impl IntoRawFd for fs::File {
185 #[inline]
186 fn into_raw_fd(self) -> RawFd {
187 self.into_inner().into_inner().into_raw_fd()
188 }
189}
190
191#[stable(feature = "asraw_stdio", since = "1.21.0")]
192#[cfg(not(target_os = "trusty"))]
193impl AsRawFd for io::Stdin {
194 #[inline]
195 fn as_raw_fd(&self) -> RawFd {
196 libc::STDIN_FILENO
197 }
198}
199
200#[stable(feature = "asraw_stdio", since = "1.21.0")]
201impl AsRawFd for io::Stdout {
202 #[inline]
203 fn as_raw_fd(&self) -> RawFd {
204 libc::STDOUT_FILENO
205 }
206}
207
208#[stable(feature = "asraw_stdio", since = "1.21.0")]
209impl AsRawFd for io::Stderr {
210 #[inline]
211 fn as_raw_fd(&self) -> RawFd {
212 libc::STDERR_FILENO
213 }
214}
215
216#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
217#[cfg(not(target_os = "trusty"))]
218impl<'a> AsRawFd for io::StdinLock<'a> {
219 #[inline]
220 fn as_raw_fd(&self) -> RawFd {
221 libc::STDIN_FILENO
222 }
223}
224
225#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
226impl<'a> AsRawFd for io::StdoutLock<'a> {
227 #[inline]
228 fn as_raw_fd(&self) -> RawFd {
229 libc::STDOUT_FILENO
230 }
231}
232
233#[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
234impl<'a> AsRawFd for io::StderrLock<'a> {
235 #[inline]
236 fn as_raw_fd(&self) -> RawFd {
237 libc::STDERR_FILENO
238 }
239}
240
241#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
257impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
258 #[inline]
259 fn as_raw_fd(&self) -> RawFd {
260 (**self).as_raw_fd()
261 }
262}
263
264#[stable(feature = "asfd_rc", since = "1.69.0")]
265impl<T: AsRawFd> AsRawFd for crate::rc::Rc<T> {
266 #[inline]
267 fn as_raw_fd(&self) -> RawFd {
268 (**self).as_raw_fd()
269 }
270}
271
272#[unstable(feature = "unique_rc_arc", issue = "112566")]
273impl<T: AsRawFd + ?Sized> AsRawFd for crate::rc::UniqueRc<T> {
274 #[inline]
275 fn as_raw_fd(&self) -> RawFd {
276 (**self).as_raw_fd()
277 }
278}
279
280#[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
281impl<T: AsRawFd> AsRawFd for Box<T> {
282 #[inline]
283 fn as_raw_fd(&self) -> RawFd {
284 (**self).as_raw_fd()
285 }
286}
287
288#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
289#[cfg(not(target_os = "trusty"))]
290impl AsRawFd for io::PipeReader {
291 fn as_raw_fd(&self) -> RawFd {
292 self.0.as_raw_fd()
293 }
294}
295
296#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
297#[cfg(not(target_os = "trusty"))]
298impl FromRawFd for io::PipeReader {
299 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
300 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
301 }
302}
303
304#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
305#[cfg(not(target_os = "trusty"))]
306impl IntoRawFd for io::PipeReader {
307 fn into_raw_fd(self) -> RawFd {
308 self.0.into_raw_fd()
309 }
310}
311
312#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
313#[cfg(not(target_os = "trusty"))]
314impl AsRawFd for io::PipeWriter {
315 fn as_raw_fd(&self) -> RawFd {
316 self.0.as_raw_fd()
317 }
318}
319
320#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
321#[cfg(not(target_os = "trusty"))]
322impl FromRawFd for io::PipeWriter {
323 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
324 Self::from_inner(unsafe { FromRawFd::from_raw_fd(raw_fd) })
325 }
326}
327
328#[stable(feature = "anonymous_pipe", since = "CURRENT_RUSTC_VERSION")]
329#[cfg(not(target_os = "trusty"))]
330impl IntoRawFd for io::PipeWriter {
331 fn into_raw_fd(self) -> RawFd {
332 self.0.into_raw_fd()
333 }
334}