]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/wasi/ext/io.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / src / libstd / sys / wasi / ext / io.rs
1 //! WASI-specific extensions to general I/O primitives
2
3 #![unstable(feature = "wasi_ext", issue = "0")]
4
5 use crate::fs;
6 use crate::io;
7 use crate::sys;
8 use crate::net;
9 use crate::sys_common::{AsInner, FromInner, IntoInner};
10
11 /// Raw file descriptors.
12 pub type RawFd = u32;
13
14 /// A trait to extract the raw WASI file descriptor from an underlying
15 /// object.
16 pub trait AsRawFd {
17 /// Extracts the raw file descriptor.
18 ///
19 /// This method does **not** pass ownership of the raw file descriptor
20 /// to the caller. The descriptor is only guaranteed to be valid while
21 /// the original object has not yet been destroyed.
22 fn as_raw_fd(&self) -> RawFd;
23 }
24
25 /// A trait to express the ability to construct an object from a raw file
26 /// descriptor.
27 pub trait FromRawFd {
28 /// Constructs a new instance of `Self` from the given raw file
29 /// descriptor.
30 ///
31 /// This function **consumes ownership** of the specified file
32 /// descriptor. The returned object will take responsibility for closing
33 /// it when the object goes out of scope.
34 ///
35 /// This function is also unsafe as the primitives currently returned
36 /// have the contract that they are the sole owner of the file
37 /// descriptor they are wrapping. Usage of this function could
38 /// accidentally allow violating this contract which can cause memory
39 /// unsafety in code that relies on it being true.
40 unsafe fn from_raw_fd(fd: RawFd) -> Self;
41 }
42
43 /// A trait to express the ability to consume an object and acquire ownership of
44 /// its raw file descriptor.
45 pub trait IntoRawFd {
46 /// Consumes this object, returning the raw underlying file descriptor.
47 ///
48 /// This function **transfers ownership** of the underlying file descriptor
49 /// to the caller. Callers are then the unique owners of the file descriptor
50 /// and must close the descriptor once it's no longer needed.
51 fn into_raw_fd(self) -> RawFd;
52 }
53
54 impl AsRawFd for net::TcpStream {
55 fn as_raw_fd(&self) -> RawFd {
56 self.as_inner().fd().as_raw()
57 }
58 }
59
60 impl FromRawFd for net::TcpStream {
61 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpStream {
62 net::TcpStream::from_inner(sys::net::TcpStream::from_inner(fd))
63 }
64 }
65
66 impl IntoRawFd for net::TcpStream {
67 fn into_raw_fd(self) -> RawFd {
68 self.into_inner().into_fd().into_raw()
69 }
70 }
71
72 impl AsRawFd for net::TcpListener {
73 fn as_raw_fd(&self) -> RawFd {
74 self.as_inner().fd().as_raw()
75 }
76 }
77
78 impl FromRawFd for net::TcpListener {
79 unsafe fn from_raw_fd(fd: RawFd) -> net::TcpListener {
80 net::TcpListener::from_inner(sys::net::TcpListener::from_inner(fd))
81 }
82 }
83
84 impl IntoRawFd for net::TcpListener {
85 fn into_raw_fd(self) -> RawFd {
86 self.into_inner().into_fd().into_raw()
87 }
88 }
89
90 impl AsRawFd for net::UdpSocket {
91 fn as_raw_fd(&self) -> RawFd {
92 self.as_inner().fd().as_raw()
93 }
94 }
95
96 impl FromRawFd for net::UdpSocket {
97 unsafe fn from_raw_fd(fd: RawFd) -> net::UdpSocket {
98 net::UdpSocket::from_inner(sys::net::UdpSocket::from_inner(fd))
99 }
100 }
101
102 impl IntoRawFd for net::UdpSocket {
103 fn into_raw_fd(self) -> RawFd {
104 self.into_inner().into_fd().into_raw()
105 }
106 }
107
108 impl AsRawFd for fs::File {
109 fn as_raw_fd(&self) -> RawFd {
110 self.as_inner().fd().as_raw()
111 }
112 }
113
114 impl FromRawFd for fs::File {
115 unsafe fn from_raw_fd(fd: RawFd) -> fs::File {
116 fs::File::from_inner(sys::fs::File::from_inner(fd))
117 }
118 }
119
120 impl IntoRawFd for fs::File {
121 fn into_raw_fd(self) -> RawFd {
122 self.into_inner().into_fd().into_raw()
123 }
124 }
125
126 impl AsRawFd for io::Stdin {
127 fn as_raw_fd(&self) -> RawFd {
128 sys::stdio::Stdin.as_raw_fd()
129 }
130 }
131
132 impl AsRawFd for io::Stdout {
133 fn as_raw_fd(&self) -> RawFd {
134 sys::stdio::Stdout.as_raw_fd()
135 }
136 }
137
138 impl AsRawFd for io::Stderr {
139 fn as_raw_fd(&self) -> RawFd {
140 sys::stdio::Stderr.as_raw_fd()
141 }
142 }