1 //! WASI-specific extensions to general I/O primitives
3 #![deny(unsafe_op_in_unsafe_fn)]
4 #![unstable(feature = "wasi_ext", issue = "none")]
10 use crate::sys_common::{AsInner, FromInner, IntoInner};
12 /// Raw file descriptors.
15 /// A trait to extract the raw WASI file descriptor from an underlying
18 /// Extracts the raw file descriptor.
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;
26 /// A trait to express the ability to construct an object from a raw file
29 /// Constructs a new instance of `Self` from the given raw file
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.
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;
44 /// A trait to express the ability to consume an object and acquire ownership of
45 /// its raw file descriptor.
47 /// Consumes this object, returning the raw underlying file descriptor.
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;
55 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
56 impl AsRawFd for RawFd {
57 fn as_raw_fd(&self) -> RawFd {
61 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
62 impl IntoRawFd for RawFd {
63 fn into_raw_fd(self) -> RawFd {
67 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
68 impl FromRawFd for RawFd {
69 unsafe fn from_raw_fd(fd: RawFd) -> RawFd {
74 impl AsRawFd for net::TcpStream {
75 fn as_raw_fd(&self) -> RawFd {
76 self.as_inner().fd().as_raw()
80 impl FromRawFd for net::TcpStream {
81 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
82 net::TcpStream::from_inner(sys::net::TcpStream::from_inner(fd))
86 impl IntoRawFd for net::TcpStream {
87 fn into_raw_fd(self) -> RawFd {
88 self.into_inner().into_fd().into_raw()
92 impl AsRawFd for net::TcpListener {
93 fn as_raw_fd(&self) -> RawFd {
94 self.as_inner().fd().as_raw()
98 impl FromRawFd for net::TcpListener {
99 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
100 net::TcpListener::from_inner(sys::net::TcpListener::from_inner(fd))
104 impl IntoRawFd for net::TcpListener {
105 fn into_raw_fd(self) -> RawFd {
106 self.into_inner().into_fd().into_raw()
110 impl AsRawFd for net::UdpSocket {
111 fn as_raw_fd(&self) -> RawFd {
112 self.as_inner().fd().as_raw()
116 impl FromRawFd for net::UdpSocket {
117 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
118 net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(fd))
122 impl IntoRawFd for net::UdpSocket {
123 fn into_raw_fd(self) -> RawFd {
124 self.into_inner().into_fd().into_raw()
128 impl AsRawFd for fs::File {
129 fn as_raw_fd(&self) -> RawFd {
130 self.as_inner().fd().as_raw()
134 impl FromRawFd for fs::File {
135 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
136 fs::File::from_inner(sys::fs::File::from_inner(fd))
140 impl IntoRawFd for fs::File {
141 fn into_raw_fd(self) -> RawFd {
142 self.into_inner().into_fd().into_raw()
146 impl AsRawFd for io::Stdin {
147 fn as_raw_fd(&self) -> RawFd {
148 sys::stdio::Stdin.as_raw_fd()
152 impl AsRawFd for io::Stdout {
153 fn as_raw_fd(&self) -> RawFd {
154 sys::stdio::Stdout.as_raw_fd()
158 impl AsRawFd for io::Stderr {
159 fn as_raw_fd(&self) -> RawFd {
160 sys::stdio::Stderr.as_raw_fd()
164 impl<'a> AsRawFd for io::StdinLock<'a> {
165 fn as_raw_fd(&self) -> RawFd {
166 sys::stdio::Stdin.as_raw_fd()
170 impl<'a> AsRawFd for io::StdoutLock<'a> {
171 fn as_raw_fd(&self) -> RawFd {
172 sys::stdio::Stdout.as_raw_fd()
176 impl<'a> AsRawFd for io::StderrLock<'a> {
177 fn as_raw_fd(&self) -> RawFd {
178 sys::stdio::Stderr.as_raw_fd()