diff --git a/tokio/src/macros/cfg.rs b/tokio/src/macros/cfg.rs index 443cc14b3..5f0c66388 100644 --- a/tokio/src/macros/cfg.rs +++ b/tokio/src/macros/cfg.rs @@ -25,6 +25,18 @@ macro_rules! cfg_windows { } } +/// Enables Unix-specific code. +/// Use this macro instead of `cfg(unix)` to generate docs properly. +macro_rules! cfg_unix { + ($($item:item)*) => { + $( + #[cfg(any(all(doc, docsrs), unix))] + #[cfg_attr(docsrs, doc(cfg(unix)))] + $item + )* + } +} + /// Enables unstable Windows-specific code. /// Use this macro instead of `cfg(windows)` to generate docs properly. macro_rules! cfg_unstable_windows { diff --git a/tokio/src/net/tcp/socket.rs b/tokio/src/net/tcp/socket.rs index aa9639a64..679e95866 100644 --- a/tokio/src/net/tcp/socket.rs +++ b/tokio/src/net/tcp/socket.rs @@ -777,38 +777,36 @@ impl fmt::Debug for TcpSocket { } } -#[cfg(unix)] -impl AsRawFd for TcpSocket { - fn as_raw_fd(&self) -> RawFd { - self.inner.as_raw_fd() +cfg_unix! { + impl AsRawFd for TcpSocket { + fn as_raw_fd(&self) -> RawFd { + self.inner.as_raw_fd() + } } -} -#[cfg(unix)] -impl AsFd for TcpSocket { - fn as_fd(&self) -> BorrowedFd<'_> { - unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + impl AsFd for TcpSocket { + fn as_fd(&self) -> BorrowedFd<'_> { + unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) } + } } -} -#[cfg(unix)] -impl FromRawFd for TcpSocket { - /// Converts a `RawFd` to a `TcpSocket`. - /// - /// # Notes - /// - /// The caller is responsible for ensuring that the socket is in - /// non-blocking mode. - unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket { - let inner = socket2::Socket::from_raw_fd(fd); - TcpSocket { inner } + impl FromRawFd for TcpSocket { + /// Converts a `RawFd` to a `TcpSocket`. + /// + /// # Notes + /// + /// The caller is responsible for ensuring that the socket is in + /// non-blocking mode. + unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket { + let inner = socket2::Socket::from_raw_fd(fd); + TcpSocket { inner } + } } -} -#[cfg(unix)] -impl IntoRawFd for TcpSocket { - fn into_raw_fd(self) -> RawFd { - self.inner.into_raw_fd() + impl IntoRawFd for TcpSocket { + fn into_raw_fd(self) -> RawFd { + self.inner.into_raw_fd() + } } }