Skip to content

Commit ed5f233

Browse files
authored
Miscellaneous cleanups. (#650)
- Factor out more crate/module paths into `use` statements. - Fix a few clippy lints. - Use more target categories.
1 parent 1dfbd05 commit ed5f233

File tree

30 files changed

+256
-287
lines changed

30 files changed

+256
-287
lines changed

examples/stdio.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
111111
print!(" IMAXBEL");
112112
}
113113
#[cfg(not(any(
114-
bsd,
114+
freebsdlike,
115+
netbsdlike,
115116
solarish,
116117
target_os = "aix",
117118
target_os = "emscripten",
@@ -127,7 +128,13 @@ fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
127128
if (term.c_oflag & OPOST) != 0 {
128129
print!(" OPOST");
129130
}
130-
#[cfg(not(any(bsd, target_os = "aix", target_os = "redox")))]
131+
#[cfg(not(any(
132+
apple,
133+
freebsdlike,
134+
target_os = "aix",
135+
target_os = "netbsd",
136+
target_os = "redox"
137+
)))]
131138
if (term.c_oflag & OLCUC) != 0 {
132139
print!(" OLCUC");
133140
}

src/backend/libc/fs/syscalls.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
use super::super::c;
44
use super::super::conv::{borrowed_fd, c_str, ret, ret_c_int, ret_off_t, ret_owned_fd, ret_usize};
5-
#[cfg(any(target_os = "android", target_os = "linux"))]
6-
use super::super::conv::{syscall_ret, syscall_ret_owned_fd, syscall_ret_usize};
75
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
86
use super::super::offset::libc_fallocate;
97
#[cfg(not(any(
@@ -83,10 +81,6 @@ use crate::fs::SealFlags;
8381
target_os = "wasi",
8482
)))]
8583
use crate::fs::StatFs;
86-
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
87-
use crate::fs::XattrFlags;
88-
#[cfg(any(target_os = "android", target_os = "linux"))]
89-
use crate::fs::{cwd, RenameFlags, ResolveFlags, Statx, StatxFlags};
9084
use crate::fs::{Access, Mode, OFlags, Stat, Timestamps};
9185
#[cfg(not(any(apple, target_os = "redox", target_os = "wasi")))]
9286
use crate::fs::{Dev, FileType};
@@ -100,21 +94,25 @@ use crate::process::{Gid, Uid};
10094
target_env = "gnu",
10195
)))]
10296
use crate::utils::as_ptr;
97+
#[cfg(apple)]
98+
use alloc::vec;
10399
use core::convert::TryInto;
104-
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
105-
use core::mem::size_of;
106100
use core::mem::MaybeUninit;
107-
#[cfg(any(target_os = "android", target_os = "linux"))]
108-
use core::ptr::null;
109-
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
110-
use core::ptr::null_mut;
111101
#[cfg(apple)]
112102
use {
113103
super::super::conv::nonnegative_ret,
114104
crate::fs::{copyfile_state_t, CloneFlags, CopyfileFlags},
115105
};
106+
#[cfg(any(target_os = "android", target_os = "linux"))]
107+
use {
108+
super::super::conv::{syscall_ret, syscall_ret_owned_fd, syscall_ret_usize},
109+
crate::fs::{cwd, RenameFlags, ResolveFlags, Statx, StatxFlags},
110+
core::ptr::null,
111+
};
116112
#[cfg(not(target_os = "redox"))]
117113
use {super::super::offset::libc_openat, crate::fs::AtFlags};
114+
#[cfg(any(apple, target_os = "android", target_os = "linux"))]
115+
use {crate::fs::XattrFlags, core::mem::size_of, core::ptr::null_mut};
118116

