Skip to content

Commit baac3f1

Browse files
committed
chore(driver): simplify cmsg
1 parent 7c59655 commit baac3f1

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

compio-net/src/cmsg/mod.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,7 @@ impl<B> CMsgBuilder<B> {
7575
/// properly aligned and has enough space, safety conditions of all unsafe
7676
/// functions involved are satisfied, except for `CMSG_*`/`wsa_cmsg_*`, as
7777
/// their safety are not documented.
78-
pub unsafe fn try_push<T>(
79-
&mut self,
80-
level: sys::c_int,
81-
ty: sys::c_int,
82-
value: T,
83-
) -> Option<()> {
78+
pub unsafe fn try_push<T>(&mut self, level: i32, ty: i32, value: T) -> Option<()> {
8479
if !self.inner.is_aligned::<T>() || !self.inner.is_space_enough::<T>() {
8580
return None;
8681
}

compio-net/src/cmsg/unix.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::mem;
2-
3-
pub use libc::c_int;
4-
use libc::{cmsghdr, msghdr, CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE};
1+
use libc::{c_int, cmsghdr, msghdr, CMSG_DATA, CMSG_FIRSTHDR, CMSG_LEN, CMSG_NXTHDR, CMSG_SPACE};
52

63
/// Reference to a control message.
74
pub struct CMsgRef<'a>(&'a cmsghdr);
@@ -47,7 +44,7 @@ impl<'a> CMsgMut<'a> {
4744
}
4845

4946
pub(crate) unsafe fn set_data<T>(&mut self, data: T) {
50-
self.0.cmsg_len = CMSG_LEN(mem::size_of::<T>() as _) as _;
47+
self.0.cmsg_len = CMSG_LEN(std::mem::size_of::<T>() as _) as _;
5148
let data_ptr = CMSG_DATA(self.0);
5249
std::ptr::write(data_ptr.cast::<T>(), data);
5350
}
@@ -63,7 +60,7 @@ impl CMsgIter {
6360
assert!(len >= unsafe { CMSG_SPACE(0) as _ }, "buffer too short");
6461
assert!(ptr.cast::<cmsghdr>().is_aligned(), "misaligned buffer");
6562

66-
let mut msg: msghdr = unsafe { mem::zeroed() };
63+
let mut msg: msghdr = unsafe { std::mem::zeroed() };
6764
msg.msg_control = ptr as _;
6865
msg.msg_controllen = len as _;
6966
// SAFETY: msg is initialized and valid
@@ -91,7 +88,7 @@ impl CMsgIter {
9188

9289
pub(crate) fn is_space_enough<T>(&self) -> bool {
9390
if !self.cmsg.is_null() {
94-
let space = unsafe { CMSG_SPACE(mem::size_of::<T>() as _) as usize };
91+
let space = unsafe { CMSG_SPACE(std::mem::size_of::<T>() as _) as usize };
9592
#[allow(clippy::unnecessary_cast)]
9693
let max = self.msg.msg_control as usize + self.msg.msg_controllen as usize;
9794
self.cmsg as usize + space <= max
@@ -102,5 +99,5 @@ impl CMsgIter {
10299
}
103100

104101
pub(crate) fn space_of<T>() -> usize {
105-
unsafe { CMSG_SPACE(mem::size_of::<T>() as _) as _ }
102+
unsafe { CMSG_SPACE(std::mem::size_of::<T>() as _) as _ }
106103
}

compio-net/src/cmsg/windows.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
use std::{mem, ptr::null_mut};
1+
use std::ptr::null_mut;
22

3-
pub use i32 as c_int;
43
use windows_sys::Win32::Networking::WinSock::{CMSGHDR, WSABUF, WSAMSG};
54

65
// Macros from https://github.com/microsoft/win32metadata/blob/main/generation/WinSDK/RecompiledIdlHeaders/shared/ws2def.h
76
#[inline]
87
const fn wsa_cmsghdr_align(length: usize) -> usize {
9-
(length + mem::align_of::<CMSGHDR>() - 1) & !(mem::align_of::<CMSGHDR>() - 1)
8+
(length + std::mem::align_of::<CMSGHDR>() - 1) & !(std::mem::align_of::<CMSGHDR>() - 1)
109
}
1110

1211
// WSA_CMSGDATA_ALIGN(sizeof(CMSGHDR))
1312
const WSA_CMSGDATA_OFFSET: usize =
14-
(mem::size_of::<CMSGHDR>() + mem::align_of::<usize>() - 1) & !(mem::align_of::<usize>() - 1);
13+
(std::mem::size_of::<CMSGHDR>() + std::mem::align_of::<usize>() - 1) & !(std::mem::align_of::<usize>() - 1);
1514

1615
#[inline]
1716
unsafe fn wsa_cmsg_firsthdr(msg: *const WSAMSG) -> *mut CMSGHDR {
18-
if (*msg).Control.len as usize >= mem::size_of::<CMSGHDR>() {
17+
if (*msg).Control.len as usize >= std::mem::size_of::<CMSGHDR>() {
1918
(*msg).Control.buf as _
2019
} else {
2120
null_mut()
@@ -28,7 +27,7 @@ unsafe fn wsa_cmsg_nxthdr(msg: *const WSAMSG, cmsg: *const CMSGHDR) -> *mut CMSG
2827
wsa_cmsg_firsthdr(msg)
2928
} else {
3029
let next = cmsg as usize + wsa_cmsghdr_align((*cmsg).cmsg_len);
31-
if next + mem::size_of::<CMSGHDR>()
30+
if next + std::mem::size_of::<CMSGHDR>()
3231
> (*msg).Control.buf as usize + (*msg).Control.len as usize
3332
{
3433
null_mut()
@@ -97,7 +96,7 @@ impl<'a> CMsgMut<'a> {
9796
}
9897

9998
pub(crate) unsafe fn set_data<T>(&mut self, data: T) {
100-
self.0.cmsg_len = wsa_cmsg_len(mem::size_of::<T>() as _) as _;
99+
self.0.cmsg_len = wsa_cmsg_len(std::mem::size_of::<T>() as _) as _;
101100
let data_ptr = wsa_cmsg_data(self.0);
102101
std::ptr::write(data_ptr.cast::<T>(), data);
103102
}
@@ -113,7 +112,7 @@ impl CMsgIter {
113112
assert!(len >= wsa_cmsg_space(0) as _, "buffer too short");
114113
assert!(ptr.cast::<CMSGHDR>().is_aligned(), "misaligned buffer");
115114

116-
let mut msg: WSAMSG = unsafe { mem::zeroed() };
115+
let mut msg: WSAMSG = unsafe { std::mem::zeroed() };
117116
msg.Control = WSABUF {
118117
len: len as _,
119118
buf: ptr as _,
@@ -143,7 +142,7 @@ impl CMsgIter {
143142

144143
pub(crate) fn is_space_enough<T>(&self) -> bool {
145144
if !self.cmsg.is_null() {
146-
let space = wsa_cmsg_space(mem::size_of::<T>() as _);
145+
let space = wsa_cmsg_space(std::mem::size_of::<T>() as _);
147146
let max = self.msg.Control.buf as usize + self.msg.Control.len as usize;
148147
self.cmsg as usize + space <= max
149148
} else {
@@ -153,5 +152,5 @@ impl CMsgIter {
153152
}
154153

155154
pub(crate) fn space_of<T>() -> usize {
156-
wsa_cmsg_space(mem::size_of::<T>() as _)
155+
wsa_cmsg_space(std::mem::size_of::<T>() as _)
157156
}

0 commit comments

Comments
 (0)