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