Skip to content

Commit e15f886

Browse files
committed
[WIP] Add links to OS documentation
Depends on extended_key_value_attributes, tracked here: rust-lang/rust#78835.
1 parent 9140e2a commit e15f886

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
#![cfg_attr(test, deny(warnings))]
6666
// Disallow warnings in examples.
6767
#![doc(test(attr(deny(warnings))))]
68+
// FIXME: remove.
69+
#![feature(extended_key_value_attributes)]
6870

6971
use std::net::SocketAddr;
7072
use std::time::Duration;
@@ -119,6 +121,22 @@ macro_rules! from {
119121
};
120122
}
121123

124+
#[rustfmt::skip]
125+
macro_rules! man_links {
126+
($syscall: tt ( $section: tt ) ) => {
127+
concat!(
128+
"Additional documentation can be found in documentation of the OS.\n\n",
129+
" * DragonFly BSD: <https://man.dragonflybsd.org/?command=", stringify!($syscall), "&section=", stringify!($section), ">\n",
130+
" * FreeBSD: <https://www.freebsd.org/cgi/man.cgi?query=", stringify!($syscall), "&sektion=", stringify!($section), ">\n",
131+
" * illumos: <https://illumos.org/man/3SOCKET/", stringify!($syscall), ">\n",
132+
" * Linux: <https://man7.org/linux/man-pages/man", stringify!($section), "/", stringify!($syscall), ".", stringify!($section), ".html>\n",
133+
" * NetBSD: <https://man.netbsd.org/", stringify!($syscall), ".", stringify!($section), ">\n",
134+
" * OpenBSD: <https://man.openbsd.org/", stringify!($syscall), ".", stringify!($section), ">\n",
135+
" * Windows: <https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-", stringify!($syscall), ">\n",
136+
);
137+
};
138+
}
139+
122140
mod sockaddr;
123141
mod socket;
124142
mod sockref;

