Skip to content

Commit b462f43

Browse files
Changes outside sqlx_rt
1 parent 7de5718 commit b462f43

File tree

5 files changed

+174
-25
lines changed

5 files changed

+174
-25
lines changed

Cargo.lock

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

sqlx-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ json = ["serde", "serde_json"]
2222
_rt-async-global-executor = [
2323
"async-global-executor",
2424
"async-io-global-executor",
25+
"async-lock",
2526
"async-net",
2627
]
2728
_rt-async-std = ["async-std", "async-io-std"]
@@ -62,6 +63,7 @@ uuid = { workspace = true, optional = true }
6263

6364
async-io-global-executor = { package = "async-io", version = "2.2", optional = true }
6465
async-io-std = { package = "async-io", version = "1.9.0", optional = true }
66+
async-lock = { version = "3.4.0", optional = true }
6567
async-net = { package = "async-net", version = "2.0.0", optional = true }
6668
base64 = { version = "0.22.0", default-features = false, features = ["std"] }
6769
bytes = "1.1.0"

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@ pub async fn connect_tcp<Ws: WithSocket>(
202202
return Ok(with_socket.with_socket(stream).await);
203203
}
204204

205-
#[cfg(feature = "_rt-async-global-executor")]
205+
#[cfg(any(feature = "_rt-async-global-executor", feature = "_rt-smol"))]
206206
{
207+
#[cfg(feature = "_rt-async-global-executor")]
207208
use async_io_global_executor::Async;
209+
#[cfg(feature = "_rt-async-global-executor")]
208210
use async_net::resolve;
211+
#[cfg(feature = "_rt-smol")]
212+
use smol::Async;
213+
#[cfg(feature = "_rt-smol")]
214+
use smol::net::resolve;
209215
use std::net::TcpStream;
210216

211217
let mut last_err = None;
@@ -270,7 +276,11 @@ pub async fn connect_tcp<Ws: WithSocket>(
270276
};
271277
}
272278

273-
#[cfg(not(all(feature = "_rt-async-global-executor", feature = "_rt-async-std")))]
279+
#[cfg(not(all(
280+
feature = "_rt-async-global-executor",
281+
feature = "_rt-async-std",
282+
feature = "_rt-smol"
283+
)))]
274284
#[allow(unreachable_code)]
275285
{
276286
crate::rt::missing_rt((host, port, with_socket))
@@ -315,7 +325,21 @@ pub async fn connect_uds<P: AsRef<Path>, Ws: WithSocket>(
315325
return Ok(with_socket.with_socket(stream).await);
316326
}
317327

318-
#[cfg(not(all(feature = "_rt-async-global-executor", feature = "_rt-async-std")))]
328+
#[cfg(feature = "_rt-smol")]
329+
{
330+
use smol::Async;
331+
use std::os::unix::net::UnixStream;
332+
333+
let stream = Async::<UnixStream>::connect(path).await?;
334+
335+
return Ok(with_socket.with_socket(stream).await);
336+
}
337+
338+
#[cfg(not(all(
339+
feature = "_rt-async-global-executor",
340+
feature = "_rt-async-std",
341+
feature = "_rt-smol"
342+
)))]
319343
#[allow(unreachable_code)]
320344
{
321345
crate::rt::missing_rt((path, with_socket))

sqlx-core/src/sync.rs

Lines changed: 129 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44
// We'll generally lean towards Tokio's types as those are more featureful
55
// (including `tokio-console` support) and more widely deployed.
66

7+
#[cfg(all(feature = "_rt-async-global-executor", not(feature = "_rt-tokio")))]
8+
pub use async_lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
9+
710
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
811
pub use async_std::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
912

13+
#[cfg(all(feature = "_rt-smol", not(feature = "_rt-tokio")))]
14+
pub use smol::lock::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
15+
1016
#[cfg(feature = "_rt-tokio")]
1117
pub use tokio::sync::{Mutex as AsyncMutex, MutexGuard as AsyncMutexGuard};
1218

1319
pub struct AsyncSemaphore {
14-
// We use the semaphore from futures-intrusive as the one from async-std
20+
// We use the semaphore from futures-intrusive as the one from async-lock
1521
// is missing the ability to add arbitrary permits, and is not guaranteed to be fair:
1622
// * https://github.com/smol-rs/async-lock/issues/22
1723
// * https://github.com/smol-rs/async-lock/issues/23
@@ -20,7 +26,14 @@ pub struct AsyncSemaphore {
2026
// and there are some soundness concerns (although it turns out any intrusive future is unsound
2127
// in MIRI due to the necessitated mutable aliasing):
2228
// https://github.com/launchbadge/sqlx/issues/1668
23-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
29+
#[cfg(all(
30+
any(
31+
feature = "_rt-async-global-executor",
32+
feature = "_rt-async-std",
33+
feature = "_rt-smol"
34+
),
35+
not(feature = "_rt-tokio")
36+
))]
2437
inner: futures_intrusive::sync::Semaphore,
2538

