]> git.proxmox.com Git - rustc.git/blob - library/std/src/sys/wasi/ext/io.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / std / src / sys / wasi / ext / 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 = "none")]
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 fn as_raw_fd(&self) -> RawFd {
58 *self
59 }
60 }
61 #[stable(feature = "raw_fd_reflexive_traits", since = "1.48.0")]
62 impl IntoRawFd for RawFd {
63 fn into_raw_fd(self) -> RawFd {
64 self
65 }
66 }
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 {
70 fd
71 }
72 }
73
74 impl AsRawFd for net::TcpStream {
75 fn as_raw_fd(&self) -> RawFd {
76 self.as_inner().fd().as_raw()
77 }
78 }
79
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))
83 }
84 }
85
86 impl IntoRawFd for net::TcpStream {
87 fn into_raw_fd(self) -> RawFd {
88 self.into_inner().into_fd().into_raw()
89 }
90 }
91
92 impl AsRawFd for net::TcpListener {
93 fn as_raw_fd(&self) -> RawFd {
94 self.as_inner().fd().as_raw()
95 }
96 }
97
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))
101 }
102 }
103
104 impl IntoRawFd for net::TcpListener {
105 fn into_raw_fd(self) -> RawFd {
106 self.into_inner().into_fd().into_raw()
107 }
108 }
109
110 impl AsRawFd for net::UdpSocket {
111 fn as_raw_fd(&self) -> RawFd {
112 self.as_inner().fd().as_raw()
113 }
114 }
115
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))
119 }
120 }
121
122 impl IntoRawFd for net::UdpSocket {
123 fn into_raw_fd(self) -> RawFd {
124 self.into_inner().into_fd().into_raw()
125 }
126 }
127
128 impl AsRawFd for fs::File {
129 fn as_raw_fd(&self) -> RawFd {
130 self.as_inner().fd().as_raw()
131 }
132 }
133
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))
137 }
138 }
139
140 impl IntoRawFd for fs::File {
141 fn into_raw_fd(self) -> RawFd {
142 self.into_inner().into_fd().into_raw()
143 }
144 }
145
146 impl AsRawFd for io::Stdin {
147 fn as_raw_fd(&self) -> RawFd {
148 sys::stdio::Stdin.as_raw_fd()
149 }
150 }
151
152 impl AsRawFd for io::Stdout {
153 fn as_raw_fd(&self) -> RawFd {
154 sys::stdio::Stdout.as_raw_fd()
155 }
156 }
157
158 impl AsRawFd for io::Stderr {
159 fn as_raw_fd(&self) -> RawFd {
160 sys::stdio::Stderr.as_raw_fd()
161 }
162 }