async_std/os/unix/net/
stream.rs1use std::fmt;
4use std::io::{Read as _, Write as _};
5use std::net::Shutdown;
6use std::pin::Pin;
7
8use mio_uds;
9
10use super::SocketAddr;
11use crate::io::{self, Read, Write};
12use crate::net::driver::Watcher;
13use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
14use crate::path::Path;
15use crate::task::{spawn_blocking, Context, Poll};
16
17pub struct UnixStream {
41 pub(super) watcher: Watcher<mio_uds::UnixStream>,
42}
43
44impl UnixStream {
45 pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
59 let path = path.as_ref().to_owned();
60
61 spawn_blocking(move || {
62 let std_stream = std::os::unix::net::UnixStream::connect(path)?;
63 let mio_stream = mio_uds::UnixStream::from_stream(std_stream)?;
64 Ok(UnixStream {
65 watcher: Watcher::new(mio_stream),
66 })
67 })
68 .await?
69 }
70
71 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
87 let (a, b) = mio_uds::UnixStream::pair()?;
88 let a = UnixStream {
89 watcher: Watcher::new(a),
90 };
91 let b = UnixStream {
92 watcher: Watcher::new(b),
93 };
94 Ok((a, b))
95 }
96
97 pub fn local_addr(&self) -> io::Result<SocketAddr> {
112 self.watcher.get_ref().local_addr()
113 }
114
115 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
130 self.watcher.get_ref().peer_addr()
131 }
132
133 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
152 self.watcher.get_ref().shutdown(how)
153 }
154}
155
156impl Read for UnixStream {
157 fn poll_read(
158 self: Pin<&mut Self>,
159 cx: &mut Context<'_>,
160 buf: &mut [u8],
161 ) -> Poll<io::Result<usize>> {
162 Pin::new(&mut &*self).poll_read(cx, buf)
163 }
164}
165
166impl Read for &UnixStream {
167 fn poll_read(
168 self: Pin<&mut Self>,
169 cx: &mut Context<'_>,
170 buf: &mut [u8],
171 ) -> Poll<io::Result<usize>> {
172 self.watcher.poll_read_with(cx, |mut inner| inner.read(buf))
173 }
174}
175
176impl Write for UnixStream {
177 fn poll_write(
178 self: Pin<&mut Self>,
179 cx: &mut Context<'_>,
180 buf: &[u8],
181 ) -> Poll<io::Result<usize>> {
182 Pin::new(&mut &*self).poll_write(cx, buf)
183 }
184
185 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
186 Pin::new(&mut &*self).poll_flush(cx)
187 }
188
189 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
190 Pin::new(&mut &*self).poll_close(cx)
191 }
192}
193
194impl Write for &UnixStream {
195 fn poll_write(
196 self: Pin<&mut Self>,
197 cx: &mut Context<'_>,
198 buf: &[u8],
199 ) -> Poll<io::Result<usize>> {
200 self.watcher
201 .poll_write_with(cx, |mut inner| inner.write(buf))
202 }
203
204 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
205 self.watcher.poll_write_with(cx, |mut inner| inner.flush())
206 }
207
208 fn poll_close(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<io::Result<()>> {
209 Poll::Ready(Ok(()))
210 }
211}
212
213impl fmt::Debug for UnixStream {
214 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215 let mut builder = f.debug_struct("UnixStream");
216 builder.field("fd", &self.as_raw_fd());
217
218 if let Ok(addr) = self.local_addr() {
219 builder.field("local", &addr);
220 }
221
222 if let Ok(addr) = self.peer_addr() {
223 builder.field("peer", &addr);
224 }
225
226 builder.finish()
227 }
228}
229
230impl From<std::os::unix::net::UnixStream> for UnixStream {
231 fn from(stream: std::os::unix::net::UnixStream) -> UnixStream {
233 let mio_stream = mio_uds::UnixStream::from_stream(stream).unwrap();
234 UnixStream {
235 watcher: Watcher::new(mio_stream),
236 }
237 }
238}
239
240impl AsRawFd for UnixStream {
241 fn as_raw_fd(&self) -> RawFd {
242 self.watcher.get_ref().as_raw_fd()
243 }
244}
245
246impl FromRawFd for UnixStream {
247 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
248 let stream = std::os::unix::net::UnixStream::from_raw_fd(fd);
249 stream.into()
250 }
251}
252
253impl IntoRawFd for UnixStream {
254 fn into_raw_fd(self) -> RawFd {
255 self.watcher.into_inner().into_raw_fd()
256 }
257}