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