Skip to content

Commit f26edab

Browse files
authored
Miscellaneous documentation and debug output fixes. (#1390)
1 parent 7d6d3fa commit f26edab

File tree

14 files changed

+118
-37
lines changed

14 files changed

+118
-37
lines changed

examples/buffer_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn error_retry_closure_uninit() {
7070

7171
let mut event_buf = [MaybeUninit::<u8>::uninit(); 4];
7272

73-
// Ideally we'd write this, but it gets:
73+
// It's tempting to write this, but it gets:
7474
// "captured variable cannot escape `FnMut` closure body".
7575
/*
7676
rustix::io::retry_on_intr(|| b(&mut event_buf)).unwrap();

examples/stdio.rs

+2
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ fn key(b: u8) -> String {
348348
format!("^{}", (b + 0x40) as char)
349349
} else if b == 0x7f {
350350
"^?".to_string()
351+
} else if b >= 0x80 {
352+
format!("M-{}", key(b - 0x80))
351353
} else {
352354
format!("{}", b as char)
353355
}

src/backend/libc/event/syscalls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ pub(crate) unsafe fn port_getn(
421421
};
422422

423423
// `port_getn` special-cases a max value of 0 to be a query that returns
424-
// the number of events, so so bail out early if needed.
424+
// the number of events, so bail out early if needed.
425425
if events.1 == 0 {
426426
return Ok(0);
427427
}

src/backend/libc/mount/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ bitflags! {
312312
/// Mount and unmount events propagate from the upstream peer group
313313
/// into the downstream.
314314
///
315-
/// In Linux documentation, this flag is named <code>MS_SLAVE</code>,
316-
/// and the concepts of “upstream” and “downstream” are called
315+
/// In Linux documentation, this flag is named `MS_SLAVE`, and the
316+
/// concepts of “upstream” and “downstream” are called
317317
/// “master” and “slave”.
318318
#[doc(alias = "SLAVE")]
319319
const DOWNSTREAM = c::MS_SLAVE;

src/backend/libc/net/netdevice.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
use crate::alloc::string::String;
77
use crate::backend::c;
88
use crate::backend::io::syscalls::ioctl;
9-
use crate::fd::AsFd;
9+
use crate::fd::BorrowedFd;
1010
use crate::io;
1111
#[cfg(feature = "alloc")]
1212
use c::SIOCGIFNAME;
1313
use c::{__c_anonymous_ifr_ifru, c_char, ifreq, IFNAMSIZ, SIOCGIFINDEX};
1414

15-
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
15+
pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32> {
1616
let if_name_bytes = if_name.as_bytes();
1717
if if_name_bytes.len() >= IFNAMSIZ as usize {
1818
return Err(io::Errno::NODEV);
@@ -26,21 +26,21 @@ pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
2626
let mut if_name_c_char_iter = if_name_bytes.iter().map(|byte| *byte as c_char);
2727
ifreq.ifr_name[..if_name_bytes.len()].fill_with(|| if_name_c_char_iter.next().unwrap());
2828

29-
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX as _, &mut ifreq as *mut ifreq as _) }?;
29+
unsafe { ioctl(fd, SIOCGIFINDEX as _, &mut ifreq as *mut ifreq as _) }?;
3030
let index = unsafe { ifreq.ifr_ifru.ifru_ifindex };
3131
Ok(index as u32)
3232
}
3333

3434
#[cfg(feature = "alloc")]
35-
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
35+
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String> {
3636
let mut ifreq = ifreq {
3737
ifr_name: [0; 16],
3838
ifr_ifru: __c_anonymous_ifr_ifru {
3939
ifru_ifindex: index as _,
4040
},
4141
};
4242

43-
unsafe { ioctl(fd.as_fd(), SIOCGIFNAME as _, &mut ifreq as *mut ifreq as _) }?;
43+
unsafe { ioctl(fd, SIOCGIFNAME as _, &mut ifreq as *mut ifreq as _) }?;
4444

4545
if let Some(nul_byte) = ifreq.ifr_name.iter().position(|char| *char == 0) {
4646
let name: String = ifreq.ifr_name[..nul_byte]

src/backend/linux_raw/mount/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ bitflags! {
304304
/// Mount and unmount events propagate from the upstream peer group
305305
/// into the downstream.
306306
///
307-
/// In Linux documentation, this flag is named <code>MS_SLAVE</code>,
308-
/// and the concepts of “upstream” and “downstream” are called
307+
/// In Linux documentation, this flag is named `MS_SLAVE`, and the
308+
/// concepts of “upstream” and “downstream” are called
309309
/// “master” and “slave”.
310310
#[doc(alias = "SLAVE")]
311311
const DOWNSTREAM = linux_raw_sys::general::MS_SLAVE;

src/backend/linux_raw/net/netdevice.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(unsafe_code)]
44

55
use crate::backend::io::syscalls::ioctl;
6-
use crate::fd::AsFd;
6+
use crate::fd::BorrowedFd;
77
use crate::io;
88
use core::ptr::addr_of_mut;
99
use core::{slice, str};
@@ -15,7 +15,7 @@ use linux_raw_sys::net::{ifreq, ifreq__bindgen_ty_1, ifreq__bindgen_ty_2, IFNAMS
1515
#[cfg(feature = "alloc")]
1616
use {alloc::borrow::ToOwned, alloc::string::String};
1717

18-
pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
18+
pub(crate) fn name_to_index(fd: BorrowedFd<'_>, if_name: &str) -> io::Result<u32> {
1919
let if_name_bytes = if_name.as_bytes();
2020
if if_name_bytes.len() >= IFNAMSIZ as usize {
2121
return Err(io::Errno::NODEV);
@@ -35,21 +35,21 @@ pub(crate) fn name_to_index(fd: impl AsFd, if_name: &str) -> io::Result<u32> {
3535
};
3636
unsafe { ifreq.ifr_ifrn.ifrn_name[..if_name_bytes.len()].copy_from_slice(if_name_bytes) };
3737

38-
unsafe { ioctl(fd.as_fd(), SIOCGIFINDEX, addr_of_mut!(ifreq).cast()) }?;
38+
unsafe { ioctl(fd, SIOCGIFINDEX, addr_of_mut!(ifreq).cast()) }?;
3939
let index = unsafe { ifreq.ifr_ifru.ifru_ivalue };
4040
Ok(index as u32)
4141
}
4242

4343
#[cfg(feature = "alloc")]
44-
pub(crate) fn index_to_name(fd: impl AsFd, index: u32) -> io::Result<String> {
44+
pub(crate) fn index_to_name(fd: BorrowedFd<'_>, index: u32) -> io::Result<String> {
4545
let mut ifreq = ifreq {
4646
ifr_ifrn: ifreq__bindgen_ty_1 { ifrn_name: [0; 16] },
4747
ifr_ifru: ifreq__bindgen_ty_2 {
4848
ifru_ivalue: index as _,
4949
},
5050
};
5151

52-
unsafe { ioctl(fd.as_fd(), SIOCGIFNAME, addr_of_mut!(ifreq).cast()) }?;
52+
unsafe { ioctl(fd, SIOCGIFNAME, addr_of_mut!(ifreq).cast()) }?;
5353

5454
if let Some(nul_byte) = unsafe { ifreq.ifr_ifrn.ifrn_name }
5555
.iter()

src/backend/linux_raw/param/auxv.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,7 @@ unsafe fn check_elf_base(base: *const Elf_Ehdr) -> Option<NonNull<Elf_Ehdr>> {
487487
return None;
488488
}
489489

490-
let hdr = match check_raw_pointer::<Elf_Ehdr>(base as *mut _) {
491-
Some(hdr) => hdr,
492-
None => return None,
493-
};
490+
let hdr = check_raw_pointer::<Elf_Ehdr>(base as *mut _)?;
494491

495492
let hdr = hdr.as_ref();
496493
if hdr.e_ident[..SELFMAG] != ELFMAG {

src/io/close.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use backend::fd::RawFd;
1010

1111
/// `close(raw_fd)`—Closes a `RawFd` directly.
1212
///
13-
/// Most users won't need to use this, as `OwnedFd` automatically closes its
13+
/// Most users won't need to use this, as [`OwnedFd`] automatically closes its
1414
/// file descriptor on `Drop`.
1515
///
1616
/// This function does not return a `Result`, as it is the [responsibility] of
@@ -33,6 +33,7 @@ use backend::fd::RawFd;
3333
/// - [illumos]
3434
/// - [glibc]
3535
///
36+
/// [`OwnedFd`]: crate::fd::OwnedFd
3637
/// [Beej's Guide to Network Programming]: https://beej.us/guide/bgnet/html/split/system-calls-or-bust.html#close-and-shutdownget-outta-my-face
3738
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/close.html
3839
/// [Linux]: https://man7.org/linux/man-pages/man2/close.2.html

src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,12 @@
8989
//! [`cap-std`]: https://crates.io/crates/cap-std
9090
//! [`system-interface`]: https://crates.io/crates/system-interface
9191
//! [`io-streams`]: https://crates.io/crates/io-streams
92-
//! [`getrandom`]: https://crates.io/crates/getrandom
93-
//! [`bitflags`]: https://crates.io/crates/bitflags
94-
//! [`AsFd`]: https://doc.rust-lang.org/stable/std/os/fd/trait.AsFd.html
95-
//! [`OwnedFd`]: https://doc.rust-lang.org/stable/std/os/fd/struct.OwnedFd.html
92+
//! [`bitflags`]: bitflags
93+
//! [`AsFd`]: crate::fd::AsFd
94+
//! [`OwnedFd`]: crate::fd::OwnedFd
9695
//! [I/O-safe]: https://github.com/rust-lang/rfcs/blob/master/text/3128-io-safety.md
9796
//! [`Arg`]: path::Arg
98-
//! [support for externally defined flags]: https://docs.rs/bitflags/*/bitflags/#externally-defined-flags
97+
//! [support for externally defined flags]: bitflags#externally-defined-flags
9998
10099
#![deny(missing_docs)]
101100
#![allow(stable_features)]

src/net/netdevice.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use alloc::string::String;
3434
#[inline]
3535
#[doc(alias = "SIOCGIFINDEX")]
3636
pub fn name_to_index<Fd: AsFd>(fd: Fd, if_name: &str) -> io::Result<u32> {
37-
crate::backend::net::netdevice::name_to_index(fd, if_name)
37+
crate::backend::net::netdevice::name_to_index(fd.as_fd(), if_name)
3838
}
3939

4040
/// `ioctl(fd, SIOCGIFNAME, ifreq)`—Returns the interface name for a given
@@ -52,12 +52,13 @@ pub fn name_to_index<Fd: AsFd>(fd: Fd, if_name: &str) -> io::Result<u32> {
5252
#[cfg(feature = "alloc")]
5353
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
5454
pub fn index_to_name<Fd: AsFd>(fd: Fd, index: u32) -> io::Result<String> {
55-
crate::backend::net::netdevice::index_to_name(fd, index)
55+
crate::backend::net::netdevice::index_to_name(fd.as_fd(), index)
5656
}
5757

5858
#[cfg(test)]
5959
mod tests {
6060
use crate::backend::net::netdevice::{index_to_name, name_to_index};
61+
use crate::fd::AsFd;
6162
use crate::net::{AddressFamily, SocketFlags, SocketType};
6263

6364
#[test]
@@ -77,7 +78,7 @@ mod tests {
7778
.0
7879
.parse::<u32>()
7980
.unwrap();
80-
assert_eq!(Ok(loopback_index), name_to_index(fd, "lo"));
81+
assert_eq!(Ok(loopback_index), name_to_index(fd.as_fd(), "lo"));
8182
}
8283

8384
#[test]
@@ -98,6 +99,9 @@ mod tests {
9899
.0
99100
.parse::<u32>()
100101
.unwrap();
101-
assert_eq!(Ok("lo".to_owned()), index_to_name(fd, loopback_index));
102+
assert_eq!(
103+
Ok("lo".to_owned()),
104+
index_to_name(fd.as_fd(), loopback_index)
105+
);
102106
}
103107
}

src/net/send_recv/msg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ pub fn sendmsg<Fd: AsFd>(
703703
/// - [DragonFly BSD]
704704
/// - [illumos]
705705
///
706-
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendmsg.html
706+
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9799919799/functions/sendmsg.html
707707
/// [Linux]: https://man7.org/linux/man-pages/man2/sendmsg.2.html
708708
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/sendmsg.2.html
709709
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=sendmsg&sektion=2

src/process/wait.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bitflags! {
7272
}
7373

7474
/// The status of a child process after calling [`wait`]/[`waitpid`].
75-
#[derive(Debug, Clone, Copy)]
75+
#[derive(Clone, Copy)]
7676
#[repr(transparent)]
7777
pub struct WaitStatus(i32);
7878

@@ -91,31 +91,36 @@ impl WaitStatus {
9191

9292
/// Returns whether the process is currently stopped.
9393
#[inline]
94+
#[doc(alias = "WIFSTOPPED")]
9495
pub fn stopped(self) -> bool {
9596
backend::process::wait::WIFSTOPPED(self.0)
9697
}
9798

9899
/// Returns whether the process has exited normally.
99100
#[inline]
101+
#[doc(alias = "WIFEXITED")]
100102
pub fn exited(self) -> bool {
101103
backend::process::wait::WIFEXITED(self.0)
102104
}
103105

104106
/// Returns whether the process was terminated by a signal.
105107
#[inline]
108+
#[doc(alias = "WIFSIGNALED")]
106109
pub fn signaled(self) -> bool {
107110
backend::process::wait::WIFSIGNALED(self.0)
108111
}
109112

110113
/// Returns whether the process has continued from a job control stop.
111114
#[inline]
115+
#[doc(alias = "WIFCONTINUED")]
112116
pub fn continued(self) -> bool {
113117
backend::process::wait::WIFCONTINUED(self.0)
114118
}
115119

116120
/// Returns the number of the signal that stopped the process, if the
117121
/// process was stopped by a signal.
118122
#[inline]
123+
#[doc(alias = "WSTOPSIG")]
119124
pub fn stopping_signal(self) -> Option<i32> {
120125
if self.stopped() {
121126
Some(backend::process::wait::WSTOPSIG(self.0))
@@ -127,6 +132,7 @@ impl WaitStatus {
127132
/// Returns the exit status number returned by the process, if it exited
128133
/// normally.
129134
#[inline]
135+
#[doc(alias = "WEXITSTATUS")]
130136
pub fn exit_status(self) -> Option<i32> {
131137
if self.exited() {
132138
Some(backend::process::wait::WEXITSTATUS(self.0))
@@ -138,6 +144,7 @@ impl WaitStatus {
138144
/// Returns the number of the signal that terminated the process, if the
139145
/// process was terminated by a signal.
140146
#[inline]
147+
#[doc(alias = "WTERMSIG")]
141148
pub fn terminating_signal(self) -> Option<i32> {
142149
if self.signaled() {
143150
Some(backend::process::wait::WTERMSIG(self.0))
@@ -147,6 +154,26 @@ impl WaitStatus {
147154
}
148155
}
149156

157+
impl fmt::Debug for WaitStatus {
158+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
159+
let mut s = f.debug_struct("WaitStatus");
160+
s.field("stopped", &self.stopped());
161+
s.field("exited", &self.exited());
162+
s.field("signaled", &self.signaled());
163+
s.field("continued", &self.continued());
164+
if let Some(stopping_signal) = self.stopping_signal() {
165+
s.field("stopping_signal", &stopping_signal);
166+
}
167+
if let Some(exit_status) = self.exit_status() {
168+
s.field("exit_status", &exit_status);
169+
}
170+
if let Some(terminating_signal) = self.terminating_signal() {
171+
s.field("terminating_signal", &terminating_signal);
172+
}
173+
s.finish()
174+
}
175+
}
176+
150177
/// The status of a process after calling [`waitid`].
151178
#[derive(Clone, Copy)]
152179
#[repr(transparent)]

0 commit comments

Comments
 (0)