Skip to content

Commit a5dce6c

Browse files
authored
Rollup merge of #86357 - de-vri-es:simplify-repeated-cfg-ifs, r=m-ou-se
Rely on libc for correct integer types in os/unix/net/ancillary.rs. This PR is a small maintainability improvement. It simplifies `unix/net/ancillary.rs` in `std` by removing the `cfg_ifs` for casting to the correct integer type, and just rely on libc to define the struct correctly.
2 parents b7dd942 + 259bf5f commit a5dce6c

File tree

1 file changed

+10
-100
lines changed

1 file changed

+10
-100
lines changed

library/std/src/os/unix/net/ancillary.rs

+10-100
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,8 @@ pub(super) fn recv_vectored_with_ancillary_from(
3232
msg.msg_name = &mut msg_name as *mut _ as *mut _;
3333
msg.msg_namelen = size_of::<libc::sockaddr_un>() as libc::socklen_t;
3434
msg.msg_iov = bufs.as_mut_ptr().cast();
35-
cfg_if::cfg_if! {
36-
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
37-
msg.msg_iovlen = bufs.len() as libc::size_t;
38-
msg.msg_controllen = ancillary.buffer.len() as libc::size_t;
39-
} else if #[cfg(any(
40-
target_os = "dragonfly",
41-
target_os = "emscripten",
42-
target_os = "freebsd",
43-
all(target_os = "linux", target_env = "musl",),
44-
target_os = "macos",
45-
target_os = "netbsd",
46-
target_os = "openbsd",
47-
))] {
48-
msg.msg_iovlen = bufs.len() as libc::c_int;
49-
msg.msg_controllen = ancillary.buffer.len() as libc::socklen_t;
50-
}
51-
}
35+
msg.msg_iovlen = bufs.len() as _;
36+
msg.msg_controllen = ancillary.buffer.len() as _;
5237
// macos requires that the control pointer is null when the len is 0.
5338
if msg.msg_controllen > 0 {
5439
msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
@@ -80,23 +65,8 @@ pub(super) fn send_vectored_with_ancillary_to(
8065
msg.msg_name = &mut msg_name as *mut _ as *mut _;
8166
msg.msg_namelen = msg_namelen;
8267
msg.msg_iov = bufs.as_ptr() as *mut _;
83-
cfg_if::cfg_if! {
84-
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
85-
msg.msg_iovlen = bufs.len() as libc::size_t;
86-
msg.msg_controllen = ancillary.length as libc::size_t;
87-
} else if #[cfg(any(
88-
target_os = "dragonfly",
89-
target_os = "emscripten",
90-
target_os = "freebsd",
91-
all(target_os = "linux", target_env = "musl",),
92-
target_os = "macos",
93-
target_os = "netbsd",
94-
target_os = "openbsd",
95-
))] {
96-
msg.msg_iovlen = bufs.len() as libc::c_int;
97-
msg.msg_controllen = ancillary.length as libc::socklen_t;
98-
}
99-
}
68+
msg.msg_iovlen = bufs.len() as _;
69+
msg.msg_controllen = ancillary.length as _;
10070
// macos requires that the control pointer is null when the len is 0.
10171
if msg.msg_controllen > 0 {
10272
msg.msg_control = ancillary.buffer.as_mut_ptr().cast();
@@ -144,21 +114,7 @@ fn add_to_ancillary_data<T>(
144114

145115
let mut msg: libc::msghdr = zeroed();
146116
msg.msg_control = buffer.as_mut_ptr().cast();
147-
cfg_if::cfg_if! {
148-
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
149-
msg.msg_controllen = *length as libc::size_t;
150-
} else if #[cfg(any(
151-
target_os = "dragonfly",
152-
target_os = "emscripten",
153-
target_os = "freebsd",
154-
all(target_os = "linux", target_env = "musl",),
155-
target_os = "macos",
156-
target_os = "netbsd",
157-
target_os = "openbsd",
158-
))] {
159-
msg.msg_controllen = *length as libc::socklen_t;
160-
}
161-
}
117+
msg.msg_controllen = *length as _;
162118

