cap_async_std/os/unix/net/
unix_stream.rs1use crate::net::Shutdown;
2use crate::os::unix::net::SocketAddr;
3use async_std::io::{self, IoSlice, IoSliceMut, Read, Write};
4use async_std::os::unix;
5use async_std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
6use async_std::task::{Context, Poll};
7use io_lifetimes::{AsFd, BorrowedFd, OwnedFd};
8use std::fmt;
9use std::pin::Pin;
10
11#[derive(Clone)]
23pub struct UnixStream {
24    std: unix::net::UnixStream,
25}
26
27impl UnixStream {
28    #[inline]
35    pub fn from_std(std: unix::net::UnixStream) -> Self {
36        Self { std }
37    }
38
39    #[inline]
47    pub fn pair() -> io::Result<(Self, Self)> {
48        unix::net::UnixStream::pair().map(|(a, b)| (Self::from_std(a), Self::from_std(b)))
49    }
50
51    #[inline]
60    pub fn local_addr(&self) -> io::Result<SocketAddr> {
61        self.std.local_addr()
62    }
63
64    #[inline]
71    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
72        self.std.peer_addr()
73    }
74
75    #[inline]
93    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
94        self.std.shutdown(how)
95    }
96}
97
98impl FromRawFd for UnixStream {
99    #[inline]
100    unsafe fn from_raw_fd(fd: RawFd) -> Self {
101        Self::from_std(unix::net::UnixStream::from_raw_fd(fd))
102    }
103}
104
105impl From<OwnedFd> for UnixStream {
106    #[inline]
107    fn from(fd: OwnedFd) -> Self {
108        Self::from_std(unix::net::UnixStream::from(fd))
109    }
110}
111
112impl AsRawFd for UnixStream {
113    #[inline]
114    fn as_raw_fd(&self) -> RawFd {
115        self.std.as_raw_fd()
116    }
117}
118
119impl AsFd for UnixStream {
120    #[inline]
121    fn as_fd(&self) -> BorrowedFd<'_> {
122        self.std.as_fd()
123    }
124}
125
126impl IntoRawFd for UnixStream {
127    #[inline]
128    fn into_raw_fd(self) -> RawFd {
129        self.std.into_raw_fd()
130    }
131}
132
133impl From<UnixStream> for OwnedFd {
134    #[inline]
135    fn from(stream: UnixStream) -> OwnedFd {
136        stream.std.into()
137    }
138}
139
140impl Read for UnixStream {
141    #[inline]
142    fn poll_read(
143        mut self: Pin<&mut Self>,
144        cx: &mut Context,
145        buf: &mut [u8],
146    ) -> Poll<io::Result<usize>> {
147        Read::poll_read(Pin::new(&mut self.std), cx, buf)
148    }
149
150    #[inline]
151    fn poll_read_vectored(
152        mut self: Pin<&mut Self>,
153        cx: &mut Context,
154        bufs: &mut [IoSliceMut],
155    ) -> Poll<io::Result<usize>> {
156        Read::poll_read_vectored(Pin::new(&mut self.std), cx, bufs)
157    }
158
159    }
163
164impl Read for &UnixStream {
165    #[inline]
166    fn poll_read(
167        self: Pin<&mut Self>,
168        cx: &mut Context,
169        buf: &mut [u8],
170    ) -> Poll<io::Result<usize>> {
171        Read::poll_read(Pin::new(&mut &self.std), cx, buf)
172    }
173
174    #[inline]
175    fn poll_read_vectored(
176        self: Pin<&mut Self>,
177        cx: &mut Context,
178        bufs: &mut [IoSliceMut],
179    ) -> Poll<io::Result<usize>> {
180        Read::poll_read_vectored(Pin::new(&mut &self.std), cx, bufs)
181    }
182
183    }
187
188impl Write for UnixStream {
189    #[inline]
190    fn poll_write(
191        mut self: Pin<&mut Self>,
192        cx: &mut Context,
193        buf: &[u8],
194    ) -> Poll<io::Result<usize>> {
195        Write::poll_write(Pin::new(&mut self.std), cx, buf)
196    }
197
198    #[inline]
199    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
200        Write::poll_flush(Pin::new(&mut self.std), cx)
201    }
202
203    #[inline]
204    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
205        Write::poll_close(Pin::new(&mut self.std), cx)
206    }
207
208    #[inline]
209    fn poll_write_vectored(
210        mut self: Pin<&mut Self>,
211        cx: &mut Context,
212        bufs: &[IoSlice],
213    ) -> Poll<io::Result<usize>> {
214        Write::poll_write_vectored(Pin::new(&mut self.std), cx, bufs)
215    }
216
217    }
221
222impl Write for &UnixStream {
223    #[inline]
224    fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
225        Write::poll_write(Pin::new(&mut &self.std), cx, buf)
226    }
227
228    #[inline]
229    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
230        Write::poll_flush(Pin::new(&mut &self.std), cx)
231    }
232
233    #[inline]
234    fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
235        Write::poll_close(Pin::new(&mut &self.std), cx)
236    }
237
238    #[inline]
239    fn poll_write_vectored(
240        self: Pin<&mut Self>,
241        cx: &mut Context,
242        bufs: &[IoSlice],
243    ) -> Poll<io::Result<usize>> {
244        Write::poll_write_vectored(Pin::new(&mut &self.std), cx, bufs)
245    }
246
247    }
251
252impl fmt::Debug for UnixStream {
253    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254        self.std.fmt(f)
255    }
256}