1 //! Raw Unix-like file descriptors.
3 #![stable(feature = "rust1", since = "1.0.0")]
8 #[cfg(all(doc, not(target_arch = "wasm32")))]
9 use crate::os::unix::io::AsFd;
11 use crate::os::unix::io::OwnedFd;
12 #[cfg(target_os = "wasi")]
13 use crate::os::wasi::io::OwnedFd;
14 use crate::sys_common::{AsInner, IntoInner};
16 /// Raw file descriptors.
17 #[stable(feature = "rust1", since = "1.0.0")]
18 pub type RawFd = raw::c_int;
20 /// A trait to extract the raw file descriptor from an underlying object.
22 /// This is only available on unix and WASI platforms and must be imported in
23 /// order to call the method. Windows platforms have a corresponding
24 /// `AsRawHandle` and `AsRawSocket` set of traits.
25 #[stable(feature = "rust1", since = "1.0.0")]
27 /// Extracts the raw file descriptor.
29 /// This function is typically used to **borrow** an owned file descriptor.
30 /// When used in this way, this method does **not** pass ownership of the
31 /// raw file descriptor to the caller, and the file descriptor is only
32 /// guaranteed to be valid while the original object has not yet been
35 /// However, borrowing is not strictly required. See [`AsFd::as_fd`]
36 /// for an API which strictly borrows a file descriptor.
41 /// use std::fs::File;
44 /// use std::os::unix::io::{AsRawFd, RawFd};
45 /// #[cfg(target_os = "wasi")]
46 /// use std::os::wasi::io::{AsRawFd, RawFd};
48 /// let mut f = File::open("foo.txt")?;
49 /// // Note that `raw_fd` is only valid as long as `f` exists.
50 /// #[cfg(any(unix, target_os = "wasi"))]
51 /// let raw_fd: RawFd = f.as_raw_fd();
52 /// # Ok::<(), io::Error>(())
54 #[stable(feature = "rust1", since = "1.0.0")]
55 fn as_raw_fd(&self) -> RawFd;
58 /// A trait to express the ability to construct an object from a raw file
60 #[stable(feature = "from_raw_os", since = "1.1.0")]
62 /// Constructs a new instance of `Self` from the given raw file
65 /// This function is typically used to **consume ownership** of the
66 /// specified file descriptor. When used in this way, the returned object
67 /// will take responsibility for closing it when the object goes out of
70 /// However, consuming ownership is not strictly required. Use a
71 /// [`From<OwnedFd>::from`] implementation for an API which strictly
72 /// consumes ownership.
76 /// The `fd` passed in must be a valid an open file descriptor.
81 /// use std::fs::File;
84 /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
85 /// #[cfg(target_os = "wasi")]
86 /// use std::os::wasi::io::{FromRawFd, IntoRawFd, RawFd};
88 /// let f = File::open("foo.txt")?;
89 /// # #[cfg(any(unix, target_os = "wasi"))]
90 /// let raw_fd: RawFd = f.into_raw_fd();
91 /// // SAFETY: no other functions should call `from_raw_fd`, so there
92 /// // is only one owner for the file descriptor.
93 /// # #[cfg(any(unix, target_os = "wasi"))]
94 /// let f = unsafe { File::from_raw_fd(raw_fd) };
95 /// # Ok::<(), io::Error>(())
97 #[stable(feature = "from_raw_os", since = "1.1.0")]
98 unsafe fn from_raw_fd(fd: RawFd) -> Self;
101 /// A trait to express the ability to consume an object and acquire ownership of
102 /// its raw file descriptor.
103 #[stable(feature = "into_raw_os", since = "1.4.0")]
104 pub trait IntoRawFd {
105 /// Consumes this object, returning the raw underlying file descriptor.
107 /// This function is typically used to **transfer ownership** of the underlying
108 /// file descriptor to the caller. When used in this way, callers are then the unique
109 /// owners of the file descriptor and must close it once it's no longer needed.
111 /// However, transferring ownership is not strictly required. Use a
112 /// [`Into<OwnedFd>::into`] implementation for an API which strictly
113 /// transfers ownership.
118 /// use std::fs::File;
121 /// use std::os::unix::io::{IntoRawFd, RawFd};
122 /// #[cfg(target_os = "wasi")]
123 /// use std::os::wasi::io::{IntoRawFd, RawFd};
125 /// let f = File::open("foo.txt")?;
126 /// #[cfg(any(unix, target_os = "wasi"))]
127 /// let raw_fd: RawFd = f.into_raw_fd();
128 /// # Ok::<(), io::Error>(())
130 #[stable(feature = "into_raw_os", since = "1.4.0")]
131 fn into_raw_fd(self) -> RawFd;
134 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
135 impl AsRawFd for RawFd {
137 fn as_raw_fd(&self) -> RawFd {
141 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
142 impl IntoRawFd for RawFd {
144 fn into_raw_fd(self) -> RawFd {
148 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
149 impl FromRawFd for RawFd {
151 unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl AsRawFd for fs::File {
159 fn as_raw_fd(&self) -> RawFd {
160 self.as_inner().as_raw_fd()
163 #[stable(feature = "from_raw_os", since = "1.1.0")]
164 impl FromRawFd for fs::File {
166 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
167 unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
170 #[stable(feature = "into_raw_os", since = "1.4.0")]
171 impl IntoRawFd for fs::File {
173 fn into_raw_fd(self) -> RawFd {
174 self.into_inner().into_inner().into_raw_fd()
178 #[stable(feature = "asraw_stdio", since = "1.21.0")]
179 impl AsRawFd for io::Stdin {
181 fn as_raw_fd(&self) -> RawFd {
186 #[stable(feature = "asraw_stdio", since = "1.21.0")]
187 impl AsRawFd for io::Stdout {
189 fn as_raw_fd(&self) -> RawFd {
194 #[stable(feature = "asraw_stdio", since = "1.21.0")]
195 impl AsRawFd for io::Stderr {
197 fn as_raw_fd(&self) -> RawFd {
202 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
203 impl<'a> AsRawFd for io::StdinLock<'a> {
205 fn as_raw_fd(&self) -> RawFd {
210 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
211 impl<'a> AsRawFd for io::StdoutLock<'a> {
213 fn as_raw_fd(&self) -> RawFd {
218 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
219 impl<'a> AsRawFd for io::StderrLock<'a> {
221 fn as_raw_fd(&self) -> RawFd {