src/socket.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ impl Socket {
8282
/// the socket is made non-inheritable.
8383
///
8484
/// [`Socket::new_raw`] can be used if you don't want these flags to be set.
85+
///
86+
#[doc = man_links!(socket(2))]
8587
pub fn new(domain: Domain, ty: Type, protocol: Option<Protocol>) -> io::Result<Socket> {
8688
let ty = set_common_type(ty);
8789
Socket::new_raw(domain, ty, protocol).and_then(set_common_flags)
@@ -91,6 +93,8 @@ impl Socket {
9193
///
9294
/// This function corresponds to `socket(2)` on Unix and `WSASocketW` on
9395
/// Windows and simply creates a new socket, no other configuration is done.
96+
///
97+
#[doc = man_links!(socket(2))]
9498
pub fn new_raw(domain: Domain, ty: Type, protocol: Option<Protocol>) -> io::Result<Socket> {
9599
let protocol = protocol.map(|p| p.0).unwrap_or(0);
96100
sys::socket(domain.0, ty.0, protocol).map(|inner| Socket { inner })
@@ -106,6 +110,8 @@ impl Socket {
106110
/// # Notes
107111
///
108112
/// This function is only available on Unix.
113+
///
114+
#[doc = man_links!(socketpair(2))]
109115
#[cfg(all(feature = "all", unix))]
110116
pub fn pair(
111117
domain: Domain,
@@ -126,6 +132,8 @@ impl Socket {
126132
/// # Notes
127133
///
128134
/// This function is only available on Unix.
135+
///
136+
#[doc = man_links!(socketpair(2))]
129137
#[cfg(all(feature = "all", unix))]
130138
pub fn pair_raw(
131139
domain: Domain,
@@ -141,6 +149,8 @@ impl Socket {
141149
///
142150
/// This function directly corresponds to the `bind(2)` function on Windows
143151
/// and Unix.
152+
///
153+
#[doc = man_links!(bind(2))]
144154
pub fn bind(&self, address: &SockAddr) -> io::Result<()> {
145155
sys::bind(self.inner, address)
146156
}
@@ -159,6 +169,8 @@ impl Socket {
159169
/// non-blocking mode before calling this function), socket option can't be
160170
/// set *while connecting*. This will cause errors on Windows. Socket
161171
/// options can be safely set before and after connecting the socket.
172+
///
173+
#[doc = man_links!(connect(2))]
162174
pub fn connect(&self, address: &SockAddr) -> io::Result<()> {
163175
sys::connect(self.inner, address)
164176
}
@@ -171,6 +183,8 @@ impl Socket {
171183
///
172184
/// An error will be returned if `listen` or `connect` has already been
173185
/// called on this builder.
186+
///
187+
#[doc = man_links!(listen(2))]
174188
pub fn listen(&self, backlog: i32) -> io::Result<()> {
175189
sys::listen(self.inner, backlog)
176190
}
@@ -182,6 +196,8 @@ impl Socket {
182196
///
183197
/// This function sets the same flags as in done for [`Socket::new`],
184198
/// [`Socket::accept_raw`] can be used if you don't want to set those flags.
199+
///
200+
#[doc = man_links!(accept(2))]
185201
pub fn accept(&self) -> io::Result<(Socket, SockAddr)> {
186202
// Use `accept4` on platforms that support it.
187203
#[cfg(any(
@@ -213,6 +229,8 @@ impl Socket {
213229
///
214230
/// This function directly corresponds to the `accept(2)` function on
215231
/// Windows and Unix.
232+
///
233+
#[doc = man_links!(accept(2))]
216234
pub fn accept_raw(&self) -> io::Result<(Socket, SockAddr)> {
217235
sys::accept(self.inner).map(|(inner, addr)| (Socket { inner }, addr))
218236
}
@@ -225,6 +243,8 @@ impl Socket {
225243
/// [bound].
226244
///
227245
/// [bound]: Socket::bind
246+
///
247+
#[doc = man_links!(getsockname(2))]
228248
pub fn local_addr(&self) -> io::Result<SockAddr> {
229249
sys::getsockname(self.inner)
230250
}
@@ -236,6 +256,8 @@ impl Socket {
236256
/// This returns an error if the socket is not [`connect`ed].
237257
///
238258
/// [`connect`ed]: Socket::connect
259+
///
260+
#[doc = man_links!(getpeername(2))]
239261
pub fn peer_addr(&self) -> io::Result<SockAddr> {
240262
sys::getpeername(self.inner)
241263
}
@@ -253,6 +275,8 @@ impl Socket {
253275
/// On Windows this can **not** be used function cannot be used on a
254276
/// QOS-enabled socket, see
255277
/// <https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsaduplicatesocketw>.
278+
///
279+
#[doc = man_links!(dup(2))] // FIXME: Windows link to WSADuplicateSocketW.
256280
pub fn try_clone(&self) -> io::Result<Socket> {
257281
sys::try_clone(self.inner).map(|inner| Socket { inner })
258282
}
@@ -273,6 +297,8 @@ impl Socket {
273297
///
274298
/// This function will cause all pending and future I/O on the specified
275299
/// portions to return immediately with an appropriate value.
300+
///
301+
#[doc = man_links!(shutdown(2))]
276302
pub fn shutdown(&self, how: Shutdown) -> io::Result<()> {
277303
sys::shutdown(self.inner, how)
278304
}
@@ -284,6 +310,8 @@ impl Socket {
284310
/// This method might fail if the socket is not connected.
285311
///
286312
/// [`connect`]: Socket::connect
313+
///
314+
#[doc = man_links!(recv(2))]
287315
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
288316
self.recv_with_flags(buf, 0)
289317
}
@@ -295,6 +323,8 @@ impl Socket {
295323
///
296324
/// [`recv`]: Socket::recv
297325
/// [`out_of_band_inline`]: Socket::out_of_band_inline
326+
///
327+
#[doc = man_links!(recv(2))]
298328
#[cfg(feature = "all")]
299329
pub fn recv_out_of_band(&self, buf: &mut [u8]) -> io::Result<usize> {
300330
self.recv_with_flags(buf, sys::MSG_OOB)
@@ -304,6 +334,8 @@ impl Socket {
304334
/// the underlying `recv` call.
305335
///
306336
/// [`recv`]: Socket::recv
337+
///
338+
#[doc = man_links!(recv(2))]
307339
pub fn recv_with_flags(&self, buf: &mut [u8], flags: sys::c_int) -> io::Result<usize> {
308340
sys::recv(self.inner, buf, flags)
309341
}
@@ -320,6 +352,8 @@ impl Socket {
320352
///
321353
/// [`recv`]: Socket::recv
322354
/// [`connect`]: Socket::connect
355+
///
356+
#[doc = man_links!(recvmsg(2))] // FIXME: on Windows link to WSARecv.
323357
#[cfg(not(target_os = "redox"))]
324358
pub fn recv_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<(usize, RecvFlags)> {
325359
self.recv_vectored_with_flags(bufs, 0)
@@ -329,6 +363,8 @@ impl Socket {
329363
/// flags to the underlying `recvmsg`/`WSARecv` call.
330364
///
331365
/// [`recv_vectored`]: Socket::recv_vectored
366+
///
367+
#[doc = man_links!(recvmsg(2))] // FIXME: on Windows link to WSARecv.
332368
#[cfg(not(target_os = "redox"))]
333369
pub fn recv_vectored_with_flags(
334370
&self,
@@ -344,12 +380,16 @@ impl Socket {
344380
///
345381
/// Successive calls return the same data. This is accomplished by passing
346382
/// `MSG_PEEK` as a flag to the underlying `recv` system call.
383+
///
384+
#[doc = man_links!(recv(2))]
347385
pub fn peek(&self, buf: &mut [u8]) -> io::Result<usize> {
348386
self.recv_with_flags(buf, sys::MSG_PEEK)
349387
}
350388

351389
/// Receives data from the socket. On success, returns the number of bytes
352390
/// read and the address from whence the data came.
391+
///
392+
#[doc = man_links!(recvfrom(2))]
353393
pub fn recv_from(&self, buf: &mut [u8]) -> io::Result<(usize, SockAddr)> {
354394
self.recv_from_with_flags(buf, 0)
355395
}
@@ -358,6 +398,8 @@ impl Socket {
358398
/// flags to the underlying `recvfrom` call.
359399
///
360400
/// [`recv_from`]: Socket::recv_from
401+
///
402+
#[doc = man_links!(recvfrom(2))]
361403
pub fn recv_from_with_flags(
362404
&self,
363405
buf: &mut [u8],
@@ -371,6 +413,8 @@ impl Socket {
371413
/// [`recv_from`] this allows passing multiple buffers.
372414
///
373415
/// [`recv_from`]: Socket::recv_from
416+
///
417+
#[doc = man_links!(recvmsg(2))] // FIXME: fix Windows link.
374418
#[cfg(not(target_os = "redox"))]
375419
pub fn recv_from_vectored(
376420
&self,
@@ -383,6 +427,8 @@ impl Socket {
383427
/// arbitrary flags to the underlying `recvmsg`/`WSARecvFrom` call.
384428
///
385429
/// [`recv_from_vectored`]: Socket::recv_from_vectored
430+
///
431+
#[doc = man_links!(recvmsg(2))] // FIXME: fix Windows link.
386432
#[cfg(not(target_os = "redox"))]
387433
pub fn recv_from_vectored_with_flags(
388434
&self,
@@ -399,6 +445,8 @@ impl Socket {
399445
///
400446
/// On success, returns the number of bytes peeked and the address from
401447
/// whence the data came.
448+
///
449+
#[doc = man_links!(recv(2))]
402450
pub fn peek_from(&self, buf: &mut [u8]) -> io::Result<(usize, SockAddr)> {
403451
self.recv_from_with_flags(buf, sys::MSG_PEEK)
404452
}
@@ -409,6 +457,8 @@ impl Socket {
409457
/// been connected.
410458
///
411459
/// On success returns the number of bytes that were sent.
460+
///
461+
#[doc = man_links!(send(2))]
412462
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
413463
self.send_with_flags(buf, 0)
414464
}
@@ -417,11 +467,15 @@ impl Socket {
417467
/// `send` call.
418468
///
419469
/// [`send`]: #method.send
470+
///
471+
#[doc = man_links!(send(2))]
420472
pub fn send_with_flags(&self, buf: &[u8], flags: i32) -> io::Result<usize> {
421473
sys::send(self.inner, buf, flags)
422474
}
423475

424476
/// Send data to the connected peer. Returns the amount of bytes written.
477+
///
478+
#[doc = man_links!(sendmsg(2))] // FIXME: Windows link.
425479
#[cfg(not(target_os = "redox"))]
426480
pub fn send_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
427481
self.send_vectored_with_flags(bufs, 0)
@@ -431,6 +485,8 @@ impl Socket {
431485
/// flags to the underlying `sendmsg`/`WSASend` call.
432486
///
433487
/// [`send_vectored`]: Socket::send_vectored
488+
///
489+
#[doc = man_links!(sendmsg(2))] // FIXME: Windows link.
434490
#[cfg(not(target_os = "redox"))]
435491
pub fn send_vectored_with_flags(&self, bufs: &[IoSlice<'_>], flags: i32) -> io::Result<usize> {
436492
sys::send_vectored(self.inner, bufs, flags)
@@ -443,6 +499,8 @@ impl Socket {
443499
///
444500
/// [`send`]: #method.send
445501
/// [`out_of_band_inline`]: #method.out_of_band_inline
502+
///
503+
#[doc = man_links!(send(2))]
446504
#[cfg(feature = "all")]
447505
pub fn send_out_of_band(&self, buf: &[u8]) -> io::Result<usize> {
448506
self.send_with_flags(buf, sys::MSG_OOB)
@@ -452,6 +510,8 @@ impl Socket {
452510
/// number of bytes written.
453511
///
454512
/// This is typically used on UDP or datagram-oriented sockets.
513+
///
514+
#[doc = man_links!(sendto(2))]
455515
pub fn send_to(&self, buf: &[u8], addr: &SockAddr) -> io::Result<usize> {
456516
self.send_to_with_flags(buf, addr, 0)
457517
}
@@ -460,12 +520,16 @@ impl Socket {
460520
/// to the underlying `sendto` call.
461521
///
462522
/// [`send_to`]: Socket::send_to
523+
///
524+
#[doc = man_links!(sendto(2))]
463525
pub fn send_to_with_flags(&self, buf: &[u8], addr: &SockAddr, flags: i32) -> io::Result<usize> {
464526
sys::send_to(self.inner, buf, addr, flags)
465527
}
466528

467529
/// Send data to a peer listening on `addr`. Returns the amount of bytes
468530
/// written.
531+
///
532+
#[doc = man_links!(sendmsg(2))] // FIXME: windows link.
469533
#[cfg(not(target_os = "redox"))]
470534
pub fn send_to_vectored(&self, bufs: &[IoSlice<'_>], addr: &SockAddr) -> io::Result<usize> {
471535
self.send_to_vectored_with_flags(bufs, addr, 0)
@@ -475,6 +539,8 @@ impl Socket {
475539
/// arbitrary flags to the underlying `sendmsg`/`WSASendTo` call.
476540
///
477541
/// [`send_to_vectored`]: Socket::send_to_vectored
542+
///
543+
#[doc = man_links!(sendmsg(2))] // FIXME: windows link.
478544
#[cfg(not(target_os = "redox"))]
479545
pub fn send_to_vectored_with_flags(
480546
&self,

0 commit comments

Comments
 (0)