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