1
- use std:: ptr:: null_mut;
1
+ use std:: {
2
+ mem:: { align_of, size_of} ,
3
+ ptr:: null_mut,
4
+ } ;
2
5
3
6
use windows_sys:: Win32 :: Networking :: WinSock :: { CMSGHDR , WSABUF , WSAMSG } ;
4
7
5
8
// Macros from https://github.com/microsoft/win32metadata/blob/main/generation/WinSDK/RecompiledIdlHeaders/shared/ws2def.h
6
9
#[ inline]
7
10
const fn wsa_cmsghdr_align ( length : usize ) -> usize {
8
- ( length + std :: mem :: align_of :: < CMSGHDR > ( ) - 1 ) & !( std :: mem :: align_of :: < CMSGHDR > ( ) - 1 )
11
+ ( length + align_of :: < CMSGHDR > ( ) - 1 ) & !( align_of :: < CMSGHDR > ( ) - 1 )
9
12
}
10
13
11
14
// WSA_CMSGDATA_ALIGN(sizeof(CMSGHDR))
12
15
const WSA_CMSGDATA_OFFSET : usize =
13
- ( std :: mem :: size_of :: < CMSGHDR > ( ) + std :: mem :: align_of :: < usize > ( ) - 1 ) & !( std :: mem :: align_of :: < usize > ( ) - 1 ) ;
16
+ ( size_of :: < CMSGHDR > ( ) + align_of :: < usize > ( ) - 1 ) & !( align_of :: < usize > ( ) - 1 ) ;
14
17
15
18
#[ inline]
16
19
unsafe fn wsa_cmsg_firsthdr ( msg : * const WSAMSG ) -> * mut CMSGHDR {
17
- if ( * msg) . Control . len as usize >= std :: mem :: size_of :: < CMSGHDR > ( ) {
20
+ if ( * msg) . Control . len as usize >= size_of :: < CMSGHDR > ( ) {
18
21
( * msg) . Control . buf as _
19
22
} else {
20
23
null_mut ( )
@@ -27,9 +30,7 @@ unsafe fn wsa_cmsg_nxthdr(msg: *const WSAMSG, cmsg: *const CMSGHDR) -> *mut CMSG
27
30
wsa_cmsg_firsthdr ( msg)
28
31
} else {
29
32
let next = cmsg as usize + wsa_cmsghdr_align ( ( * cmsg) . cmsg_len ) ;
30
- if next + std:: mem:: size_of :: < CMSGHDR > ( )
31
- > ( * msg) . Control . buf as usize + ( * msg) . Control . len as usize
32
- {
33
+ if next + size_of :: < CMSGHDR > ( ) > ( * msg) . Control . buf as usize + ( * msg) . Control . len as usize {
33
34
null_mut ( )
34
35
} else {
35
36
next as _
@@ -52,32 +53,21 @@ const fn wsa_cmsg_len(length: usize) -> usize {
52
53
WSA_CMSGDATA_OFFSET + length
53
54
}
54
55
55
- /// Reference to a control message.
56
56
pub struct CMsgRef < ' a > ( & ' a CMSGHDR ) ;
57
57
58
58
impl < ' a > CMsgRef < ' a > {
59
- /// Returns the level of the control message.
60
59
pub fn level ( & self ) -> i32 {
61
60
self . 0 . cmsg_level
62
61
}
63
62
64
- /// Returns the type of the control message.
65
63
pub fn ty ( & self ) -> i32 {
66
64
self . 0 . cmsg_type
67
65
}
68
66
69
- /// Returns the length of the control message.
70
- #[ allow( clippy:: len_without_is_empty) ]
71
67
pub fn len ( & self ) -> usize {
72
68
self . 0 . cmsg_len
73
69
}
74
70
75
- /// Returns a reference to the data of the control message.
76
- ///
77
- /// # Safety
78
- ///
79
- /// The data part must be properly aligned and contains an initialized
80
- /// instance of `T`.
81
71
pub unsafe fn data < T > ( & self ) -> & T {
82
72
let data_ptr = wsa_cmsg_data ( self . 0 ) ;
83
73
data_ptr. cast :: < T > ( ) . as_ref ( ) . unwrap ( )
@@ -95,10 +85,11 @@ impl<'a> CMsgMut<'a> {
95
85
self . 0 . cmsg_type = ty;
96
86
}
97
87
98
- pub ( crate ) unsafe fn set_data < T > ( & mut self , data : T ) {
99
- self . 0 . cmsg_len = wsa_cmsg_len ( std :: mem :: size_of :: < T > ( ) as _ ) as _ ;
88
+ pub ( crate ) unsafe fn set_data < T > ( & mut self , data : T ) -> usize {
89
+ self . 0 . cmsg_len = wsa_cmsg_len ( size_of :: < T > ( ) as _ ) as _ ;
100
90
let data_ptr = wsa_cmsg_data ( self . 0 ) ;
101
91
std:: ptr:: write ( data_ptr. cast :: < T > ( ) , data) ;
92
+ wsa_cmsg_space ( size_of :: < T > ( ) as _ )
102
93
}
103
94
}
104
95
@@ -142,15 +133,11 @@ impl CMsgIter {
142
133
143
134
pub ( crate ) fn is_space_enough < T > ( & self ) -> bool {
144
135
if !self . cmsg . is_null ( ) {
145
- let space = wsa_cmsg_space ( std :: mem :: size_of :: < T > ( ) as _ ) ;
136
+ let space = wsa_cmsg_space ( size_of :: < T > ( ) as _ ) ;
146
137
let max = self . msg . Control . buf as usize + self . msg . Control . len as usize ;
147
138
self . cmsg as usize + space <= max
148
139
} else {
149
140
false
150
141
}
151
142
}
152
143
}
153
-
154
- pub ( crate ) fn space_of < T > ( ) -> usize {
155
- wsa_cmsg_space ( std:: mem:: size_of :: < T > ( ) as _ )
156
- }
0 commit comments