cap_async_std/os/unix/net/
unix_stream.rs

1use 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/// A Unix stream socket.
12///
13/// This corresponds to [`async_std::os::unix::net::UnixStream`].
14///
15/// This `UnixStream` has no `connect` method. To create a `UnixStream`, first
16/// obtain a [`Dir`] containing the path, and then call
17/// [`Dir::connect_unix_stream`].
18///
19/// [`async_std::os::unix::net::UnixStream`]: https://docs.rs/async-std/latest/async_std/os/unix/net/struct.UnixStream.html
20/// [`Dir`]: struct.Dir.html
21/// [`Dir::connect_unix_stream`]: struct.Dir.html#method.connect_unix_stream
22#[derive(Clone)]
23pub struct UnixStream {
24    std: unix::net::UnixStream,
25}
26
27impl UnixStream {
28    /// Constructs a new instance of `Self` from the given
29    /// `async_std::os::unix::net::UnixStream`.
30    ///
31    /// This grants access the resources the
32    /// `async_std::os::unix::net::UnixStream` instance already has access
33    /// to.
34    #[inline]
35    pub fn from_std(std: unix::net::UnixStream) -> Self {
36        Self { std }
37    }
38
39    /// Creates an unnamed pair of connected sockets.
40    ///
41    /// This corresponds to [`async_std::os::unix::net::UnixStream::pair`].
42    ///
43    /// TODO: should this require a capability?
44    ///
45    /// [`async_std::os::unix::net::UnixStream::pair`]: https://docs.rs/async-std/latest/async_std/os/unix/net/struct.UnixStream.html#method.pair
46    #[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    // async_std doesn't have `try_clone`.
52
53    /// Returns the socket address of the local half of this connection.
54    ///
55    /// This corresponds to
56    /// [`async_std::os::unix::net::UnixStream::local_addr`].
57    ///
58    /// [`async_std::os::unix::net::UnixStream::local_addr`]: https://docs.rs/async-std/latest/async_std/os/unix/net/struct.UnixStream.html#method.local_addr
59    #[inline]
60    pub fn local_addr(&self) -> io::Result<SocketAddr> {
61        self.std.local_addr()
62    }
63
64    /// Returns the socket address of the remote half of this connection.
65    ///
66    /// This corresponds to
67    /// [`async_std::os::unix::net::UnixStream::peer_addr`].
68    ///
69    /// [`async_std::os::unix::net::UnixStream::peer_addr`]: https://docs.rs/async-std/latest/async_std/os/unix/net/struct.UnixStream.html#method.peer_addr
70    #[inline]
71    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
72        self.std.peer_addr()
73    }
74
75    // async_std doesn't have `set_read_timeout`.
76
77    // async_std doesn't have `set_write_timeout`.
78
79    // async_std doesn't have `read_timeout`.
80
81    // async_std doesn't have `write_timeout`.
82
83    // async_std doesn't have `set_nonblocking`.
84
85    // async_std doesn't have `take_error`.
86
87    /// Shuts down the read, write, or both halves of this connection.
88    ///
89    /// This corresponds to [`async_std::os::unix::net::UnixStream::shutdown`].
90    ///
91    /// [`async_std::os::unix::net::UnixStream::shutdown`]: https://docs.rs/async-std/latest/async_std/os/unix/net/struct.UnixStream.html#method.shutdown
92    #[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    // async_std doesn't have `is_read_vectored`.
160
161    // async_std doesn't have `initializer`.
162}
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    // async_std doesn't have `is_read_vectored`.
184
185    // async_std doesn't have `initializer`.
186}
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    // async_std doesn't have `is_write_vectored`.
218
219    // async_std doesn't have `write_all_vectored`.
220}
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    // async_std doesn't have `is_write_vectored`.
248
249    // async_std doesn't have `write_all_vectored`.
250}
251
252impl fmt::Debug for UnixStream {
253    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254        self.std.fmt(f)
255    }
256}