163119
let mut cmsg = libc::CMSG_FIRSTHDR(&msg);
164120
let mut previous_cmsg = cmsg;
@@ -180,21 +136,7 @@ fn add_to_ancillary_data<T>(
180136

181137
(*previous_cmsg).cmsg_level = cmsg_level;
182138
(*previous_cmsg).cmsg_type = cmsg_type;
183-
cfg_if::cfg_if! {
184-
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
185-
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as libc::size_t;
186-
} else if #[cfg(any(
187-
target_os = "dragonfly",
188-
target_os = "emscripten",
189-
target_os = "freebsd",
190-
all(target_os = "linux", target_env = "musl",),
191-
target_os = "macos",
192-
target_os = "netbsd",
193-
target_os = "openbsd",
194-
))] {
195-
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as libc::socklen_t;
196-
}
197-
}
139+
(*previous_cmsg).cmsg_len = libc::CMSG_LEN(source_len) as _;
198140

199141
let data = libc::CMSG_DATA(previous_cmsg).cast();
200142

@@ -364,28 +306,10 @@ impl<'a> AncillaryData<'a> {
364306

365307
fn try_from_cmsghdr(cmsg: &'a libc::cmsghdr) -> Result<Self, AncillaryError> {
366308
unsafe {
367-
cfg_if::cfg_if! {
368-
if #[cfg(any(
369-
target_os = "android",
370-
all(target_os = "linux", target_env = "gnu"),
371-
all(target_os = "linux", target_env = "uclibc"),
372-
))] {
373-
let cmsg_len_zero = libc::CMSG_LEN(0) as libc::size_t;
374-
} else if #[cfg(any(
375-
target_os = "dragonfly",
376-
target_os = "emscripten",
377-
target_os = "freebsd",
378-
all(target_os = "linux", target_env = "musl",),
379-
target_os = "macos",
380-
target_os = "netbsd",
381-
target_os = "openbsd",
382-
))] {
383-
let cmsg_len_zero = libc::CMSG_LEN(0) as libc::socklen_t;
384-
}
385-
}
386-
let data_len = (*cmsg).cmsg_len - cmsg_len_zero;
309+
let cmsg_len_zero = libc::CMSG_LEN(0) as usize;
310+
let data_len = (*cmsg).cmsg_len as usize - cmsg_len_zero;
387311
let data = libc::CMSG_DATA(cmsg).cast();
388-
let data = from_raw_parts(data, data_len as usize);
312+
let data = from_raw_parts(data, data_len);
389313

390314
match (*cmsg).cmsg_level {
391315
libc::SOL_SOCKET => match (*cmsg).cmsg_type {
@@ -419,21 +343,7 @@ impl<'a> Iterator for Messages<'a> {
419343
unsafe {
420344
let mut msg: libc::msghdr = zeroed();
421345
msg.msg_control = self.buffer.as_ptr() as *mut _;
422-
cfg_if::cfg_if! {
423-
if #[cfg(any(target_os = "android", all(target_os = "linux", target_env = "gnu")))] {
424-
msg.msg_controllen = self.buffer.len() as libc::size_t;
425-
} else if #[cfg(any(
426-
target_os = "dragonfly",
427-
target_os = "emscripten",
428-
target_os = "freebsd",
429-
all(target_os = "linux", target_env = "musl",),
430-
target_os = "macos",
431-
target_os = "netbsd",
432-
target_os = "openbsd",
433-
))] {
434-
msg.msg_controllen = self.buffer.len() as libc::socklen_t;
435-
}
436-
}
346+
msg.msg_controllen = self.buffer.len() as _;
437347

438348
let cmsg = if let Some(current) = self.current {
439349
libc::CMSG_NXTHDR(&msg, current)

0 commit comments

Comments
 (0)