@@ -8,9 +8,8 @@ use crate::net::{Shutdown, SocketAddr};
8
8
use crate :: os:: windows:: io:: {
9
9
AsRawSocket , AsSocket , BorrowedSocket , FromRawSocket , IntoRawSocket , OwnedSocket , RawSocket ,
10
10
} ;
11
- use crate :: sync:: atomic:: Atomic ;
12
- use crate :: sync:: atomic:: Ordering :: { AcqRel , Relaxed } ;
13
11
use crate :: sys:: c;
12
+ use crate :: sys:: pal:: winsock:: last_error;
14
13
use crate :: sys_common:: { AsInner , FromInner , IntoInner } ;
15
14
use crate :: time:: Duration ;
16
15
use crate :: { cmp, mem, ptr, sys} ;
@@ -112,84 +111,11 @@ pub(super) mod netc {
112
111
}
113
112
}
114
113
114
+ pub use crate :: sys:: pal:: winsock:: { cleanup, cvt, cvt_gai, cvt_r, startup as init} ;
115
+
115
116
#[ expect( missing_debug_implementations) ]
116
117
pub struct Socket ( OwnedSocket ) ;
117
118
118
- static WSA_INITIALIZED : Atomic < bool > = Atomic :: < bool > :: new ( false ) ;
119
-
120
- /// Checks whether the Windows socket interface has been started already, and
121
- /// if not, starts it.
122
- #[ inline]
123
- pub fn init ( ) {
124
- if !WSA_INITIALIZED . load ( Relaxed ) {
125
- wsa_startup ( ) ;
126
- }
127
- }
128
-
129
- #[ cold]
130
- fn wsa_startup ( ) {
131
- unsafe {
132
- let mut data: c:: WSADATA = mem:: zeroed ( ) ;
133
- let ret = c:: WSAStartup (
134
- 0x202 , // version 2.2
135
- & mut data,
136
- ) ;
137
- assert_eq ! ( ret, 0 ) ;
138
- if WSA_INITIALIZED . swap ( true , AcqRel ) {
139
- // If another thread raced with us and called WSAStartup first then call
140
- // WSACleanup so it's as though WSAStartup was only called once.
141
- c:: WSACleanup ( ) ;
142
- }
143
- }
144
- }
145
-
146
- pub fn cleanup ( ) {
147
- // We don't need to call WSACleanup here because exiting the process will cause
148
- // the OS to clean everything for us, which is faster than doing it manually.
149
- // See #141799.
150
- }
151
-
152
- /// Returns the last error from the Windows socket interface.
153
- fn last_error ( ) -> io:: Error {
154
- io:: Error :: from_raw_os_error ( unsafe { c:: WSAGetLastError ( ) } )
155
- }
156
-
157
- #[ doc( hidden) ]
158
- pub trait IsMinusOne {
159
- fn is_minus_one ( & self ) -> bool ;
160
- }
161
-
162
- macro_rules! impl_is_minus_one {
163
- ( $( $t: ident) * ) => ( $( impl IsMinusOne for $t {
164
- fn is_minus_one( & self ) -> bool {
165
- * self == -1
166
- }
167
- } ) * )
168
- }
169
-
170
- impl_is_minus_one ! { i8 i16 i32 i64 isize }
171
-
172
- /// Checks if the signed integer is the Windows constant `SOCKET_ERROR` (-1)
173
- /// and if so, returns the last error from the Windows socket interface. This
174
- /// function must be called before another call to the socket API is made.
175
- pub fn cvt < T : IsMinusOne > ( t : T ) -> io:: Result < T > {
176
- if t. is_minus_one ( ) { Err ( last_error ( ) ) } else { Ok ( t) }
177
- }
178
-
179
- /// A variant of `cvt` for `getaddrinfo` which return 0 for a success.
180
- pub fn cvt_gai ( err : c_int ) -> io:: Result < ( ) > {
181
- if err == 0 { Ok ( ( ) ) } else { Err ( last_error ( ) ) }
182
- }
183
-
184
- /// Just to provide the same interface as sys/pal/unix/net.rs
185
- pub fn cvt_r < T , F > ( mut f : F ) -> io:: Result < T >
186
- where
187
- T : IsMinusOne ,
188
- F : FnMut ( ) -> T ,
189
- {
190
- cvt ( f ( ) )
191
- }
192
-
193
119
impl Socket {
194
120
pub fn new ( addr : & SocketAddr , ty : c_int ) -> io:: Result < Socket > {
195
121
let family = match * addr {
0 commit comments