From be483ff4c10e8bea2a4bac4849063ad5686a737b Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 21 Aug 2021 19:42:30 -0700 Subject: [PATCH 1/2] Implement `AsFd` etc. for `UnixListener`. Implement `AsFd`, `From`, and `Into` for `UnixListener`. This is a follow-up to #87329. --- library/std/src/os/unix/net/listener.rs | 26 ++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/library/std/src/os/unix/net/listener.rs b/library/std/src/os/unix/net/listener.rs index 9066c71794fad..4508367b9ee7c 100644 --- a/library/std/src/os/unix/net/listener.rs +++ b/library/std/src/os/unix/net/listener.rs @@ -1,5 +1,5 @@ use super::{sockaddr_un, SocketAddr, UnixStream}; -use crate::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; +use crate::os::unix::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; use crate::path::Path; use crate::sys::cvt; use crate::sys::net::Socket; @@ -262,6 +262,30 @@ impl IntoRawFd for UnixListener { } } +#[unstable(feature = "io_safety", issue = "87074")] +impl AsFd for UnixListener { + #[inline] + fn as_fd(&self) -> BorrowedFd<'_> { + self.0.as_inner().as_fd() + } +} + +#[unstable(feature = "io_safety", issue = "87074")] +impl From for UnixListener { + #[inline] + fn from(fd: OwnedFd) -> UnixListener { + UnixListener(Socket::from_inner(FromInner::from_inner(OwnedFd::from(fd)))) + } +} + +#[unstable(feature = "io_safety", issue = "87074")] +impl From for OwnedFd { + #[inline] + fn from(listener: UnixListener) -> OwnedFd { + listener.0.into_inner().into_inner().into() + } +} + #[stable(feature = "unix_socket", since = "1.10.0")] impl<'a> IntoIterator for &'a UnixListener { type Item = io::Result; From a0ce5f25fab82f52eb65248dbf452f6d7a485d24 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sun, 22 Aug 2021 16:51:30 -0700 Subject: [PATCH 2/2] Remove redundant conversions. --- library/std/src/os/unix/net/listener.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/os/unix/net/listener.rs b/library/std/src/os/unix/net/listener.rs index 4508367b9ee7c..f08bd252e46fa 100644 --- a/library/std/src/os/unix/net/listener.rs +++ b/library/std/src/os/unix/net/listener.rs @@ -274,7 +274,7 @@ impl AsFd for UnixListener { impl From for UnixListener { #[inline] fn from(fd: OwnedFd) -> UnixListener { - UnixListener(Socket::from_inner(FromInner::from_inner(OwnedFd::from(fd)))) + UnixListener(Socket::from_inner(FromInner::from_inner(fd))) } } @@ -282,7 +282,7 @@ impl From for UnixListener { impl From for OwnedFd { #[inline] fn from(listener: UnixListener) -> OwnedFd { - listener.0.into_inner().into_inner().into() + listener.0.into_inner().into_inner() } }