]> git.lizzy.rs Git - rust.git/blob - library/std/src/os/wasi/io.rs
Add tracking issue number to `wasi_ext`
[rust.git] / library / std / src / os / wasi / io.rs
1 //! WASI-specific extensions to general I/O primitives
2
3 #![deny(unsafe_op_in_unsafe_fn)]
4 #![unstable(feature = "wasi_ext", issue = "71213")]
5
6 use crate::fs;
7 use crate::io;
8 use crate::net;
9 use crate::sys;
10 use crate::sys_common::{AsInner, FromInner, IntoInner};
11
12 /// Raw file descriptors.
13 pub type RawFd = u32;
14
15 /// A trait to extract the raw WASI file descriptor from an underlying
16 /// object.
17 pub trait AsRawFd {
18     /// Extracts the raw file descriptor.
19     ///
20     /// This method does **not** pass ownership of the raw file descriptor
21     /// to the caller. The descriptor is only guaranteed to be valid while
22     /// the original object has not yet been destroyed.
23     fn as_raw_fd(&self) -> RawFd;
24 }
25
26 /// A trait to express the ability to construct an object from a raw file
27 /// descriptor.
28 pub trait FromRawFd {
29     /// Constructs a new instance of `Self` from the given raw file
30     /// descriptor.
31     ///
32     /// This function **consumes ownership** of the specified file
33     /// descriptor. The returned object will take responsibility for closing
34     /// it when the object goes out of scope.
35     ///
36     /// This function is also unsafe as the primitives currently returned
37     /// have the contract that they are the sole owner of the file
38     /// descriptor they are wrapping. Usage of this function could
39     /// accidentally allow violating this contract which can cause memory
40     /// unsafety in code that relies on it being true.
41     unsafe fn from_raw_fd(fd: RawFd) -> Self;
42 }
43
44 /// A trait to express the ability to consume an object and acquire ownership of
45 /// its raw file descriptor.
46 pub trait IntoRawFd {
47     /// Consumes this object, returning the raw underlying file descriptor.
48     ///
49     /// This function **transfers ownership** of the underlying file descriptor
50     /// to the caller. Callers are then the unique owners of the file descriptor
51     /// and must close the descriptor once it's no longer needed.
52     fn into_raw_fd(self) -> RawFd;
53 }
54
55 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
56 impl AsRawFd for RawFd {
57     #[inline]
58     fn as_raw_fd(&self) -> RawFd {
59         *self
60     }
61 }
62 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
63 impl IntoRawFd for RawFd {
64     #[inline]
65     fn into_raw_fd(self) -> RawFd {
66         self
67     }
68 }
69 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
70 impl FromRawFd for RawFd {
71     #[inline]
72     unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
73         fd
74     }
75 }
76
77 impl AsRawFd for net::TcpStream {
78     #[inline]
79     fn as_raw_fd(&self) -> RawFd {
80         self.as_inner().fd().as_raw()
81     }
82 }
83
84 impl FromRawFd for net::TcpStream {
85     #[inline]
86     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
87         net::TcpStream::from_inner(sys::net::TcpStream::from_inner(fd))
88     }
89 }
90
91 impl IntoRawFd for net::TcpStream {
92     #[inline]
93     fn into_raw_fd(self) -> RawFd {
94         self.into_inner().into_fd().into_raw()
95     }
96 }
97
98 impl AsRawFd for net::TcpListener {
99     #[inline]
100     fn as_raw_fd(&self) -> RawFd {
101         self.as_inner().fd().as_raw()
102     }
103 }
104
105 impl FromRawFd for net::TcpListener {
106     #[inline]
107     unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
108         net::TcpListener::from_inner(sys::net::TcpListener::from_inner(fd))
109     }
110 }
111
112 impl IntoRawFd for net::TcpListener {
113     #[inline]
114     fn into_raw_fd(self) -> RawFd {
115         self.into_inner().into_fd().into_raw()
116     }
117 }
118
119 impl AsRawFd for net::UdpSocket {
120     #[inline]
121     fn as_raw_fd(&self) -> RawFd {
122         self.as_inner().fd().as_raw()
123     }
124 }
125
126 impl FromRawFd for net::UdpSocket {
127     #[inline]
128     unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
129         net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(fd))
130     }
131 }
132
133 impl IntoRawFd for net::UdpSocket {
134     #[inline]
135     fn into_raw_fd(self) -> RawFd {
136         self.into_inner().into_fd().into_raw()
137     }
138 }
139
140 impl AsRawFd for fs::File {
141     #[inline]
142     fn as_raw_fd(&self) -> RawFd {
143         self.as_inner().fd().as_raw()
144     }
145 }
146
147 impl FromRawFd for fs::File {
148     #[inline]
149     unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
150         fs::File::from_inner(sys::fs::File::from_inner(fd))
151     }
152 }
153
154 impl IntoRawFd for fs::File {
155     #[inline]
156     fn into_raw_fd(self) -> RawFd {
157         self.into_inner().into_fd().into_raw()
158     }
159 }
160
161 impl AsRawFd for io::Stdin {
162     #[inline]
163     fn as_raw_fd(&self) -> RawFd {
164         libc::STDIN_FILENO as RawFd
165     }
166 }
167
168 impl AsRawFd for io::Stdout {
169     #[inline]
170     fn as_raw_fd(&self) -> RawFd {
171         libc::STDOUT_FILENO as RawFd
172     }
173 }
174
175 impl AsRawFd for io::Stderr {
176     #[inline]
177     fn as_raw_fd(&self) -> RawFd {
178         libc::STDERR_FILENO as RawFd
179     }
180 }
181
182 impl<'a> AsRawFd for io::StdinLock<'a> {
183     #[inline]
184     fn as_raw_fd(&self) -> RawFd {
185         libc::STDIN_FILENO as RawFd
186     }
187 }
188
189 impl<'a> AsRawFd for io::StdoutLock<'a> {
190     #[inline]
191     fn as_raw_fd(&self) -> RawFd {
192         libc::STDOUT_FILENO as RawFd
193     }
194 }
195
196 impl<'a> AsRawFd for io::StderrLock<'a> {
197     #[inline]
198     fn as_raw_fd(&self) -> RawFd {
199         libc::STDERR_FILENO as RawFd
200     }
201 }