@@ -5,6 +5,7 @@ use std::pin::Pin;
5
5
use std:: task:: { ready, Context , Poll } ;
6
6
7
7
use bytes:: BufMut ;
8
+ use cfg_if:: cfg_if;
8
9
9
10
pub use buffered:: { BufferedSocket , WriteBuffer } ;
10
11
@@ -202,88 +203,103 @@ pub async fn connect_tcp<Ws: WithSocket>(
202
203
return Ok ( with_socket. with_socket ( stream) . await ) ;
203
204
}
204
205
205
- #[ cfg( any( feature = "_rt-async-global-executor" , feature = "_rt-smol" ) ) ]
206
- {
207
- #[ cfg( feature = "_rt-async-global-executor" ) ]
208
- use async_io_global_executor:: Async ;
209
- #[ cfg( feature = "_rt-async-global-executor" ) ]
210
- use async_net:: resolve;
211
- #[ cfg( feature = "_rt-smol" ) ]
212
- use smol:: Async ;
213
- #[ cfg( feature = "_rt-smol" ) ]
214
- use smol:: net:: resolve;
215
- use std:: net:: TcpStream ;
216
-
217
- let mut last_err = None ;
218
-
219
- // Loop through all the Socket Addresses that the hostname resolves to
220
- for socket_addr in resolve ( ( host, port) ) . await ? {
221
- let stream = Async :: < TcpStream > :: connect ( socket_addr)
222
- . await
223
- . and_then ( |s| {
224
- s. get_ref ( ) . set_nodelay ( true ) ?;
225
- Ok ( s)
226
- } ) ;
227
- match stream {
228
- Ok ( stream) => return Ok ( with_socket. with_socket ( stream) . await ) ,
229
- Err ( e) => last_err = Some ( e) ,
206
+ cfg_if ! {
207
+ if #[ cfg( feature = "_rt-async-global-executor" ) ] {
208
+ use async_io_global_executor:: Async ;
209
+ use async_net:: resolve;
210
+ use std:: net:: TcpStream ;
211
+
212
+ let mut last_err = None ;
213
+
214
+ // Loop through all the Socket Addresses that the hostname resolves to
215
+ for socket_addr in resolve( ( host, port) ) . await ? {
216
+ let stream = Async :: <TcpStream >:: connect( socket_addr)
217
+ . await
218
+ . and_then( |s| {
219
+ s. get_ref( ) . set_nodelay( true ) ?;
220
+ Ok ( s)
221
+ } ) ;
222
+ match stream {
223
+ Ok ( stream) => return Ok ( with_socket. with_socket( stream) . await ) ,
224
+ Err ( e) => last_err = Some ( e) ,
225
+ }
230
226
}
231
- }
232
227
233
- // If we reach this point, it means we failed to connect to any of the addresses.
234
- // Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
235
- return match last_err {
236
- Some ( err) => Err ( err. into ( ) ) ,
237
- None => Err ( io:: Error :: new (
238
- io:: ErrorKind :: AddrNotAvailable ,
239
- "Hostname did not resolve to any addresses" ,
240
- )
241
- . into ( ) ) ,
242
- } ;
243
- }
244
-
245
- #[ cfg( feature = "_rt-async-std" ) ]
246
- {
247
- use async_io_std:: Async ;
248
- use async_std:: net:: ToSocketAddrs ;
249
- use std:: net:: TcpStream ;
250
-
251
- let mut last_err = None ;
252
-
253
- // Loop through all the Socket Addresses that the hostname resolves to
254
- for socket_addr in ( host, port) . to_socket_addrs ( ) . await ? {
255
- let stream = Async :: < TcpStream > :: connect ( socket_addr)
256
- . await
257
- . and_then ( |s| {
258
- s. get_ref ( ) . set_nodelay ( true ) ?;
259
- Ok ( s)
260
- } ) ;
261
- match stream {
262
- Ok ( stream) => return Ok ( with_socket. with_socket ( stream) . await ) ,
263
- Err ( e) => last_err = Some ( e) ,
228
+ // If we reach this point, it means we failed to connect to any of the addresses.
229
+ // Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
230
+ Err ( match last_err {
231
+ Some ( err) => err,
232
+ None => io:: Error :: new(
233
+ io:: ErrorKind :: AddrNotAvailable ,
234
+ "Hostname did not resolve to any addresses" ,
235
+ ) ,
236
+ }
237
+ . into( ) )
238
+ } else if #[ cfg( feature = "_rt-async-std" ) ] {
239
+ use async_io_std:: Async ;
240
+ use async_std:: net:: ToSocketAddrs ;
241
+ use std:: net:: TcpStream ;
242
+
243
+ let mut last_err = None ;
244
+
245
+ // Loop through all the Socket Addresses that the hostname resolves to
246
+ for socket_addr in ( host, port) . to_socket_addrs( ) . await ? {
247
+ let stream = Async :: <TcpStream >:: connect( socket_addr)
248
+ . await
249
+ . and_then( |s| {
250
+ s. get_ref( ) . set_nodelay( true ) ?;
251
+ Ok ( s)
252
+ } ) ;
253
+ match stream {
254
+ Ok ( stream) => return Ok ( with_socket. with_socket( stream) . await ) ,
255
+ Err ( e) => last_err = Some ( e) ,
256
+ }
264
257
}
265
- }
266
258
267
- // If we reach this point, it means we failed to connect to any of the addresses.
268
- // Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
269
- return match last_err {
270
- Some ( err) => Err ( err. into ( ) ) ,
271
- None => Err ( io:: Error :: new (
272
- io:: ErrorKind :: AddrNotAvailable ,
273
- "Hostname did not resolve to any addresses" ,
274
- )
275
- . into ( ) ) ,
276
- } ;
277
- }
259
+ // If we reach this point, it means we failed to connect to any of the addresses.
260
+ // Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
261
+ Err ( match last_err {
262
+ Some ( err) => err,
263
+ None => io:: Error :: new(
264
+ io:: ErrorKind :: AddrNotAvailable ,
265
+ "Hostname did not resolve to any addresses" ,
266
+ ) ,
267
+ }
268
+ . into( ) )
269
+ } else if #[ cfg( feature = "_rt-smol" ) ] {
270
+ use smol:: net:: resolve;
271
+ use smol:: Async ;
272
+ use std:: net:: TcpStream ;
273
+
274
+ let mut last_err = None ;
275
+
276
+ // Loop through all the Socket Addresses that the hostname resolves to
277
+ for socket_addr in resolve( ( host, port) ) . await ? {
278
+ let stream = Async :: <TcpStream >:: connect( socket_addr)
279
+ . await
280
+ . and_then( |s| {
281
+ s. get_ref( ) . set_nodelay( true ) ?;
282
+ Ok ( s)
283
+ } ) ;
284
+ match stream {
285
+ Ok ( stream) => return Ok ( with_socket. with_socket( stream) . await ) ,
286
+ Err ( e) => last_err = Some ( e) ,
287
+ }
288
+ }
278
289
279
- #[ cfg( not( all(
280
- feature = "_rt-async-global-executor" ,
281
- feature = "_rt-async-std" ,
282
- feature = "_rt-smol"
283
- ) ) ) ]
284
- #[ allow( unreachable_code) ]
285
- {
286
- crate :: rt:: missing_rt ( ( host, port, with_socket) )
290
+ // If we reach this point, it means we failed to connect to any of the addresses.
291
+ // Return the last error we encountered, or a custom error if the hostname didn't resolve to any address.
292
+ Err ( match last_err {
293
+ Some ( err) => err,
294
+ None => io:: Error :: new(
295
+ io:: ErrorKind :: AddrNotAvailable ,
296
+ "Hostname did not resolve to any addresses" ,
297
+ ) ,
298
+ }
299
+ . into( ) )
300
+ } else {
301
+ crate :: rt:: missing_rt( ( host, port, with_socket) )
302
+ }
287
303
}
288
304
}
289
305
@@ -305,44 +321,31 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
305
321
return Ok ( with_socket. with_socket ( stream) . await ) ;
306
322
}
307
323
308
- #[ cfg( feature = "_rt-async-global-executor" ) ]
309
- {
310
- use async_io_global_executor:: Async ;
311
- use std:: os:: unix:: net:: UnixStream ;
312
-
313
- let stream = Async :: < UnixStream > :: connect ( path) . await ?;
314
-
315
- return Ok ( with_socket. with_socket ( stream) . await ) ;
316
- }
317
-
318
- #[ cfg( feature = "_rt-async-std" ) ]
319
- {
320
- use async_io_std:: Async ;
321
- use std:: os:: unix:: net:: UnixStream ;
324
+ cfg_if ! {
325
+ if #[ cfg( feature = "_rt-async-global-executor" ) ] {
326
+ use async_io_global_executor:: Async ;
327
+ use std:: os:: unix:: net:: UnixStream ;
322
328
323
- let stream = Async :: < UnixStream > :: connect ( path) . await ?;
329
+ let stream = Async :: <UnixStream >:: connect( path) . await ?;
324
330
325
- return Ok ( with_socket. with_socket ( stream) . await ) ;
326
- }
331
+ Ok ( with_socket. with_socket( stream) . await )
332
+ } else if #[ cfg( feature = "_rt-async-std" ) ] {
333
+ use async_io_std:: Async ;
334
+ use std:: os:: unix:: net:: UnixStream ;
327
335
328
- #[ cfg( feature = "_rt-smol" ) ]
329
- {
330
- use smol:: Async ;
331
- use std:: os:: unix:: net:: UnixStream ;
336
+ let stream = Async :: <UnixStream >:: connect( path) . await ?;
332
337
333
- let stream = Async :: < UnixStream > :: connect ( path) . await ?;
338
+ Ok ( with_socket. with_socket( stream) . await )
339
+ } else if #[ cfg( feature = "_rt-smol" ) ] {
340
+ use smol:: Async ;
341
+ use std:: os:: unix:: net:: UnixStream ;
334
342
335
- return Ok ( with_socket. with_socket ( stream) . await ) ;
336
- }
343
+ let stream = Async :: <UnixStream >:: connect( path) . await ?;
337
344
338
- #[ cfg( not( all(
339
- feature = "_rt-async-global-executor" ,
340
- feature = "_rt-async-std" ,
341
- feature = "_rt-smol"
342
- ) ) ) ]
343
- #[ allow( unreachable_code) ]
344
- {
345
- crate :: rt:: missing_rt ( ( path, with_socket) )
345
+ Ok ( with_socket. with_socket( stream) . await )
346
+ } else {
347
+ crate :: rt:: missing_rt( ( path, with_socket) )
348
+ }
346
349
}
347
350
}
348
351
0 commit comments