Skip to content

Commit e538c46

Browse files
author
Kai Jewson
authored
Use doc_cfg
1 parent 9d3e39c commit e538c46

File tree

5 files changed

+290
-69
lines changed

5 files changed

+290
-69
lines changed

src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ impl Type {
197197

198198
/// Type corresponding to `SOCK_SEQPACKET`.
199199
#[cfg(feature = "all")]
200+
#[cfg_attr(docsrs, doc(cfg(feature = "all")))]
200201
pub const SEQPACKET: Type = Type(sys::SOCK_SEQPACKET);
201202

202203
/// Type corresponding to `SOCK_RAW`.
203204
#[cfg(all(feature = "all", not(target_os = "redox")))]
205+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(target_os = "redox")))))]
204206
pub const RAW: Type = Type(sys::SOCK_RAW);
205207
}
206208

@@ -256,6 +258,7 @@ impl From<Protocol> for c_int {
256258
///
257259
/// Flags provide additional information about incoming messages.
258260
#[cfg(not(target_os = "redox"))]
261+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
259262
#[derive(Copy, Clone, Eq, PartialEq)]
260263
pub struct RecvFlags(c_int);
261264

@@ -369,6 +372,20 @@ impl TcpKeepalive {
369372
windows,
370373
)
371374
))]
375+
#[cfg_attr(
376+
docsrs,
377+
doc(cfg(all(
378+
feature = "all",
379+
any(
380+
target_os = "freebsd",
381+
target_os = "fuchsia",
382+
target_os = "linux",
383+
target_os = "netbsd",
384+
target_vendor = "apple",
385+
windows,
386+
)
387+
)))
388+
)]
372389
pub const fn with_interval(self, interval: Duration) -> Self {
373390
Self {
374391
interval: Some(interval),
@@ -383,13 +400,27 @@ impl TcpKeepalive {
383400
#[cfg(all(
384401
feature = "all",
385402
any(
403+
doc,
386404
target_os = "freebsd",
387405
target_os = "fuchsia",
388406
target_os = "linux",
389407
target_os = "netbsd",
390408
target_vendor = "apple",
391409
)
392410
))]
411+
#[cfg_attr(
412+
docsrs,
413+
doc(cfg(all(
414+
feature = "all",
415+
any(
416+
target_os = "freebsd",
417+
target_os = "fuchsia",
418+
target_os = "linux",
419+
target_os = "netbsd",
420+
target_vendor = "apple",
421+
)
422+
)))
423+
)]
393424
pub const fn with_retries(self, retries: u32) -> Self {
394425
Self {
395426
retries: Some(retries),

src/socket.rs

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,8 @@ impl Socket {
118118
///
119119
/// This function sets the same flags as in done for [`Socket::new`],
120120
/// [`Socket::pair_raw`] can be used if you don't want to set those flags.
121-
///
122-
/// # Notes
123-
///
124-
/// This function is only available on Unix.
125-
#[cfg(all(feature = "all", unix))]
121+
#[cfg(any(doc, all(feature = "all", unix)))]
122+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
126123
pub fn pair(
127124
domain: Domain,
128125
ty: Type,
@@ -138,11 +135,8 @@ impl Socket {
138135
/// Creates a pair of sockets which are connected to each other.
139136
///
140137
/// This function corresponds to `socketpair(2)`.
141-
///
142-
/// # Notes
143-
///
144-
/// This function is only available on Unix.
145-
#[cfg(all(feature = "all", unix))]
138+
#[cfg(any(doc, all(feature = "all", unix)))]
139+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
146140
pub fn pair_raw(
147141
domain: Domain,
148142
ty: Type,
@@ -416,6 +410,7 @@ impl Socket {
416410
/// function with `buf`s of type `&mut [IoSliceMut]`, allowing initialised
417411
/// buffers to be used without using `unsafe`.
418412
#[cfg(not(target_os = "redox"))]
413+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
419414
pub fn recv_vectored(
420415
&self,
421416
bufs: &mut [MaybeUninitSlice<'_>],
@@ -435,6 +430,7 @@ impl Socket {
435430
///
436431
/// [`recv_vectored`]: Socket::recv_vectored
437432
#[cfg(not(target_os = "redox"))]
433+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
438434
pub fn recv_vectored_with_flags(
439435
&self,
440436
bufs: &mut [MaybeUninitSlice<'_>],
@@ -498,6 +494,7 @@ impl Socket {
498494
///
499495
/// [`recv_vectored`]: Socket::recv_vectored
500496
#[cfg(not(target_os = "redox"))]
497+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
501498
pub fn recv_from_vectored(
502499
&self,
503500
bufs: &mut [MaybeUninitSlice<'_>],
@@ -517,6 +514,7 @@ impl Socket {
517514
///
518515
/// [`recv_vectored`]: Socket::recv_vectored
519516
#[cfg(not(target_os = "redox"))]
517+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
520518
pub fn recv_from_vectored_with_flags(
521519
&self,
522520
bufs: &mut [MaybeUninitSlice<'_>],
@@ -563,6 +561,7 @@ impl Socket {
563561

564562
/// Send data to the connected peer. Returns the amount of bytes written.
565563
#[cfg(not(target_os = "redox"))]
564+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
566565
pub fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
567566
self.send_vectored_with_flags(bufs, 0)
568567
}
@@ -572,6 +571,7 @@ impl Socket {
572571
///
573572
/// [`send_vectored`]: Socket::send_vectored
574573
#[cfg(not(target_os = "redox"))]
574+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
575575
pub fn send_vectored_with_flags(
576576
&self,
577577
bufs: &[IoSlice<'_>],
@@ -615,6 +615,7 @@ impl Socket {
615615
/// Send data to a peer listening on `addr`. Returns the amount of bytes
616616
/// written.
617617
#[cfg(not(target_os = "redox"))]
618+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
618619
pub fn send_to_vectored(&self, bufs: &[IoSlice<'_>], addr: &SockAddr) -> io::Result<usize> {
619620
self.send_to_vectored_with_flags(bufs, addr, 0)
620621
}
@@ -624,6 +625,7 @@ impl Socket {
624625
///
625626
/// [`send_to_vectored`]: Socket::send_to_vectored
626627
#[cfg(not(target_os = "redox"))]
628+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
627629
pub fn send_to_vectored_with_flags(
628630
&self,
629631
bufs: &[IoSlice<'_>],
@@ -794,6 +796,7 @@ impl Socket {
794796
///
795797
/// [`set_out_of_band_inline`]: Socket::set_out_of_band_inline
796798
#[cfg(not(target_os = "redox"))]
799+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
797800
pub fn out_of_band_inline(&self) -> io::Result<bool> {
798801
unsafe {
799802
getsockopt::<c_int>(self.as_raw(), sys::SOL_SOCKET, sys::SO_OOBINLINE)
@@ -808,6 +811,7 @@ impl Socket {
808811
/// `MSG_OOB` flag is set during receiving. As per RFC6093, TCP sockets
809812
/// using the Urgent mechanism are encouraged to set this flag.
810813
#[cfg(not(target_os = "redox"))]
814+
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
811815
pub fn set_out_of_band_inline(&self, oob_inline: bool) -> io::Result<()> {
812816
unsafe {
813817
setsockopt(
@@ -965,8 +969,9 @@ impl Socket {
965969
///
966970
/// For more information about this option, see [`set_ip_transparent`].
967971
///
968-
/// [`ip_transparent`]: Socket::set_ip_transparent
969-
#[cfg(all(feature = "all", target_os = "linux"))]
972+
/// [`set_ip_transparent`]: Socket::set_ip_transparent
973+
#[cfg(any(doc, all(feature = "all", target_os = "linux")))]
974+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
970975
pub fn ip_transparent(&self) -> io::Result<bool> {
971976
unsafe {
972977
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IP, libc::IP_TRANSPARENT)
@@ -989,8 +994,8 @@ impl Socket {
989994
///
990995
/// TProxy redirection with the iptables TPROXY target also
991996
/// requires that this option be set on the redirected socket.
992-
/// this feature is only available on linux
993-
#[cfg(all(feature = "all", target_os = "linux"))]
997+
#[cfg(any(doc, all(feature = "all", target_os = "linux")))]
998+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", target_os = "linux"))))]
994999
pub fn set_ip_transparent(&self, transparent: bool) -> io::Result<()> {
9951000
unsafe {
9961001
setsockopt(
@@ -1342,7 +1347,8 @@ impl Socket {
13421347
/// This returns the value of `SO_KEEPALIVE` on OpenBSD and Haiku,
13431348
/// `TCP_KEEPALIVE` on macOS and iOS, and `TCP_KEEPIDLE` on all other Unix
13441349
/// operating systems.
1345-
#[cfg(all(feature = "all", not(windows)))]
1350+
#[cfg(any(doc, all(feature = "all", not(windows))))]
1351+
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", not(windows)))))]
13461352
pub fn keepalive_time(&self) -> io::Result<Duration> {
13471353
sys::keepalive_time(self.as_raw())
13481354
}
@@ -1355,6 +1361,7 @@ impl Socket {
13551361
#[cfg(all(
13561362
feature = "all",
13571363
any(
1364+
doc,
13581365
target_os = "android",
13591366
target_os = "dragonfly",
13601367
target_os = "freebsd",
@@ -1365,6 +1372,22 @@ impl Socket {
13651372
target_vendor = "apple",
13661373
)
13671374
))]
1375+
#[cfg_attr(
1376+
docsrs,
1377+
doc(cfg(all(
1378+
feature = "all",
1379+
any(
1380+
target_os = "android",
1381+
target_os = "dragonfly",
1382+
target_os = "freebsd",
1383+
target_os = "fuchsia",
1384+
target_os = "illumos",
1385+
target_os = "linux",
1386+
target_os = "netbsd",
1387+
target_vendor = "apple",
1388+
)
1389+
)))
1390+
)]
13681391
pub fn keepalive_interval(&self) -> io::Result<Duration> {
13691392
unsafe {
13701393
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_TCP, sys::TCP_KEEPINTVL)
@@ -1380,6 +1403,7 @@ impl Socket {
13801403
#[cfg(all(
13811404
feature = "all",
13821405
any(
1406+
doc,
13831407
target_os = "android",
13841408
target_os = "dragonfly",
13851409
target_os = "freebsd",
@@ -1390,6 +1414,22 @@ impl Socket {
13901414
target_vendor = "apple",
13911415
)
13921416
))]
1417+
#[cfg_attr(
1418+
docsrs,
1419+
doc(cfg(all(
1420+
feature = "all",
1421+
any(
1422+
target_os = "android",
1423+
target_os = "dragonfly",
1424+
target_os = "freebsd",
1425+
target_os = "fuchsia",
1426+
target_os = "illumos",
1427+
target_os = "linux",
1428+
target_os = "netbsd",
1429+
target_vendor = "apple",
1430+
)
1431+
)))
1432+
)]
13931433
pub fn keepalive_retries(&self) -> io::Result<u32> {
13941434
unsafe {
13951435
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_TCP, sys::TCP_KEEPCNT)

src/sockref.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use crate::Socket;
2020
/// the caller must ensure the file descriptor/socket is a valid.
2121
///
2222
/// [`TcpStream`]: std::net::TcpStream
23-
/// [`AsRawFd`]: std::os::unix::io::AsRawFd
24-
/// [`AsRawSocket`]: std::os::windows::io::AsRawSocket
23+
// Don't use intra-doc links because they won't build on every platform.
24+
/// [`AsRawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/trait.AsRawFd.html
25+
/// [`AsRawSocket`]: https://doc.rust-lang.org/stable/std/os/windows/io/trait.AsRawSocket.html
2526
///
2627
/// # Examples
2728
///
@@ -74,7 +75,9 @@ impl<'s> Deref for SockRef<'s> {
7475
}
7576
}
7677

78+
/// On Windows, a corresponding `From<&impl AsRawSocket>` implementation exists.
7779
#[cfg(unix)]
80+
#[cfg_attr(docsrs, doc(cfg(unix)))]
7881
impl<'s, S> From<&'s S> for SockRef<'s>
7982
where
8083
S: AsRawFd,
@@ -88,12 +91,14 @@ where
8891
}
8992
}
9093

94+
/// On Unix, a corresponding `From<&impl AsRawFd>` implementation exists.
9195
#[cfg(windows)]
96+
#[cfg_attr(docsrs, doc(cfg(windows)))]
9297
impl<'s, S> From<&'s S> for SockRef<'s>
9398
where
9499
S: AsRawSocket,
95100
{
96-
/// See the `From<AsRawFd>` implementation.
101+
/// See the `From<&impl AsRawFd>` implementation.
97102
fn from(socket: &'s S) -> Self {
98103
SockRef {
99104
socket: ManuallyDrop::new(unsafe { Socket::from_raw_socket(socket.as_raw_socket()) }),

0 commit comments

Comments
 (0)