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