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