use crate::fmt;
use crate::io::{self, Initializer, IoSlice, IoSliceMut};
use crate::net::Shutdown;
-use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
+use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
#[cfg(any(
target_os = "android",
target_os = "linux",
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
+ target_os = "netbsd",
target_os = "openbsd"
))]
use crate::os::unix::ucred;
use crate::path::Path;
use crate::sys::cvt;
use crate::sys::net::Socket;
-use crate::sys_common::{AsInner, FromInner, IntoInner};
+use crate::sys_common::{AsInner, FromInner};
use crate::time::Duration;
#[unstable(feature = "peer_credentials_unix_socket", issue = "42839", reason = "unstable")]
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
+ target_os = "netbsd",
target_os = "openbsd"
))]
pub use ucred::UCred;
let inner = Socket::new_raw(libc::AF_UNIX, libc::SOCK_STREAM)?;
let (addr, len) = sockaddr_un(path.as_ref())?;
- cvt(libc::connect(*inner.as_inner(), &addr as *const _ as *const _, len))?;
+ cvt(libc::connect(inner.as_raw_fd(), &addr as *const _ as *const _, len))?;
Ok(UnixStream(inner))
}
}
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn local_addr(&self) -> io::Result<SocketAddr> {
- SocketAddr::new(|addr, len| unsafe { libc::getsockname(*self.0.as_inner(), addr, len) })
+ SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.as_raw_fd(), addr, len) })
}
/// Returns the socket address of the remote half of this connection.
/// ```
#[stable(feature = "unix_socket", since = "1.10.0")]
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
- SocketAddr::new(|addr, len| unsafe { libc::getpeername(*self.0.as_inner(), addr, len) })
+ SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.as_raw_fd(), addr, len) })
}
/// Gets the peer credentials for this Unix domain socket.
target_os = "freebsd",
target_os = "ios",
target_os = "macos",
+ target_os = "netbsd",
target_os = "openbsd"
))]
pub fn peer_cred(&self) -> io::Result<UCred> {
impl AsRawFd for UnixStream {
#[inline]
fn as_raw_fd(&self) -> RawFd {
- *self.0.as_inner()
+ self.0.as_raw_fd()
}
}
impl FromRawFd for UnixStream {
#[inline]
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
- UnixStream(Socket::from_inner(fd))
+ UnixStream(Socket::from_inner(FromInner::from_inner(OwnedFd::from_raw_fd(fd))))
}
}
impl IntoRawFd for UnixStream {
#[inline]
fn into_raw_fd(self) -> RawFd {
- self.0.into_inner()
+ self.0.into_raw_fd()
+ }
+}
+
+#[unstable(feature = "io_safety", issue = "87074")]
+impl AsFd for UnixStream {
+ #[inline]
+ fn as_fd(&self) -> BorrowedFd<'_> {
+ self.0.as_fd()
+ }
+}
+
+#[unstable(feature = "io_safety", issue = "87074")]
+impl From<UnixStream> for OwnedFd {
+ #[inline]
+ fn from(unix_stream: UnixStream) -> OwnedFd {
+ unsafe { OwnedFd::from_raw_fd(unix_stream.into_raw_fd()) }
+ }
+}
+
+#[unstable(feature = "io_safety", issue = "87074")]
+impl From<OwnedFd> for UnixStream {
+ #[inline]
+ fn from(owned: OwnedFd) -> Self {
+ unsafe { Self::from_raw_fd(owned.into_raw_fd()) }
}
}