cap_std/os/unix/net/
unix_stream.rs

1use crate::net::Shutdown;
2use crate::os::unix::net::SocketAddr;
3use io_lifetimes::{AsFd, BorrowedFd, OwnedFd};
4use std::fmt;
5use std::io::{self, IoSlice, IoSliceMut, Read, Write};
6use std::os::unix;
7use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
8use std::time::Duration;
9
10/// A Unix stream socket.
11///
12/// This corresponds to [`std::os::unix::net::UnixStream`].
13///
14/// This `UnixStream` has no `connect` method. To create a `UnixStream`, first
15/// obtain a [`Dir`] containing the path, and then call
16/// [`Dir::connect_unix_stream`].
17///
18/// [`std::os::unix::net::UnixStream`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html
19/// [`Dir`]: struct.Dir.html
20/// [`Dir::connect_unix_stream`]: struct.Dir.html#method.connect_unix_stream
21pub struct UnixStream {
22    std: unix::net::UnixStream,
23}
24
25impl UnixStream {
26    /// Constructs a new instance of `Self` from the given
27    /// `std::os::unix::net::UnixStream`.
28    ///
29    /// This grants access the resources the `std::os::unix::net::UnixStream`
30    /// instance already has access to.
31    #[inline]
32    pub fn from_std(std: unix::net::UnixStream) -> Self {
33        Self { std }
34    }
35
36    /// Creates an unnamed pair of connected sockets.
37    ///
38    /// This corresponds to [`std::os::unix::net::UnixStream::pair`].
39    ///
40    /// TODO: should this require a capability?
41    ///
42    /// [`std::os::unix::net::UnixStream::pair`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.pair
43    #[inline]
44    pub fn pair() -> io::Result<(Self, Self)> {
45        unix::net::UnixStream::pair().map(|(a, b)| (Self::from_std(a), Self::from_std(b)))
46    }
47
48    /// Creates a new independently owned handle to the underlying socket.
49    ///
50    /// This corresponds to [`std::os::unix::net::UnixStream::try_clone`].
51    ///
52    /// [`std::os::unix::net::UnixStream::try_clone`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.try_clone
53    #[inline]
54    pub fn try_clone(&self) -> io::Result<Self> {
55        let unix_stream = self.std.try_clone()?;
56        Ok(Self::from_std(unix_stream))
57    }
58
59    /// Returns the socket address of the local half of this connection.
60    ///
61    /// This corresponds to [`std::os::unix::net::UnixStream::local_addr`].
62    ///
63    /// [`std::os::unix::net::UnixStream::local_addr`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.local_addr
64    #[inline]
65    pub fn local_addr(&self) -> io::Result<SocketAddr> {
66        self.std.local_addr()
67    }
68
69    /// Returns the socket address of the remote half of this connection.
70    ///
71    /// This corresponds to [`std::os::unix::net::UnixStream::peer_addr`].
72    ///
73    /// [`std::os::unix::net::UnixStream::peer_addr`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.peer_addr
74    #[inline]
75    pub fn peer_addr(&self) -> io::Result<SocketAddr> {
76        self.std.peer_addr()
77    }
78
79    /// Sets the read timeout for the socket.
80    ///
81    /// This corresponds to
82    /// [`std::os::unix::net::UnixStream::set_read_timeout`].
83    ///
84    /// [`std::os::unix::net::UnixStream::set_read_timeout`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.set_read_timeout
85    #[inline]
86    pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
87        self.std.set_read_timeout(timeout)
88    }
89
90    /// Sets the write timeout for the socket.
91    ///
92    /// This corresponds to
93    /// [`std::os::unix::net::UnixStream::set_write_timeout`].
94    ///
95    /// [`std::os::unix::net::UnixStream::set_write_timeout`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.set_write_timeout
96    #[inline]
97    pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
98        self.std.set_write_timeout(timeout)
99    }
100
101    /// Returns the read timeout of this socket.
102    ///
103    /// This corresponds to [`std::os::unix::net::UnixStream::read_timeout`].
104    ///
105    /// [`std::os::unix::net::UnixStream::read_timeout`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.read_timeout
106    #[inline]
107    pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
108        self.std.read_timeout()
109    }
110
111    /// Returns the write timeout of this socket.
112    ///
113    /// This corresponds to [`std::os::unix::net::UnixStream::write_timeout`].
114    ///
115    /// [`std::os::unix::net::UnixStream::write_timeout`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.write_timeout
116    #[inline]
117    pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
118        self.std.write_timeout()
119    }
120
121    /// Moves the socket into or out of nonblocking mode.
122    ///
123    /// This corresponds to
124    /// [`std::os::unix::net::UnixStream::set_nonblocking`].
125    ///
126    /// [`std::os::unix::net::UnixStream::set_nonblocking`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.set_nonblocking
127    #[inline]
128    pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
129        self.std.set_nonblocking(nonblocking)
130    }
131
132    /// Returns the value of the `SO_ERROR` option.
133    ///
134    /// This corresponds to [`std::os::unix::net::UnixStream::take_error`].
135    ///
136    /// [`std::os::unix::net::UnixStream::take_error`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.take_error
137    #[inline]
138    pub fn take_error(&self) -> io::Result<Option<io::Error>> {
139        self.std.take_error()
140    }
141
142    /// Shuts down the read, write, or both halves of this connection.
143    ///
144    /// This corresponds to [`std::os::unix::net::UnixStream::shutdown`].
145    ///
146    /// [`std::os::unix::net::UnixStream::shutdown`]: https://doc.rust-lang.org/std/os/unix/net/struct.UnixStream.html#method.shutdown
147    #[inline]
148    pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
149        self.std.shutdown(how)
150    }
151}
152
153impl FromRawFd for UnixStream {
154    #[inline]
155    unsafe fn from_raw_fd(fd: RawFd) -> Self {
156        Self::from_std(unix::net::UnixStream::from_raw_fd(fd))
157    }
158}
159
160impl From<OwnedFd> for UnixStream {
161    #[inline]
162    fn from(fd: OwnedFd) -> Self {
163        Self::from_std(unix::net::UnixStream::from(fd))
164    }
165}
166
167impl AsRawFd for UnixStream {
168    #[inline]
169    fn as_raw_fd(&self) -> RawFd {
170        self.std.as_raw_fd()
171    }
172}
173
174impl AsFd for UnixStream {
175    #[inline]
176    fn as_fd(&self) -> BorrowedFd<'_> {
177        self.std.as_fd()
178    }
179}
180
181impl IntoRawFd for UnixStream {
182    #[inline]
183    fn into_raw_fd(self) -> RawFd {
184        self.std.into_raw_fd()
185    }
186}
187
188impl From<UnixStream> for OwnedFd {
189    #[inline]
190    fn from(stream: UnixStream) -> OwnedFd {
191        stream.std.into()
192    }
193}
194
195impl Read for UnixStream {
196    #[inline]
197    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
198        self.std.read(buf)
199    }
200
201    #[inline]
202    fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> io::Result<usize> {
203        self.std.read_vectored(bufs)
204    }
205
206    #[inline]
207    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
208        self.std.read_exact(buf)
209    }
210
211    #[inline]
212    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
213        self.std.read_to_end(buf)
214    }
215
216    #[inline]
217    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
218        self.std.read_to_string(buf)
219    }
220
221    #[cfg(can_vector)]
222    #[inline]
223    fn is_read_vectored(&self) -> bool {
224        self.std.is_read_vectored()
225    }
226}
227
228impl Read for &UnixStream {
229    #[inline]
230    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
231        (&mut &self.std).read(buf)
232    }
233
234    #[inline]
235    fn read_vectored(&mut self, bufs: &mut [IoSliceMut]) -> io::Result<usize> {
236        (&mut &self.std).read_vectored(bufs)
237    }
238
239    #[inline]
240    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
241        (&mut &self.std).read_exact(buf)
242    }
243
244    #[inline]
245    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
246        (&mut &self.std).read_to_end(buf)
247    }
248
249    #[inline]
250    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
251        (&mut &self.std).read_to_string(buf)
252    }
253
254    #[cfg(can_vector)]
255    #[inline]
256    fn is_read_vectored(&self) -> bool {
257        self.std.is_read_vectored()
258    }
259}
260
261impl Write for UnixStream {
262    #[inline]
263    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
264        self.std.write(buf)
265    }
266
267    #[inline]
268    fn flush(&mut self) -> io::Result<()> {
269        self.std.flush()
270    }
271
272    #[inline]
273    fn write_vectored(&mut self, bufs: &[IoSlice]) -> io::Result<usize> {
274        self.std.write_vectored(bufs)
275    }
276
277    #[inline]
278    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
279        self.std.write_all(buf)
280    }
281
282    #[cfg(can_vector)]
283    #[inline]
284    fn is_write_vectored(&self) -> bool {
285        self.std.is_write_vectored()
286    }
287
288    #[cfg(write_all_vectored)]
289    #[inline]
290    fn write_all_vectored(&mut self, bufs: &mut [IoSlice]) -> io::Result<()> {
291        self.std.write_all_vectored(bufs)
292    }
293}
294
295impl Write for &UnixStream {
296    #[inline]
297    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
298        (&mut &self.std).write(buf)
299    }
300
301    #[inline]
302    fn flush(&mut self) -> io::Result<()> {
303        (&mut &self.std).flush()
304    }
305
306    #[inline]
307    fn write_vectored(&mut self, bufs: &[IoSlice]) -> io::Result<usize> {
308        (&mut &self.std).write_vectored(bufs)
309    }
310
311    #[inline]
312    fn write_all(&mut self, buf: &[u8]) -> io::Result<()> {
313        (&mut &self.std).write_all(buf)
314    }
315
316    #[cfg(can_vector)]
317    #[inline]
318    fn is_write_vectored(&self) -> bool {
319        self.std.is_write_vectored()
320    }
321
322    #[cfg(write_all_vectored)]
323    #[inline]
324    fn write_all_vectored(&mut self, bufs: &mut [IoSlice]) -> io::Result<()> {
325        (&mut &self.std).write_all_vectored(bufs)
326    }
327}
328
329impl fmt::Debug for UnixStream {
330    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
331        self.std.fmt(f)
332    }
333}