2639
#[cfg(feature = "_rt-tokio")]
@@ -30,12 +43,24 @@ pub struct AsyncSemaphore {
3043
impl AsyncSemaphore {
3144
#[track_caller]
3245
pub fn new(fair: bool, permits: usize) -> Self {
33-
if cfg!(not(any(feature = "_rt-async-std", feature = "_rt-tokio"))) {
46+
if cfg!(not(any(
47+
feature = "_rt-async-global-executor",
48+
feature = "_rt-async-std",
49+
feature = "_rt-smol",
50+
feature = "_rt-tokio"
51+
))) {
3452
crate::rt::missing_rt((fair, permits));
3553
}
3654

3755
AsyncSemaphore {
38-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
56+
#[cfg(all(
57+
any(
58+
feature = "_rt-async-global-executor",
59+
feature = "_rt-async-std",
60+
feature = "_rt-smol"
61+
),
62+
not(feature = "_rt-tokio")
63+
))]
3964
inner: futures_intrusive::sync::Semaphore::new(fair, permits),
4065
#[cfg(feature = "_rt-tokio")]
4166
inner: {
@@ -46,18 +71,39 @@ impl AsyncSemaphore {
4671
}
4772

4873
pub fn permits(&self) -> usize {
49-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
74+
#[cfg(all(
75+
any(
76+
feature = "_rt-async-global-executor",
77+
feature = "_rt-async-std",
78+
feature = "_rt-smol"
79+
),
80+
not(feature = "_rt-tokio")
81+
))]
5082
return self.inner.permits();
5183

5284
#[cfg(feature = "_rt-tokio")]
5385
return self.inner.available_permits();
5486

55-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
87+
#[cfg(not(any(
88+
any(
89+
feature = "_rt-async-global-executor",
90+
feature = "_rt-async-std",
91+
feature = "_rt-smol"
92+
),
93+
feature = "_rt-tokio"
94+
)))]
5695
crate::rt::missing_rt(())
5796
}
5897

5998
pub async fn acquire(&self, permits: u32) -> AsyncSemaphoreReleaser<'_> {
60-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
99+
#[cfg(all(
100+
any(
101+
feature = "_rt-async-global-executor",
102+
feature = "_rt-async-std",
103+
feature = "_rt-smol"
104+
),
105+
not(feature = "_rt-tokio")
106+
))]
61107
return AsyncSemaphoreReleaser {
62108
inner: self.inner.acquire(permits as usize).await,
63109
};
@@ -73,12 +119,26 @@ impl AsyncSemaphore {
73119
.expect("BUG: we do not expose the `.close()` method"),
74120
};
75121

76-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
122+
#[cfg(not(any(
123+
any(
124+
feature = "_rt-async-global-executor",
125+
feature = "_rt-async-std",
126+
feature = "_rt-smol"
127+
),
128+
feature = "_rt-tokio"
129+
)))]
77130
crate::rt::missing_rt(permits)
78131
}
79132

80133
pub fn try_acquire(&self, permits: u32) -> Option<AsyncSemaphoreReleaser<'_>> {
81-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
134+
#[cfg(all(
135+
any(
136+
feature = "_rt-async-global-executor",
137+
feature = "_rt-async-std",
138+
feature = "_rt-smol"
139+
),
140+
not(feature = "_rt-tokio")
141+
))]
82142
return Some(AsyncSemaphoreReleaser {
83143
inner: self.inner.try_acquire(permits as usize)?,
84144
});
@@ -88,18 +148,39 @@ impl AsyncSemaphore {
88148
inner: self.inner.try_acquire_many(permits).ok()?,
89149
});
90150

