]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/fd/raw.rs
Rollup merge of #97915 - tbu-:pr_os_string_fmt_write, r=joshtriplett
[rust.git] / library / std / src / os / fd / raw.rs
1 //! Raw Unix-like file descriptors.
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 use crate::fs;
6 use crate::io;
7 use crate::os::raw;
8 #[cfg(all(doc, not(target_arch = "wasm32")))]
9 use crate::os::unix::io::AsFd;
10 #[cfg(unix)]
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};
15
16 /// Raw file descriptors.
17 #[stable(feature = "rust1", since = "1.0.0")]
18 pub type RawFd = raw::c_int;
19
20 /// A trait to extract the raw file descriptor from an underlying object.
21 ///
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")]
26 pub trait AsRawFd {
27     /// Extracts the raw file descriptor.
28     ///
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
33     /// destroyed.
34     ///
35     /// However, borrowing is not strictly required. See [`AsFd::as_fd`]
36     /// for an API which strictly borrows a file descriptor.
37     ///
38     /// # Example
39     ///
40     /// ```no_run
41     /// use std::fs::File;
42     /// # use std::io;
43     /// #[cfg(unix)]
44     /// use std::os::unix::io::{AsRawFd, RawFd};
45     /// #[cfg(target_os = "wasi")]
46     /// use std::os::wasi::io::{AsRawFd, RawFd};
47     ///
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>(())
53     /// ```
54     #[stable(feature = "rust1", since = "1.0.0")]
55     fn as_raw_fd(&self) -> RawFd;
56 }
57
58 /// A trait to express the ability to construct an object from a raw file
59 /// descriptor.
60 #[stable(feature = "from_raw_os", since = "1.1.0")]
61 pub trait FromRawFd {
62     /// Constructs a new instance of `Self` from the given raw file
63     /// descriptor.
64     ///
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
68     /// scope.
69     ///
70     /// However, consuming ownership is not strictly required. Use a
71     /// [`From<OwnedFd>::from`] implementation for an API which strictly
72     /// consumes ownership.
73     ///
74     /// # Safety
75     ///
76     /// The `fd` passed in must be a valid and open file descriptor.
77     ///
78     /// # Example
79     ///
80     /// ```no_run
81     /// use std::fs::File;
82     /// # use std::io;
83     /// #[cfg(unix)]
84     /// use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
85     /// #[cfg(target_os = "wasi")]
86     /// use std::os::wasi::io::{FromRawFd, IntoRawFd, RawFd};
87     ///
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>(())
96     /// ```
97     #[stable(feature = "from_raw_os", since = "1.1.0")]
98     unsafe fn from_raw_fd(fd: RawFd) -> Self;
99 }
100
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.
106     ///
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.
110     ///
111     /// However, transferring ownership is not strictly required. Use a
112     /// [`Into<OwnedFd>::into`] implementation for an API which strictly
113     /// transfers ownership.
114     ///
115     /// # Example
116     ///
117     /// ```no_run
118     /// use std::fs::File;
119     /// # use std::io;
120     /// #[cfg(unix)]
121     /// use std::os::unix::io::{IntoRawFd, RawFd};
122     /// #[cfg(target_os = "wasi")]
123     /// use std::os::wasi::io::{IntoRawFd, RawFd};
124     ///
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>(())
129     /// ```
130     #[stable(feature = "into_raw_os", since = "1.4.0")]
131     fn into_raw_fd(self) -> RawFd;
132 }
133
134 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
135 impl AsRawFd for RawFd {
136     #[inline]
137     fn as_raw_fd(&self) -> RawFd {
138         *self
139     }
140 }
141 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
142 impl IntoRawFd for RawFd {
143     #[inline]
144     fn into_raw_fd(self) -> RawFd {
145         self
146     }
147 }
148 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
149 impl FromRawFd for RawFd {
150     #[inline]
151     unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
152         fd
153     }
154 }
155
156 #[stable(feature = "rust1", since = "1.0.0")]
157 impl AsRawFd for fs::File {
158     #[inline]
159     fn as_raw_fd(&self) -> RawFd {
160         self.as_inner().as_raw_fd()
161     }
162 }
163 #[stable(feature = "from_raw_os", since = "1.1.0")]
164 impl FromRawFd for fs::File {
165     #[inline]
166     unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
167         unsafe { fs::File::from(OwnedFd::from_raw_fd(fd)) }
168     }
169 }
170 #[stable(feature = "into_raw_os", since = "1.4.0")]
171 impl IntoRawFd for fs::File {
172     #[inline]
173     fn into_raw_fd(self) -> RawFd {
174         self.into_inner().into_inner().into_raw_fd()
175     }
176 }
177
178 #[stable(feature = "asraw_stdio", since = "1.21.0")]
179 impl AsRawFd for io::Stdin {
180     #[inline]
181     fn as_raw_fd(&self) -> RawFd {
182         libc::STDIN_FILENO
183     }
184 }
185
186 #[stable(feature = "asraw_stdio", since = "1.21.0")]
187 impl AsRawFd for io::Stdout {
188     #[inline]
189     fn as_raw_fd(&self) -> RawFd {
190         libc::STDOUT_FILENO
191     }
192 }
193
194 #[stable(feature = "asraw_stdio", since = "1.21.0")]
195 impl AsRawFd for io::Stderr {
196     #[inline]
197     fn as_raw_fd(&self) -> RawFd {
198         libc::STDERR_FILENO
199     }
200 }
201
202 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
203 impl<'a> AsRawFd for io::StdinLock<'a> {
204     #[inline]
205     fn as_raw_fd(&self) -> RawFd {
206         libc::STDIN_FILENO
207     }
208 }
209
210 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
211 impl<'a> AsRawFd for io::StdoutLock<'a> {
212     #[inline]
213     fn as_raw_fd(&self) -> RawFd {
214         libc::STDOUT_FILENO
215     }
216 }
217
218 #[stable(feature = "asraw_stdio_locks", since = "1.35.0")]
219 impl<'a> AsRawFd for io::StderrLock<'a> {
220     #[inline]
221     fn as_raw_fd(&self) -> RawFd {
222         libc::STDERR_FILENO
223     }
224 }
225
226 /// This impl allows implementing traits that require `AsRawFd` on Arc.
227 /// ```
228 /// # #[cfg(any(unix, target_os = "wasi"))] mod group_cfg {
229 /// # #[cfg(target_os = "wasi")]
230 /// # use std::os::wasi::io::AsRawFd;
231 /// # #[cfg(unix)]
232 /// # use std::os::unix::io::AsRawFd;
233 /// use std::net::UdpSocket;
234 /// use std::sync::Arc;
235 /// trait MyTrait: AsRawFd {
236 /// }
237 /// impl MyTrait for Arc<UdpSocket> {}
238 /// impl MyTrait for Box<UdpSocket> {}
239 /// # }
240 /// ```
241 #[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
242 impl<T: AsRawFd> AsRawFd for crate::sync::Arc<T> {
243     #[inline]
244     fn as_raw_fd(&self) -> RawFd {
245         (**self).as_raw_fd()
246     }
247 }
248
249 #[stable(feature = "asrawfd_ptrs", since = "1.63.0")]
250 impl<T: AsRawFd> AsRawFd for Box<T> {
251     #[inline]
252     fn as_raw_fd(&self) -> RawFd {
253         (**self).as_raw_fd()
254     }
255 }