Skip to content

Commit 4f851b0

Browse files
Cleanup conditional rt compilation
1 parent b462f43 commit 4f851b0

File tree

14 files changed

+331
-536
lines changed

14 files changed

+331
-536
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ time = { version = "0.3.36", features = ["formatting", "parsing", "macros"] }
160160
uuid = "1.1.2"
161161

162162
# Common utility crates
163+
cfg-if = "1.0.0"
163164
dotenvy = { version = "0.15.0", default-features = false }
164165

165166
# Runtimes

sqlx-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ async-lock = { version = "3.4.0", optional = true }
6767
async-net = { package = "async-net", version = "2.0.0", optional = true }
6868
base64 = { version = "0.22.0", default-features = false, features = ["std"] }
6969
bytes = "1.1.0"
70+
cfg-if = { workspace = true }
7071
chrono = { version = "0.4.34", default-features = false, features = ["clock"], optional = true }
7172
crc = { version = "3", optional = true }
7273
crossbeam-queue = "0.3.2"

sqlx-core/src/net/socket/mod.rs

Lines changed: 112 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::pin::Pin;
55
use std::task::{ready, Context, Poll};
66

77
use bytes::BufMut;
8+
use cfg_if::cfg_if;
89

910
pub use buffered::{BufferedSocket, WriteBuffer};
1011

@@ -202,88 +203,103 @@ pub async fn connect_tcp<Ws: WithSocket>(
202203
return Ok(with_socket.with_socket(stream).await);
203204
}
204205

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+
}
230226
}
231-
}
232227

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+
}
264257
}
265-
}
266258

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+
}
278289

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+
}
287303
}
288304
}
289305

@@ -305,44 +321,31 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
305321
return Ok(with_socket.with_socket(stream).await);
306322
}
307323

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;
322328

323-
let stream = Async::<UnixStream>::connect(path).await?;
329+
let stream = Async::<UnixStream>::connect(path).await?;
324330

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;
327335

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?;
332337

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;
334342

335-
return Ok(with_socket.with_socket(stream).await);
336-
}
343+
let stream = Async::<UnixStream>::connect(path).await?;
337344

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+
}
346349
}
347350
}
348351

0 commit comments

Comments
 (0)