Skip to content

Commit eab26a6

Browse files
authored
net: document that *Fd traits on TcpSocket are unix-only (#6294)
1 parent 58edfc6 commit eab26a6

File tree

2 files changed

+36
-26
lines changed

2 files changed

+36
-26
lines changed

tokio/src/macros/cfg.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ macro_rules! cfg_windows {
2525
}
2626
}
2727

28+
/// Enables Unix-specific code.
29+
/// Use this macro instead of `cfg(unix)` to generate docs properly.
30+
macro_rules! cfg_unix {
31+
($($item:item)*) => {
32+
$(
33+
#[cfg(any(all(doc, docsrs), unix))]
34+
#[cfg_attr(docsrs, doc(cfg(unix)))]
35+
$item
36+
)*
37+
}
38+
}
39+
2840
/// Enables unstable Windows-specific code.
2941
/// Use this macro instead of `cfg(windows)` to generate docs properly.
3042
macro_rules! cfg_unstable_windows {

tokio/src/net/tcp/socket.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -777,38 +777,36 @@ impl fmt::Debug for TcpSocket {
777777
}
778778
}
779779

780-
#[cfg(unix)]
781-
impl AsRawFd for TcpSocket {
782-
fn as_raw_fd(&self) -> RawFd {
783-
self.inner.as_raw_fd()
780+
cfg_unix! {
781+
impl AsRawFd for TcpSocket {
782+
fn as_raw_fd(&self) -> RawFd {
783+
self.inner.as_raw_fd()
784+
}
784785
}
785-
}
786786

787-
#[cfg(unix)]
788-
impl AsFd for TcpSocket {
789-
fn as_fd(&self) -> BorrowedFd<'_> {
790-
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
787+
impl AsFd for TcpSocket {
788+
fn as_fd(&self) -> BorrowedFd<'_> {
789+
unsafe { BorrowedFd::borrow_raw(self.as_raw_fd()) }
790+
}
791791
}
792-
}
793792

794-
#[cfg(unix)]
795-
impl FromRawFd for TcpSocket {
796-
/// Converts a `RawFd` to a `TcpSocket`.
797-
///
798-
/// # Notes
799-
///
800-
/// The caller is responsible for ensuring that the socket is in
801-
/// non-blocking mode.
802-
unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket {
803-
let inner = socket2::Socket::from_raw_fd(fd);
804-
TcpSocket { inner }
793+
impl FromRawFd for TcpSocket {
794+
/// Converts a `RawFd` to a `TcpSocket`.
795+
///
796+
/// # Notes
797+
///
798+
/// The caller is responsible for ensuring that the socket is in
799+
/// non-blocking mode.
800+
unsafe fn from_raw_fd(fd: RawFd) -> TcpSocket {
801+
let inner = socket2::Socket::from_raw_fd(fd);
802+
TcpSocket { inner }
803+
}
805804
}
806-
}
807805

808-
#[cfg(unix)]
809-
impl IntoRawFd for TcpSocket {
810-
fn into_raw_fd(self) -> RawFd {
811-
self.inner.into_raw_fd()
806+
impl IntoRawFd for TcpSocket {
807+
fn into_raw_fd(self) -> RawFd {
808+
self.inner.into_raw_fd()
809+
}
812810
}
813811
}
814812

0 commit comments

Comments
 (0)