Skip to content

Commit 43b506d

Browse files
committed
Add links to online OS documentation
1 parent 9c9520c commit 43b506d

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

src/lib.rs

+50
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ macro_rules! from {
115115
};
116116
}
117117

118+
/// Link to online documentation for (almost) all supported OSs.
119+
#[rustfmt::skip]
120+
macro_rules! man_links {
121+
// Links to all OSs.
122+
($syscall: tt ( $section: tt ) ) => {
123+
concat!(
124+
man_links!(__ intro),
125+
man_links!(__ unix $syscall($section)),
126+
man_links!(__ windows $syscall($section)),
127+
)
128+
};
129+
// Links to Unix-like OSs.
130+
(unix: $syscall: tt ( $section: tt ) ) => {
131+
concat!(
132+
man_links!(__ intro),
133+
man_links!(__ unix $syscall($section)),
134+
)
135+
};
136+
// Links to Windows only.
137+
(windows: $syscall: tt ( $section: tt ) ) => {
138+
concat!(
139+
man_links!(__ intro),
140+
man_links!(__ windows $syscall($section)),
141+
)
142+
};
143+
// Internals.
144+
(__ intro) => {
145+
"\n\nAdditional documentation can be found in manual of the OS:\n\n"
146+
};
147+
// List for Unix-like OSs.
148+
(__ unix $syscall: tt ( $section: tt ) ) => {
149+
concat!(
150+
" * DragonFly BSD: <https://man.dragonflybsd.org/?command=", stringify!($syscall), "&section=", stringify!($section), ">\n",
151+
" * FreeBSD: <https://www.freebsd.org/cgi/man.cgi?query=", stringify!($syscall), "&sektion=", stringify!($section), ">\n",
152+
" * Linux: <https://man7.org/linux/man-pages/man", stringify!($section), "/", stringify!($syscall), ".", stringify!($section), ".html>\n",
153+
" * macOS: <https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/", stringify!($syscall), ".", stringify!($section), ".html> (archived, actually for iOS)\n",
154+
" * NetBSD: <https://man.netbsd.org/", stringify!($syscall), ".", stringify!($section), ">\n",
155+
" * OpenBSD: <https://man.openbsd.org/", stringify!($syscall), ".", stringify!($section), ">\n",
156+
" * iOS: <https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/", stringify!($syscall), ".", stringify!($section), ".html> (archived)\n",
157+
" * illumos: <https://illumos.org/man/3SOCKET/", stringify!($syscall), ">\n",
158+
)
159+
};
160+
// List for Window (so just Windows).
161+
(__ windows $syscall: tt ( $section: tt ) ) => {
162+
concat!(
163+
" * Windows: <https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-", stringify!($syscall), ">\n",
164+
)
165+
};
166+
}
167+
118168
mod sockaddr;
119169
mod socket;
120170
mod sockref;

src/socket.rs