119117
#[cfg(all(
120118
any(target_arch = "arm", target_arch = "mips", target_arch = "x86"),
@@ -1764,7 +1762,7 @@ pub(crate) fn getpath(fd: BorrowedFd<'_>) -> io::Result<CString> {
17641762
// `F_GETPATH` in terms of `MAXPATHLEN`, and there are no
17651763
// alternatives. If a better method is invented, it should be used
17661764
// instead.
1767-
let mut buf = alloc::vec![0; c::PATH_MAX as usize];
1765+
let mut buf = vec![0; c::PATH_MAX as usize];
17681766

17691767
// From the [macOS `fcntl` manual page]:
17701768
// `F_GETPATH` - Get the path of the file descriptor `Fildes`. The argument

src/backend/libc/io/syscalls.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! libc syscalls supporting `rustix::io`.
22
33
use super::super::c;
4-
#[cfg(any(target_os = "android", target_os = "linux"))]
5-
use super::super::conv::syscall_ret_owned_fd;
64
#[cfg(any(
75
target_os = "android",
86
all(target_os = "linux", not(target_env = "gnu")),
@@ -15,10 +13,6 @@ use super::super::offset::{libc_preadv, libc_pwritev};
1513
#[cfg(all(target_os = "linux", target_env = "gnu"))]
1614
use super::super::offset::{libc_preadv2, libc_pwritev2};
1715
use crate::fd::{AsFd, BorrowedFd, OwnedFd, RawFd};
18-
#[cfg(bsd)]
19-
use crate::io::kqueue::Event;
20-
#[cfg(solarish)]
21-
use crate::io::port::Event;
2216
#[cfg(not(any(target_os = "aix", target_os = "wasi")))]
2317
use crate::io::DupFlags;
2418
#[cfg(any(
@@ -31,15 +25,21 @@ use crate::io::EventfdFlags;
3125
#[cfg(not(any(apple, target_os = "aix", target_os = "haiku", target_os = "wasi")))]
3226
use crate::io::PipeFlags;
3327
use crate::io::{self, FdFlags, IoSlice, IoSliceMut, PollFd};
34-
#[cfg(any(target_os = "android", target_os = "linux"))]
35-
use crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags};
3628
use core::cmp::min;
3729
use core::convert::TryInto;
3830
use core::mem::MaybeUninit;
39-
#[cfg(any(target_os = "android", target_os = "linux"))]
40-
use core::ptr;
4131
#[cfg(all(feature = "fs", feature = "net"))]
4232
use libc_errno::errno;
33+
#[cfg(any(target_os = "android", target_os = "linux"))]
34+
use {
35+
super::super::conv::syscall_ret_owned_fd,
36+
crate::io::{IoSliceRaw, ReadWriteFlags, SpliceFlags},
37+
core::ptr,
38+
};
39+
#[cfg(bsd)]
40+
use {crate::io::kqueue::Event, crate::utils::as_ptr, core::ptr::null};
41+
#[cfg(solarish)]
42+
use {crate::io::port::Event, crate::utils::as_mut_ptr, core::ptr::null_mut};
4343

