std/os/unix/net/stream.rs
1use super::{SocketAddr, sockaddr_un};
2#[cfg(any(doc, target_os = "android", target_os = "linux"))]
3use super::{SocketAncillary, recv_vectored_with_ancillary_from, send_vectored_with_ancillary_to};
4#[cfg(any(
5 target_os = "android",
6 target_os = "linux",
7 target_os = "dragonfly",
8 target_os = "freebsd",
9 target_os = "netbsd",
10 target_os = "openbsd",
11 target_os = "nto",
12 target_vendor = "apple",
13 target_os = "cygwin"
14))]
15use super::{UCred, peer_cred};
16use crate::fmt;
17use crate::io::{self, IoSlice, IoSliceMut};
18use crate::net::Shutdown;
19use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
20use crate::path::Path;
21use crate::sealed::Sealed;
22use crate::sys::cvt;
23use crate::sys::net::Socket;
24use crate::sys_common::{AsInner, FromInner};
25use crate::time::Duration;
26
27/// A Unix stream socket.
28///
29/// # Examples
30///
31/// ```no_run
32/// use std::os::unix::net::UnixStream;
33/// use std::io::prelude::*;
34///
35/// fn main() -> std::io::Result<()> {
36/// let mut stream = UnixStream::connect("/path/to/my/socket")?;
37/// stream.write_all(b"hello world")?;
38/// let mut response = String::new();
39/// stream.read_to_string(&mut response)?;
40/// println!("{response}");
41/// Ok(())
42/// }
43/// ```
44#[stable(feature = "unix_socket", since = "1.10.0")]
45pub struct UnixStream(pub(super) Socket);
46
47/// Allows extension traits within `std`.
48#[unstable(feature = "sealed", issue = "none")]
49impl Sealed for UnixStream {}
50
51#[stable(feature = "unix_socket", since = "1.10.0")]
52impl fmt::Debug for UnixStream {
53 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
54 let mut builder = fmt.debug_struct("UnixStream");
55 builder.field("fd", self.0.as_inner());
56 if let Ok(addr) = self.local_addr() {
57 builder.field("local", &addr);
58 }
59 if let Ok(addr) = self.peer_addr() {
60 builder.field("peer", &addr);
61 }
62 builder.finish()
63 }
64}
65
66impl UnixStream {
67 /// Connects to the socket named by `path`.
68 ///
69 /// # Examples
70 ///
71 /// ```no_run
72 /// use std::os::unix::net::UnixStream;
73 ///
74 /// let socket = match UnixStream::connect("/tmp/sock") {
75 /// Ok(sock) => sock,
76 /// Err(e) => {
77 /// println!("Couldn't connect: {e:?}");
78 /// return
79 /// }
80 /// };
81 /// ```
82 #[stable(feature = "unix_socket", since = "1.10.0")]
83 pub fn connect<P: AsRef<Path>>(path: P) -> io::Result<UnixStream> {
84 unsafe {
85 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
86 let (addr, len) = sockaddr_un(path.as_ref())?;
87
88 cvt(libc::connect(inner.as_raw_fd(), (&raw const addr) as *const _, len))?;
89 Ok(UnixStream(inner))
90 }
91 }
92
93 /// Connects to the socket specified by [`address`].
94 ///
95 /// [`address`]: crate::os::unix::net::SocketAddr
96 ///
97 /// # Examples
98 ///
99 /// ```no_run
100 /// use std::os::unix::net::{UnixListener, UnixStream};
101 ///
102 /// fn main() -> std::io::Result<()> {
103 /// let listener = UnixListener::bind("/path/to/the/socket")?;
104 /// let addr = listener.local_addr()?;
105 ///
106 /// let sock = match UnixStream::connect_addr(&addr) {
107 /// Ok(sock) => sock,
108 /// Err(e) => {
109 /// println!("Couldn't connect: {e:?}");
110 /// return Err(e)
111 /// }
112 /// };
113 /// Ok(())
114 /// }
115 /// ````
116 #[stable(feature = "unix_socket_abstract", since = "1.70.0")]
117 pub fn connect_addr(socket_addr: &SocketAddr) -> io::Result<UnixStream> {
118 unsafe {
119 let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
120 cvt(libc::connect(
121 inner.as_raw_fd(),
122 (&raw const socket_addr.addr) as *const _,
123 socket_addr.len,
124 ))?;
125 Ok(UnixStream(inner))
126 }
127 }
128
129 /// Creates an unnamed pair of connected sockets.
130 ///
131 /// Returns two `UnixStream`s which are connected to each other.
132 ///
133 /// # Examples
134 ///
135 /// ```no_run
136 /// use std::os::unix::net::UnixStream;
137 ///
138 /// let (sock1, sock2) = match UnixStream::pair() {
139 /// Ok((sock1, sock2)) => (sock1, sock2),
140 /// Err(e) => {
141 /// println!("Couldn't create a pair of sockets: {e:?}");
142 /// return
143 /// }
144 /// };
145 /// ```
146 #[stable(feature = "unix_socket", since = "1.10.0")]
147 pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
148 let (i1, i2) = Socket::new_pair(libc::AF_UNIX, libc::SOCK_STREAM)?;
149 Ok((UnixStream(i1), UnixStream(i2)))
150 }
151
152 /// Creates a new independently owned handle to the underlying socket.
153 ///
154 /// The returned `UnixStream` is a reference to the same stream that this
155 /// object references. Both handles will read and write the same stream of
156 /// data, and options set on one stream will be propagated to the other
157 /// stream.
158 ///
159 /// # Examples
160 ///
161 /// ```no_run
162 /// use std::os::unix::net::UnixStream;
163 ///
164 /// fn main() -> std::io::Result<()> {
165 /// let socket = UnixStream::connect("/tmp/sock")?;
166 /// let sock_copy = socket.try_clone().expect("Couldn't clone socket");
167 /// Ok(())
168 /// }
169 /// ```
170 #[stable(feature = "unix_socket", since = "1.10.0")]
171 pub fn try_clone(&self) -> io::Result<UnixStream> {
172 self.0.duplicate().map(UnixStream)
173 }
174
175 /// Returns the socket address of the local half of this connection.
176 ///
177 /// # Examples
178 ///
179 /// ```no_run
180 /// use std::os::unix::net::UnixStream;
181 ///
182 /// fn main() -> std::io::Result<()> {
183 /// let socket = UnixStream::connect("/tmp/sock")?;
184 /// let addr = socket.local_addr().expect("Couldn't get local address");
185 /// Ok(())
186 /// }
187 /// ```
188 #[stable(feature = "unix_socket", since = "1.10.0")]
189 pub fn local_addr(&self) -> io::Result<SocketAddr> {
190 SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
191 }
192
193 /// Returns the socket address of the remote half of this connection.
194 ///
195 /// # Examples
196 ///
197 /// ```no_run
198 /// use std::os::unix::net::UnixStream;
199 ///
200 /// fn main() -> std::io::Result<()> {
201 /// let socket = UnixStream::connect("/tmp/sock")?;
202 /// let addr = socket.peer_addr().expect("Couldn't get peer address");
203 /// Ok(())
204 /// }
205 /// ```
206 #[stable(feature = "unix_socket", since = "1.10.0")]
207 pub fn peer_addr(&self) -> io::Result<SocketAddr> {
208 SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
209 }
210
211 /// Gets the peer credentials for this Unix domain socket.
212 ///
213 /// # Examples
214 ///
215 /// ```no_run
216 /// #![feature(peer_credentials_unix_socket)]
217 /// use std::os::unix::net::UnixStream;
218 ///
219 /// fn main() -> std::io::Result<()> {
220 /// let socket = UnixStream::connect("/tmp/sock")?;
221 /// let peer_cred = socket.peer_cred().expect("Couldn't get peer credentials");
222 /// Ok(())
223 /// }
224 /// ```
225 #[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
226 #[cfg(any(
227 target_os = "android",
228 target_os = "linux",
229 target_os = "dragonfly",
230 target_os = "freebsd",
231 target_os = "netbsd",
232 target_os = "openbsd",
233 target_os = "nto",
234 target_vendor = "apple",
235 target_os = "cygwin"
236 ))]
237 pub fn peer_cred(&self) -> io::Result<UCred> {
238 peer_cred(self)
239 }
240
241 /// Sets the read timeout for the socket.
242 ///
243 /// If the provided value is [`None`], then [`read`] calls will block
244 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is passed to this
245 /// method.
246 ///
247 /// [`read`]: io::Read::read
248 ///
249 /// # Examples
250 ///
251 /// ```no_run
252 /// use std::os::unix::net::UnixStream;
253 /// use std::time::Duration;
254 ///
255 /// fn main() -> std::io::Result<()> {
256 /// let socket = UnixStream::connect("/tmp/sock")?;
257 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
258 /// Ok(())
259 /// }
260 /// ```
261 ///
262 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
263 /// method:
264 ///
265 /// ```no_run
266 /// use std::io;
267 /// use std::os::unix::net::UnixStream;
268 /// use std::time::Duration;
269 ///
270 /// fn main() -> std::io::Result<()> {
271 /// let socket = UnixStream::connect("/tmp/sock")?;
272 /// let result = socket.set_read_timeout(Some(Duration::new(0, 0)));
273 /// let err = result.unwrap_err();
274 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
275 /// Ok(())
276 /// }
277 /// ```
278 #[stable(feature = "unix_socket", since = "1.10.0")]
279 pub fn set_read_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
280 self.0.set_timeout(timeout, libc::SO_RCVTIMEO)
281 }
282
283 /// Sets the write timeout for the socket.
284 ///
285 /// If the provided value is [`None`], then [`write`] calls will block
286 /// indefinitely. An [`Err`] is returned if the zero [`Duration`] is
287 /// passed to this method.
288 ///
289 /// [`read`]: io::Read::read
290 ///
291 /// # Examples
292 ///
293 /// ```no_run
294 /// use std::os::unix::net::UnixStream;
295 /// use std::time::Duration;
296 ///
297 /// fn main() -> std::io::Result<()> {
298 /// let socket = UnixStream::connect("/tmp/sock")?;
299 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
300 /// .expect("Couldn't set write timeout");
301 /// Ok(())
302 /// }
303 /// ```
304 ///
305 /// An [`Err`] is returned if the zero [`Duration`] is passed to this
306 /// method:
307 ///
308 /// ```no_run
309 /// use std::io;
310 /// use std::net::UdpSocket;
311 /// use std::time::Duration;
312 ///
313 /// fn main() -> std::io::Result<()> {
314 /// let socket = UdpSocket::bind("127.0.0.1:34254")?;
315 /// let result = socket.set_write_timeout(Some(Duration::new(0, 0)));
316 /// let err = result.unwrap_err();
317 /// assert_eq!(err.kind(), io::ErrorKind::InvalidInput);
318 /// Ok(())
319 /// }
320 /// ```
321 #[stable(feature = "unix_socket", since = "1.10.0")]
322 pub fn set_write_timeout(&self, timeout: Option<Duration>) -> io::Result<()> {
323 self.0.set_timeout(timeout, libc::SO_SNDTIMEO)
324 }
325
326 /// Returns the read timeout of this socket.
327 ///
328 /// # Examples
329 ///
330 /// ```no_run
331 /// use std::os::unix::net::UnixStream;
332 /// use std::time::Duration;
333 ///
334 /// fn main() -> std::io::Result<()> {
335 /// let socket = UnixStream::connect("/tmp/sock")?;
336 /// socket.set_read_timeout(Some(Duration::new(1, 0))).expect("Couldn't set read timeout");
337 /// assert_eq!(socket.read_timeout()?, Some(Duration::new(1, 0)));
338 /// Ok(())
339 /// }
340 /// ```
341 #[stable(feature = "unix_socket", since = "1.10.0")]
342 pub fn read_timeout(&self) -> io::Result<Option<Duration>> {
343 self.0.timeout(libc::SO_RCVTIMEO)
344 }
345
346 /// Returns the write timeout of this socket.
347 ///
348 /// # Examples
349 ///
350 /// ```no_run
351 /// use std::os::unix::net::UnixStream;
352 /// use std::time::Duration;
353 ///
354 /// fn main() -> std::io::Result<()> {
355 /// let socket = UnixStream::connect("/tmp/sock")?;
356 /// socket.set_write_timeout(Some(Duration::new(1, 0)))
357 /// .expect("Couldn't set write timeout");
358 /// assert_eq!(socket.write_timeout()?, Some(Duration::new(1, 0)));
359 /// Ok(())
360 /// }
361 /// ```
362 #[stable(feature = "unix_socket", since = "1.10.0")]
363 pub fn write_timeout(&self) -> io::Result<Option<Duration>> {
364 self.0.timeout(libc::SO_SNDTIMEO)
365 }
366
367 /// Moves the socket into or out of nonblocking mode.
368 ///
369 /// # Examples
370 ///
371 /// ```no_run
372 /// use std::os::unix::net::UnixStream;
373 ///
374 /// fn main() -> std::io::Result<()> {
375 /// let socket = UnixStream::connect("/tmp/sock")?;
376 /// socket.set_nonblocking(true).expect("Couldn't set nonblocking");
377 /// Ok(())
378 /// }
379 /// ```
380 #[stable(feature = "unix_socket", since = "1.10.0")]
381 pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
382 self.0.set_nonblocking(nonblocking)
383 }
384
385 /// Set the id of the socket for network filtering purpose
386 ///
387 #[cfg_attr(
388 any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"),
389 doc = "```no_run"
390 )]
391 #[cfg_attr(
392 not(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd")),
393 doc = "```ignore"
394 )]
395 /// #![feature(unix_set_mark)]
396 /// use std::os::unix::net::UnixStream;
397 ///
398 /// fn main() -> std::io::Result<()> {
399 /// let sock = UnixStream::connect("/tmp/sock")?;
400 /// sock.set_mark(32)?;
401 /// Ok(())
402 /// }
403 /// ```
404 #[cfg(any(doc, target_os = "linux", target_os = "freebsd", target_os = "openbsd",))]
405 #[unstable(feature = "unix_set_mark", issue = "96467")]
406 pub fn set_mark(&self, mark: u32) -> io::Result<()> {
407 self.0.set_mark(mark)
408 }
409
410 /// Returns the value of the `SO_ERROR` option.
411 ///
412 /// # Examples
413 ///
414 /// ```no_run
415 /// use std::os::unix::net::UnixStream;
416 ///
417 /// fn main() -> std::io::Result<()> {
418 /// let socket = UnixStream::connect("/tmp/sock")?;
419 /// if let Ok(Some(err)) = socket.take_error() {
420 /// println!("Got error: {err:?}");
421 /// }
422 /// Ok(())
423 /// }
424 /// ```
425 ///
426 /// # Platform specific
427 /// On Redox this always returns `None`.
428 #[stable(feature = "unix_socket", since = "1.10.0")]
429 pub fn take_error(&self) -> io::Result<Option<io::Error>> {
430 self.0.take_error()
431 }
432
433 /// Shuts down the read, write, or both halves of this connection.
434 ///
435 /// This function will cause all pending and future I/O calls on the
436 /// specified portions to immediately return with an appropriate value
437 /// (see the documentation of [`Shutdown`]).
438 ///
439 /// # Examples
440 ///
441 /// ```no_run
442 /// use std::os::unix::net::UnixStream;
443 /// use std::net::Shutdown;
444 ///
445 /// fn main() -> std::io::Result<()> {
446 /// let socket = UnixStream::connect("/tmp/sock")?;
447 /// socket.shutdown(Shutdown::Both).expect("shutdown function failed");
448 /// Ok(())
449 /// }
450 /// ```
451 #[stable(feature = "unix_socket", since = "1.10.0")]
452 pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
453 self.0.shutdown(how)
454 }
455
456 /// Receives data on the socket from the remote address to which it is
457 /// connected, without removing that data from the queue. On success,
458 /// returns the number of bytes peeked.
459 ///
460 /// Successive calls return the same data. This is accomplished by passing
461 /// `MSG_PEEK` as a flag to the underlying `recv` system call.
462 ///
463 /// # Examples
464 ///
465 /// ```no_run
466 /// #![feature(unix_socket_peek)]
467 ///
468 /// use std::os::unix::net::UnixStream;
469 ///
470 /// fn main() -> std::io::Result<()> {
471 /// let socket = UnixStream::connect("/tmp/sock")?;
472 /// let mut buf = [0; 10];
473 /// let len = socket.peek(&mut buf).expect("peek failed");
474 /// Ok(())
475 /// }
476 /// ```
477 #[unstable(feature = "unix_socket_peek", issue = "76923")]
478 pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
479 self.0.peek(buf)
480 }
481
482 /// Receives data and ancillary data from socket.
483 ///
484 /// On success, returns the number of bytes read.
485 ///
486 /// # Examples
487 ///
488 #[cfg_attr(any(target_os = "android", target_os = "linux"), doc = "```no_run")]
489 #[cfg_attr(not(any(target_os = "android", target_os = "linux")), doc = "```ignore")]
490 /// #![feature(unix_socket_ancillary_data)]
491 /// use std::os::unix::net::{UnixStream, SocketAncillary, AncillaryData};
492 /// use std::io::IoSliceMut;
493 ///
494 /// fn main() -> std::io::Result<()> {
495 /// let socket = UnixStream::connect("/tmp/sock")?;
496 /// let mut buf1 = [1; 8];
497 /// let mut buf2 = [2; 16];
498 /// let mut buf3 = [3; 8];
499 /// let mut bufs = &mut [
500 /// IoSliceMut::new(&mut buf1),
501 /// IoSliceMut::new(&mut buf2),
502 /// IoSliceMut::new(&mut buf3),
503 /// ][..];
504 /// let mut fds = [0; 8];
505 /// let mut ancillary_buffer = [0; 128];
506 /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
507 /// let size = socket.recv_vectored_with_ancillary(bufs, &mut ancillary)?;
508 /// println!("received {size}");
509 /// for ancillary_result in ancillary.messages() {
510 /// if let AncillaryData::ScmRights(scm_rights) = ancillary_result.unwrap() {
511 /// for fd in scm_rights {
512 /// println!("receive file descriptor: {fd}");
513 /// }
514 /// }
515 /// }
516 /// Ok(())
517 /// }
518 /// ```
519 #[cfg(any(doc, target_os = "android", target_os = "linux"))]
520 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
521 pub fn recv_vectored_with_ancillary(
522 &self,
523 bufs: &mut [IoSliceMut<'_>],
524 ancillary: &mut SocketAncillary<'_>,
525 ) -> io::Result<usize> {
526 let (count, _, _) = recv_vectored_with_ancillary_from(&self.0, bufs, ancillary)?;
527
528 Ok(count)
529 }
530
531 /// Sends data and ancillary data on the socket.
532 ///
533 /// On success, returns the number of bytes written.
534 ///
535 /// # Examples
536 ///
537 #[cfg_attr(any(target_os = "android", target_os = "linux"), doc = "```no_run")]
538 #[cfg_attr(not(any(target_os = "android", target_os = "linux")), doc = "```ignore")]
539 /// #![feature(unix_socket_ancillary_data)]
540 /// use std::os::unix::net::{UnixStream, SocketAncillary};
541 /// use std::io::IoSlice;
542 ///
543 /// fn main() -> std::io::Result<()> {
544 /// let socket = UnixStream::connect("/tmp/sock")?;
545 /// let buf1 = [1; 8];
546 /// let buf2 = [2; 16];
547 /// let buf3 = [3; 8];
548 /// let bufs = &[
549 /// IoSlice::new(&buf1),
550 /// IoSlice::new(&buf2),
551 /// IoSlice::new(&buf3),
552 /// ][..];
553 /// let fds = [0, 1, 2];
554 /// let mut ancillary_buffer = [0; 128];
555 /// let mut ancillary = SocketAncillary::new(&mut ancillary_buffer[..]);
556 /// ancillary.add_fds(&fds[..]);
557 /// socket.send_vectored_with_ancillary(bufs, &mut ancillary)
558 /// .expect("send_vectored_with_ancillary function failed");
559 /// Ok(())
560 /// }
561 /// ```
562 #[cfg(any(doc, target_os = "android", target_os = "linux"))]
563 #[unstable(feature = "unix_socket_ancillary_data", issue = "76915")]
564 pub fn send_vectored_with_ancillary(
565 &self,
566 bufs: &[IoSlice<'_>],
567 ancillary: &mut SocketAncillary<'_>,
568 ) -> io::Result<usize> {
569 send_vectored_with_ancillary_to(&self.0, None, bufs, ancillary)
570 }
571}
572
573#[stable(feature = "unix_socket", since = "1.10.0")]
574impl io::Read for UnixStream {
575 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
576 io::Read::read(&mut &*self, buf)
577 }
578
579 fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
580 io::Read::read_buf(&mut &*self, buf)
581 }
582
583 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
584 io::Read::read_vectored(&mut &*self, bufs)
585 }
586
587 #[inline]
588 fn is_read_vectored(&self) -> bool {
589 io::Read::is_read_vectored(&&*self)
590 }
591}
592
593#[stable(feature = "unix_socket", since = "1.10.0")]
594impl<'a> io::Read for &'a UnixStream {
595 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
596 self.0.read(buf)
597 }
598
599 fn read_buf(&mut self, buf: io::BorrowedCursor<'_>) -> io::Result<()> {
600 self.0.read_buf(buf)
601 }
602
603 fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
604 self.0.read_vectored(bufs)
605 }
606
607 #[inline]
608 fn is_read_vectored(&self) -> bool {
609 self.0.is_read_vectored()
610 }
611}
612
613#[stable(feature = "unix_socket", since = "1.10.0")]
614impl io::Write for UnixStream {
615 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
616 io::Write::write(&mut &*self, buf)
617 }
618
619 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
620 io::Write::write_vectored(&mut &*self, bufs)
621 }
622
623 #[inline]
624 fn is_write_vectored(&self) -> bool {
625 io::Write::is_write_vectored(&&*self)
626 }
627
628 fn flush(&mut self) -> io::Result<()> {
629 io::Write::flush(&mut &*self)
630 }
631}
632
633#[stable(feature = "unix_socket", since = "1.10.0")]
634impl<'a> io::Write for &'a UnixStream {
635 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
636 self.0.write(buf)
637 }
638
639 fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
640 self.0.write_vectored(bufs)
641 }
642
643 #[inline]
644 fn is_write_vectored(&self) -> bool {
645 self.0.is_write_vectored()
646 }
647
648 #[inline]
649 fn flush(&mut self) -> io::Result<()> {
650 Ok(())
651 }
652}
653
654#[stable(feature = "unix_socket", since = "1.10.0")]
655impl AsRawFd for UnixStream {
656 #[inline]
657 fn as_raw_fd(&self) -> RawFd {
658 self.0.as_raw_fd()
659 }
660}
661
662#[stable(feature = "unix_socket", since = "1.10.0")]
663impl FromRawFd for UnixStream {
664 #[inline]
665 unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
666 UnixStream(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
667 }
668}
669
670#[stable(feature = "unix_socket", since = "1.10.0")]
671impl IntoRawFd for UnixStream {
672 #[inline]
673 fn into_raw_fd(self) -> RawFd {
674 self.0.into_raw_fd()
675 }
676}
677
678#[stable(feature = "io_safety", since = "1.63.0")]
679impl AsFd for UnixStream {
680 #[inline]
681 fn as_fd(&self) -> BorrowedFd<'_> {
682 self.0.as_fd()
683 }
684}
685
686#[stable(feature = "io_safety", since = "1.63.0")]
687impl From<UnixStream> for OwnedFd {
688 /// Takes ownership of a [`UnixStream`]'s socket file descriptor.
689 #[inline]
690 fn from(unix_stream: UnixStream) -> OwnedFd {
691 unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) }
692 }
693}
694
695#[stable(feature = "io_safety", since = "1.63.0")]
696impl From<OwnedFd> for UnixStream {
697 #[inline]
698 fn from(owned: OwnedFd) -> Self {
699 unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
700 }
701}
702
703impl AsInner<Socket> for UnixStream {
704 #[inline]
705 fn as_inner(&self) -> &Socket {
706 &self.0
707 }
708}