+28-6
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ impl Socket {
121121
///
122122
/// On Unix-like systems, the close-on-exec flag is set on the new socket.
123123
/// Additionally, on Apple platforms `SOCK_NOSIGPIPE` is set. On Windows,
124-
/// the socket is made non-inheritable.
125-
///
126-
/// [`Socket::new_raw`] can be used if you don't want these flags to be set.
124+
/// the socket is made non-inheritable. [`Socket::new_raw`] can be used if
125+
/// you don't want these flags to be set.
126+
#[doc = man_links!(socket(2))]
127127
pub fn new(domain: Domain, ty: Type, protocol: Option<Protocol>) -> io::Result<Socket> {
128128
let ty = set_common_type(ty);
129129
Socket::new_raw(domain, ty, protocol).and_then(set_common_flags)
@@ -142,8 +142,9 @@ impl Socket {
142142
///
143143
/// This function corresponds to `socketpair(2)`.
144144
///
145-
/// This function sets the same flags as in done for [`Socket::new`],
145+
/// This function sets the same flags as in done for [`Socket::new`].
146146
/// [`Socket::pair_raw`] can be used if you don't want to set those flags.
147+
#[doc = man_links!(unix: socketpair(2))]
147148
#[cfg(any(doc, all(feature = "all", unix)))]
148149
#[cfg_attr(docsrs, doc(cfg(all(feature = "all", unix))))]
149150
pub fn pair(
@@ -177,6 +178,7 @@ impl Socket {
177178
///
178179
/// This function directly corresponds to the `bind(2)` function on Windows
179180
/// and Unix.
181+
#[doc = man_links!(bind(2))]
180182
pub fn bind(&self, address: &SockAddr) -> io::Result<()> {
181183
sys::bind(self.as_raw(), address)
182184
}
@@ -188,6 +190,7 @@ impl Socket {
188190
///
189191
/// An error will be returned if `listen` or `connect` has already been
190192
/// called on this builder.
193+
#[doc = man_links!(connect(2))]
191194
///
192195
/// # Notes
193196
///
@@ -242,6 +245,7 @@ impl Socket {
242245
///
243246
/// An error will be returned if `listen` or `connect` has already been
244247
/// called on this builder.
248+
#[doc = man_links!(listen(2))]
245249
pub fn listen(&self, backlog: c_int) -> io::Result<()> {
246250
sys::listen(self.as_raw(), backlog)
247251
}
@@ -253,6 +257,7 @@ impl Socket {
253257
///
254258
/// This function sets the same flags as in done for [`Socket::new`],
255259
/// [`Socket::accept_raw`] can be used if you don't want to set those flags.
260+
#[doc = man_links!(accept(2))]
256261
pub fn accept(&self) -> io::Result<(Socket, SockAddr)> {
257262
// Use `accept4` on platforms that support it.
258263
#[cfg(any(
@@ -299,6 +304,10 @@ impl Socket {
299304

300305
/// Returns the socket address of the local half of this socket.
301306
///
307+
/// This function directly corresponds to the `getsockname(2)` function on
308+
/// Windows and Unix.
309+
#[doc = man_links!(getsockname(2))]
310+
///
302311
/// # Notes
303312
///
304313
/// Depending on the OS this may return an error if the socket is not
@@ -311,6 +320,10 @@ impl Socket {
311320

312321
/// Returns the socket address of the remote peer of this socket.
313322
///
323+
/// This function directly corresponds to the `getpeername(2)` function on
324+
/// Windows and Unix.
325+
#[doc = man_links!(getpeername(2))]
326+
///
314327
/// # Notes
315328
///
316329
/// This returns an error if the socket is not [`connect`ed].
@@ -336,8 +349,7 @@ impl Socket {
336349
/// On Windows this uses `WSA_FLAG_NO_HANDLE_INHERIT` setting inheriting to
337350
/// false.
338351
///
339-
/// On Windows this can **not** be used function cannot be used on a
340-
/// QOS-enabled socket, see
352+
/// On Windows this function cannot be used on a QOS-enabled socket, see
341353
/// <https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketw>.
342354
pub fn try_clone(&self) -> io::Result<Socket> {
343355
sys::try_clone(self.as_raw()).map(Socket::from_raw)
@@ -359,6 +371,7 @@ impl Socket {
359371
///
360372
/// This function will cause all pending and future I/O on the specified
361373
/// portions to return immediately with an appropriate value.
374+
#[doc = man_links!(shutdown(2))]
362375
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
363376
sys::shutdown(self.as_raw(), how)
364377
}
@@ -368,6 +381,7 @@ impl Socket {
368381
///
369382
/// The [`connect`] method will connect this socket to a remote address.
370383
/// This method might fail if the socket is not connected.
384+
#[doc = man_links!(recv(2))]
371385
///
372386
/// [`connect`]: Socket::connect
373387
///
@@ -419,6 +433,7 @@ impl Socket {
419433
/// In addition to the number of bytes read, this function returns the flags
420434
/// for the received message. See [`RecvFlags`] for more information about
421435
/// the returned flags.
436+
#[doc = man_links!(recvmsg(2))]
422437
///
423438
/// [`recv`]: Socket::recv
424439
/// [`connect`]: Socket::connect
@@ -484,6 +499,7 @@ impl Socket {
484499

485500
/// Receives data from the socket. On success, returns the number of bytes
486501
/// read and the address from whence the data came.
502+
#[doc = man_links!(recvfrom(2))]
487503
///
488504
/// # Safety
489505
///
@@ -510,6 +526,7 @@ impl Socket {
510526
/// Receives data from the socket. Returns the amount of bytes read, the
511527
/// [`RecvFlags`] and the remote address from the data is coming. Unlike
512528
/// [`recv_from`] this allows passing multiple buffers.
529+
#[doc = man_links!(recvmsg(2))]
513530
///
514531
/// [`recv_from`]: Socket::recv_from
515532
///
@@ -573,6 +590,7 @@ impl Socket {
573590
/// been connected.
574591
///
575592
/// On success returns the number of bytes that were sent.
593+
#[doc = man_links!(send(2))]
576594
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
577595
self.send_with_flags(buf, 0)
578596
}
@@ -594,6 +612,7 @@ impl Socket {
594612

595613
/// Identical to [`send_vectored`] but allows for specification of arbitrary
596614
/// flags to the underlying `sendmsg`/`WSASend` call.
615+
#[doc = man_links!(sendmsg(2))]
597616
///
598617
/// [`send_vectored`]: Socket::send_vectored
599618
#[cfg(not(target_os = "redox"))]
@@ -621,6 +640,7 @@ impl Socket {
621640
/// number of bytes written.
622641
///
623642
/// This is typically used on UDP or datagram-oriented sockets.
643+
#[doc = man_links!(sendto(2))]
624644
pub fn send_to(&self, buf: &[u8], addr: &SockAddr) -> io::Result<usize> {
625645
self.send_to_with_flags(buf, addr, 0)
626646
}
@@ -640,6 +660,7 @@ impl Socket {
640660

641661
/// Send data to a peer listening on `addr`. Returns the amount of bytes
642662
/// written.
663+
#[doc = man_links!(sendmsg(2))]
643664
#[cfg(not(target_os = "redox"))]
644665
#[cfg_attr(docsrs, doc(cfg(not(target_os = "redox"))))]
645666
pub fn send_to_vectored(&self, bufs: &[IoSlice<'_>], addr: &SockAddr) -> io::Result<usize> {
@@ -737,6 +758,7 @@ pub enum InterfaceIndexOrAddress {
737758
/// Additional documentation can be found in documentation of the OS.
738759
/// * Linux: <https://man7.org/linux/man-pages/man7/socket.7.html>
739760
/// * Windows: <https://docs.microsoft.com/en-us/windows/win32/winsock/sol-socket-socket-options>
761+
//#[doc = man_links!(setsockopt(2))]
740762
impl Socket {
741763
/// Get the value of the `SO_BROADCAST` option for this socket.
742764
///

src/sys/unix.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,7 @@ impl crate::Socket {
10531053
/// This function will block the calling thread until a new connection is
10541054
/// established. When established, the corresponding `Socket` and the remote
10551055
/// peer's address will be returned.
1056+
#[doc = man_links!(unix: accept4(2))]
10561057
#[cfg(all(
10571058
feature = "all",
10581059
any(
@@ -1806,6 +1807,7 @@ impl crate::Socket {
18061807
/// Different OSs support different kinds of `file`s, see the OS
18071808
/// documentation for what kind of files are supported. Generally *regular*
18081809
/// files are supported by all OSs.
1810+
#[doc = man_links!(unix: sendfile(2))]
18091811
///
18101812
/// The `offset` is the absolute offset into the `file` to use as starting
18111813
/// point.

0 commit comments

Comments
 (0)