4444
pub(crate) fn read(fd: BorrowedFd<'_>, buf: &mut [u8]) -> io::Result<usize> {
4545
unsafe {
@@ -531,17 +531,17 @@ pub(crate) unsafe fn kevent(
531531
) -> io::Result<c::c_int> {
532532
ret_c_int(c::kevent(
533533
borrowed_fd(kq),
534-
changelist.as_ptr() as *const _,
534+
changelist.as_ptr().cast(),
535535
changelist
536536
.len()
537537
.try_into()
538538
.map_err(|_| io::Errno::OVERFLOW)?,
539-
eventlist.as_mut_ptr() as *mut _,
539+
eventlist.as_mut_ptr().cast(),
540540
eventlist
541541
.len()
542542
.try_into()
543543
.map_err(|_| io::Errno::OVERFLOW)?,
544-
timeout.map_or(core::ptr::null(), |t| t as *const _),
544+
timeout.map_or(null(), as_ptr),
545545
))
546546
}
547547

@@ -658,7 +658,7 @@ pub(crate) fn port_get(
658658
timeout: Option<&mut c::timespec>,
659659
) -> io::Result<Event> {
660660
let mut event = MaybeUninit::<c::port_event>::uninit();
661-
let timeout = timeout.map_or(core::ptr::null_mut(), |t| t as *mut _);
661+
let timeout = timeout.map_or(null_mut(), as_mut_ptr);
662662

663663
unsafe {
664664
ret(c::port_get(borrowed_fd(port), event.as_mut_ptr(), timeout))?;
@@ -675,7 +675,7 @@ pub(crate) fn port_getn(
675675
events: &mut Vec<Event>,
676676
mut nget: u32,
677677
) -> io::Result<()> {
678-
let timeout = timeout.map_or(core::ptr::null_mut(), |t| t as *mut _);
678+
let timeout = timeout.map_or(null_mut(), as_mut_ptr);
679679
unsafe {
680680
ret(c::port_getn(
681681
borrowed_fd(port),

src/backend/libc/net/addr.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
//! IPv4, IPv6, and Socket addresses.
1+
//! Socket address utilities.
22
33
use super::super::c;
4-
#[cfg(unix)]
5-
use crate::ffi::CStr;
6-
#[cfg(unix)]
7-
use crate::io;
8-
#[cfg(unix)]
9-
use crate::path;
104
#[cfg(not(windows))]
115
use core::convert::TryInto;
126
#[cfg(unix)]
13-
use core::fmt;
14-
#[cfg(unix)]
15-
use core::slice;
7+
use {
8+
crate::ffi::CStr,
9+
crate::io,
10+
crate::path,
11+
core::cmp::Ordering,
12+
core::fmt,
13+
core::hash::{Hash, Hasher},
14+
core::slice,
15+
};
1616

1717
/// `struct sockaddr_un`
1818
#[cfg(unix)]
@@ -159,7 +159,7 @@ impl Eq for SocketAddrUnix {}
159159
#[cfg(unix)]
160160
impl PartialOrd for SocketAddrUnix {
161161
#[inline]
162-
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
162+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
163163
let self_len = self.len() - offsetof_sun_path();
164164
let other_len = other.len() - offsetof_sun_path();
165165
self.unix.sun_path[..self_len].partial_cmp(&other.unix.sun_path[..other_len])
@@ -169,17 +169,17 @@ impl PartialOrd for SocketAddrUnix {
169169
#[cfg(unix)]
170170
impl Ord for SocketAddrUnix {
171171
#[inline]
172-
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
172+
fn cmp(&self, other: &Self) -> Ordering {
173173
let self_len = self.len() - offsetof_sun_path();
174174
let other_len = other.len() - offsetof_sun_path();
175175
self.unix.sun_path[..self_len].cmp(&other.unix.sun_path[..other_len])
176176
}
177177
}
178178

179179
#[cfg(unix)]
180-
impl core::hash::Hash for SocketAddrUnix {
180+
impl Hash for SocketAddrUnix {
181181
#[inline]
182-
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
182+
fn hash<H: Hasher>(&self, state: &mut H) {
183183
let self_len = self.len() - offsetof_sun_path();
184184
self.unix.sun_path[..self_len].hash(state)
185185
}

src/backend/libc/net/msghdr.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use super::super::c;
77
use super::super::conv::{msg_control_len, msg_iov_len};
88
use super::super::net::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
99

10-
use crate::io;
11-
use crate::net::{SocketAddrV4, SocketAddrV6};
10+
use crate::io::{IoSlice, IoSliceMut};
11+
use crate::net::{RecvAncillaryBuffer, SendAncillaryBuffer, SocketAddrV4, SocketAddrV6};
1212
use crate::utils::as_ptr;
1313

1414
use core::convert::TryInto;
@@ -17,8 +17,8 @@ use core::mem::{size_of, zeroed, MaybeUninit};
1717
/// Create a message header intended to receive a datagram.
1818
pub(crate) fn with_recv_msghdr<R>(
1919
name: &mut MaybeUninit<c::sockaddr_storage>,
20-
iov: &mut [io::IoSliceMut<'_>],
21-
control: &mut crate::net::RecvAncillaryBuffer<'_>,
20+
iov: &mut [IoSliceMut<'_>],
21+
control: &mut RecvAncillaryBuffer<'_>,
2222
f: impl FnOnce(&mut c::msghdr) -> R,
2323
) -> R {
2424
let namelen = size_of::<c::sockaddr_storage>() as c::socklen_t;
@@ -45,8 +45,8 @@ pub(crate) fn with_recv_msghdr<R>(
4545

4646
/// Create a message header intended to send without an address.
4747
pub(crate) fn with_noaddr_msghdr<R>(
48-
iov: &[io::IoSlice<'_>],
49-
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
48+
iov: &[IoSlice<'_>],
49+
control: &mut SendAncillaryBuffer<'_, '_, '_>,
5050
f: impl FnOnce(c::msghdr) -> R,
5151
) -> R {
5252
f({
@@ -62,8 +62,8 @@ pub(crate) fn with_noaddr_msghdr<R>(
6262
/// Create a message header intended to send with an IPv4 address.
6363
pub(crate) fn with_v4_msghdr<R>(
6464
addr: &SocketAddrV4,
65-
iov: &[io::IoSlice<'_>],
66-
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
65+
iov: &[IoSlice<'_>],
66+
control: &mut SendAncillaryBuffer<'_, '_, '_>,
6767
f: impl FnOnce(c::msghdr) -> R,
6868
) -> R {
6969
let encoded = unsafe { encode_sockaddr_v4(addr) };
@@ -83,8 +83,8 @@ pub(crate) fn with_v4_msghdr<R>(
8383
/// Create a message header intended to send with an IPv6 address.
8484
pub(crate) fn with_v6_msghdr<R>(
8585
addr: &SocketAddrV6,
86-
iov: &[io::IoSlice<'_>],
87-
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
86+
iov: &[IoSlice<'_>],
87+
control: &mut SendAncillaryBuffer<'_, '_, '_>,
8888
f: impl FnOnce(c::msghdr) -> R,
8989
) -> R {
9090
let encoded = unsafe { encode_sockaddr_v6(addr) };
@@ -105,8 +105,8 @@ pub(crate) fn with_v6_msghdr<R>(
105105
#[cfg(all(unix, not(target_os = "redox")))]
106106
pub(crate) fn with_unix_msghdr<R>(
107107
addr: &crate::net::SocketAddrUnix,
108-
iov: &[io::IoSlice<'_>],
109-
control: &mut crate::net::SendAncillaryBuffer<'_, '_, '_>,
108+
iov: &[IoSlice<'_>],
109+
control: &mut SendAncillaryBuffer<'_, '_, '_>,
110110
f: impl FnOnce(c::msghdr) -> R,
111111
) -> R {
112112
f({

src/backend/libc/net/syscalls.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,26 @@ use super::super::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_rec
55
#[cfg(unix)]
66
use super::addr::SocketAddrUnix;
77
use super::ext::{in6_addr_new, in_addr_new};
8-
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
9-
use super::msghdr::{with_noaddr_msghdr, with_recv_msghdr, with_v4_msghdr, with_v6_msghdr};
10-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
11-
use super::read_sockaddr::initialize_family_to_unspec;
12-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
13-
use super::read_sockaddr::{maybe_read_sockaddr_os, read_sockaddr_os};
14-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
15-
use super::send_recv::{RecvFlags, SendFlags};
16-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
17-
use super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType};
18-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
19-
use super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6};
208
use crate::fd::{BorrowedFd, OwnedFd};
219
use crate::io;
2210
use crate::net::{SocketAddrAny, SocketAddrV4, SocketAddrV6};
2311
use crate::utils::as_ptr;
2412
use core::convert::TryInto;
2513
use core::mem::{size_of, MaybeUninit};
26-
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
27-
use core::ptr::null_mut;
2814
#[cfg(not(any(windows, target_os = "redox", target_os = "wasi")))]
2915
use {
16+
super::msghdr::{with_noaddr_msghdr, with_recv_msghdr, with_v4_msghdr, with_v6_msghdr},
3017
crate::io::{IoSlice, IoSliceMut},
3118
crate::net::{RecvAncillaryBuffer, RecvMsgReturn, SendAncillaryBuffer},
3219
};
20+
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
21+
use {
22+
super::read_sockaddr::{initialize_family_to_unspec, maybe_read_sockaddr_os, read_sockaddr_os},
23+
super::send_recv::{RecvFlags, SendFlags},
24+
super::types::{AddressFamily, Protocol, Shutdown, SocketFlags, SocketType},
25+
super::write_sockaddr::{encode_sockaddr_v4, encode_sockaddr_v6},
26+
core::ptr::null_mut,
27+
};
3328

3429
#[cfg(not(any(target_os = "redox", target_os = "wasi")))]
3530
pub(crate) fn recv(fd: BorrowedFd<'_>, buf: &mut [u8], flags: RecvFlags) -> io::Result<usize> {

src/backend/libc/termios/syscalls.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ use crate::fd::BorrowedFd;
1010
#[cfg(feature = "procfs")]
1111
#[cfg(not(any(target_os = "fuchsia", target_os = "wasi")))]
1212
use crate::ffi::CStr;
13-
#[cfg(not(target_os = "wasi"))]
14-
use crate::io;
15-
#[cfg(not(target_os = "wasi"))]
16-
use crate::process::{Pid, RawNonZeroPid};
17-
#[cfg(not(target_os = "wasi"))]
18-
use crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize};
1913
use core::mem::MaybeUninit;
14+
#[cfg(not(target_os = "wasi"))]
15+
use {
16+
crate::io,
17+
crate::process::{Pid, RawNonZeroPid},
18+
crate::termios::{Action, OptionalActions, QueueSelector, Speed, Termios, Winsize},
19+
};
2020

2121
#[cfg(not(target_os = "wasi"))]
2222
pub(crate) fn tcgetattr(fd: BorrowedFd<'_>) -> io::Result<Termios> {

0 commit comments

Comments
 (0)