async_std/os/unix/net/
stream.rs1use std::fmt;
4use std::net::Shutdown;
5use std::os::unix::net::UnixStream as StdUnixStream;
6use std::pin::Pin;
7
8use async_io::Async;
9
10use super::SocketAddr;
11use crate::io::{self, Read, Write};
12use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
13use crate::path::Path;
14use crate::sync::Arc;
15use crate::task::{Context, Poll};
16
17#[derive(Clone)]
41pub struct UnixStream {
42 pub(super) watcher: Arc<Async<StdUnixStream>>,
43}
44
45impl UnixStream {
46 pub async fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
60 let path = path.as_ref().to_owned();
61 let stream = Arc::new(Async::<StdUnixStream>::connect(path).await?);
62
63 Ok(UnixStream { watcher: stream })
64 }
65
66 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
82 let (a, b) = Async::<StdUnixStream>::pair()?;
83 let a = UnixStream {
84 watcher: Arc::new(a),
85 };
86 let b = UnixStream {
87 watcher: Arc::new(b),
88 };
89 Ok((a, b))
90 }
91
92 pub fn local_addr(&self) -> io::Result<SocketAddr> {
107 self.watcher.get_ref().local_addr()
108 }
109
110 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
125 self.watcher.get_ref().peer_addr()
126 }
127
128 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
147 self.watcher.get_ref().shutdown(how)
148 }
149}
150
151impl Read for UnixStream {
152 fn poll_read(
153 self: Pin<&mut Self>,
154 cx: &mut Context<'_>,
155 buf: &mut [u8],
156 ) -> Poll<io::Result<usize>> {
157 Pin::new(&mut &*self).poll_read(cx, buf)
158 }
159}
160
161impl Read for &UnixStream {
162 fn poll_read(
163 self: Pin<&mut Self>,
164 cx: &mut Context<'_>,
165 buf: &mut [u8],
166 ) -> Poll<io::Result<usize>> {
167 Pin::new(&mut &*self.watcher).poll_read(cx, buf)
168 }
169}
170
171impl Write for UnixStream {
172 fn poll_write(
173 self: Pin<&mut Self>,
174 cx: &mut Context<'_>,
175 buf: &[u8],
176 ) -> Poll<io::Result<usize>> {
177 Pin::new(&mut &*self).poll_write(cx, buf)
178 }
179
180 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
181 Pin::new(&mut &*self).poll_flush(cx)
182 }
183
184 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
185 Pin::new(&mut &*self).poll_close(cx)
186 }
187}
188
189impl Write for &UnixStream {
190 fn poll_write(
191 self: Pin<&mut Self>,
192 cx: &mut Context<'_>,
193 buf: &[u8],
194 ) -> Poll<io::Result<usize>> {
195 Pin::new(&mut &*self.watcher).poll_write(cx, buf)
196 }
197
198 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
199 Pin::new(&mut &*self.watcher).poll_flush(cx)
200 }
201
202 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
203 Pin::new(&mut &*self.watcher).poll_close(cx)
204 }
205}
206
207impl fmt::Debug for UnixStream {
208 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 let mut builder = f.debug_struct("UnixStream");
210 builder.field("fd", &self.as_raw_fd());
211
212 if let Ok(addr) = self.local_addr() {
213 builder.field("local", &addr);
214 }
215
216 if let Ok(addr) = self.peer_addr() {
217 builder.field("peer", &addr);
218 }
219
220 builder.finish()
221 }
222}
223
224impl From<StdUnixStream> for UnixStream {
225 fn from(stream: StdUnixStream) -> UnixStream {
227 let stream = Async::new(stream).expect("UnixStream is known to be good");
228 UnixStream {
229 watcher: Arc::new(stream),
230 }
231 }
232}
233
234impl std::convert::TryFrom<UnixStream> for StdUnixStream {
235 type Error = io::Error;
236 fn try_from(stream: UnixStream) -> io::Result<StdUnixStream> {
238 let inner = Arc::try_unwrap(stream.watcher)
239 .map_err(|_| io::Error::new(
240 io::ErrorKind::Other,
241 "Cannot convert UnixStream to synchronous: multiple references",
242 ))?
243 .into_inner()?;
244 inner.set_nonblocking(false)?;
245 Ok(inner)
246 }
247}
248
249impl AsRawFd for UnixStream {
250 fn as_raw_fd(&self) -> RawFd {
251 self.watcher.as_raw_fd()
252 }
253}
254
255impl FromRawFd for UnixStream {
256 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
257 let stream = std::os::unix::net::UnixStream::from_raw_fd(fd);
258 stream.into()
259 }
260}
261
262impl IntoRawFd for UnixStream {
263 fn into_raw_fd(self) -> RawFd {
264 (*self.watcher).get_ref().try_clone().unwrap().into_raw_fd()
265 }
266}
267
268cfg_io_safety! {
269 use crate::os::unix::io::{AsFd, BorrowedFd, OwnedFd};
270
271 impl AsFd for UnixStream {
272 fn as_fd(&self) -> BorrowedFd<'_> {
273 self.watcher.get_ref().as_fd()
274 }
275 }
276
277 impl From<OwnedFd> for UnixStream {
278 fn from(fd: OwnedFd) -> UnixStream {
279 std::os::unix::net::UnixStream::from(fd).into()
280 }
281 }
282
283 impl From<UnixStream> for OwnedFd {
284 fn from(stream: UnixStream) -> OwnedFd {
285 stream.watcher.get_ref().try_clone().unwrap().into()
286 }
287 }
288}