91-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
151+
#[cfg(not(any(
152+
any(
153+
feature = "_rt-async-global-executor",
154+
feature = "_rt-async-std",
155+
feature = "_rt-smol"
156+
),
157+
feature = "_rt-tokio"
158+
)))]
92159
crate::rt::missing_rt(permits)
93160
}
94161

95162
pub fn release(&self, permits: usize) {
96-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
163+
#[cfg(all(
164+
any(
165+
feature = "_rt-async-global-executor",
166+
feature = "_rt-async-std",
167+
feature = "_rt-smol"
168+
),
169+
not(feature = "_rt-tokio")
170+
))]
97171
return self.inner.release(permits);
98172

99173
#[cfg(feature = "_rt-tokio")]
100174
return self.inner.add_permits(permits);
101175

102-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
176+
#[cfg(not(any(
177+
any(
178+
feature = "_rt-async-global-executor",
179+
feature = "_rt-async-std",
180+
feature = "_rt-smol"
181+
),
182+
feature = "_rt-tokio"
183+
)))]
103184
crate::rt::missing_rt(permits)
104185
}
105186
}
@@ -114,30 +195,58 @@ pub struct AsyncSemaphoreReleaser<'a> {
114195
// and there are some soundness concerns (although it turns out any intrusive future is unsound
115196
// in MIRI due to the necessitated mutable aliasing):
116197
// https://github.com/launchbadge/sqlx/issues/1668
117-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
198+
#[cfg(all(
199+
any(
200+
feature = "_rt-async-global-executor",
201+
feature = "_rt-async-std",
202+
feature = "_rt-smol"
203+
),
204+
not(feature = "_rt-tokio")
205+
))]
118206
inner: futures_intrusive::sync::SemaphoreReleaser<'a>,
119207

120208
#[cfg(feature = "_rt-tokio")]
121209
inner: tokio::sync::SemaphorePermit<'a>,
122210

123-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
211+
#[cfg(not(any(
212+
any(
213+
feature = "_rt-async-global-executor",
214+
feature = "_rt-async-std",
215+
feature = "_rt-smol"
216+
),
217+
feature = "_rt-tokio"
218+
)))]
124219
_phantom: std::marker::PhantomData<&'a ()>,
125220
}
126221

127222
impl AsyncSemaphoreReleaser<'_> {
128223
pub fn disarm(self) {
129-
#[cfg(feature = "_rt-tokio")]
224+
#[cfg(all(
225+
any(
226+
feature = "_rt-async-global-executor",
227+
feature = "_rt-async-std",
228+
feature = "_rt-smol"
229+
),
230+
not(feature = "_rt-tokio")
231+
))]
130232
{
131-
self.inner.forget();
233+
let mut this = self;
234+
this.inner.disarm();
132235
}
133236

134-
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
237+
#[cfg(feature = "_rt-tokio")]
135238
{
136-
let mut this = self;
137-
this.inner.disarm();
239+
self.inner.forget();
138240
}
139241

140-
#[cfg(not(any(feature = "_rt-async-std", feature = "_rt-tokio")))]
242+
#[cfg(not(any(
243+
any(
244+
feature = "_rt-async-global-executor",
245+
feature = "_rt-async-std",
246+
feature = "_rt-smol"
247+
),
248+
feature = "_rt-tokio"
249+
)))]
141250
crate::rt::missing_rt(())
142251
}
143252
}

sqlx-macros-core/src/lib.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,24 @@ where
7272
TOKIO_RT.block_on(f)
7373
}
7474

75-
#[cfg(all(feature = "_rt-async-std", not(feature = "tokio")))]
75+
#[cfg(all(
76+
any(feature = "_rt-async-global-executor", feature = "_rt-smol"),
77+
not(feature = "_rt-tokio")
78+
))]
79+
{
80+
sqlx_core::rt::test_block_on(f)
81+
}
82+
83+
#[cfg(all(feature = "_rt-async-std", not(feature = "_rt-tokio")))]
7684
{
7785
async_std::task::block_on(f)
7886
}
7987

80-
#[cfg(not(any(feature = "_rt-async-std", feature = "tokio")))]
88+
#[cfg(not(any(
89+
feature = "_rt-async-global-executor",
90+
feature = "_rt-async-std",
91+
feature = "_rt-smol",
92+
feature = "_rt-tokio"
93+
)))]
8194
sqlx_core::rt::missing_rt(f)
8295
}

0 commit comments

Comments
 (0)