Skip to content

Commit c42e859

Browse files
committed
Add support for tokio to sqlx-macros
1 parent e770f2a commit c42e859

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

.github/workflows/mariadb.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848

4949
# -----------------------------------------------------
5050

51-
- run: cargo test -p sqlx --no-default-features --features 'mysql macros chrono'
51+
- run: cargo test -p sqlx --no-default-features --features 'runtime-async-std mysql macros chrono'
5252
env:
5353
DATABASE_URL: mariadb://root:password@localhost:${{ job.services.mariadb.ports[3306] }}/sqlx
5454

.github/workflows/mysql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151

5252
# -----------------------------------------------------
5353

54-
- run: cargo test -p sqlx --no-default-features --features 'mysql macros chrono tls'
54+
- run: cargo test -p sqlx --no-default-features --features 'runtime-async-std mysql macros chrono tls'
5555
env:
5656
# pass the path to the CA that the MySQL service generated
5757
# Github Actions' YML parser doesn't handle multiline strings correctly

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ macros = [ "sqlx-macros" ]
3333
tls = [ "sqlx-core/tls" ]
3434

3535
# runtime
36-
runtime-async-std = [ "sqlx-core/runtime-async-std" ]
37-
runtime-tokio = [ "sqlx-core/runtime-tokio" ]
36+
runtime-async-std = [ "sqlx-core/runtime-async-std", "sqlx-macros/runtime-async-std" ]
37+
runtime-tokio = [ "sqlx-core/runtime-tokio", "sqlx-macros/runtime-tokio" ]
3838

3939
# database
4040
postgres = [ "sqlx-core/postgres", "sqlx-macros/postgres" ]

sqlx-macros/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ proc-macro = true
1818
[features]
1919
default = []
2020

21+
runtime-async-std = [ "sqlx/runtime-async-std", "async-std" ]
22+
runtime-tokio = [ "sqlx/runtime-tokio", "tokio", "once_cell" ]
23+
2124
# database
2225
mysql = [ "sqlx/mysql" ]
2326
postgres = [ "sqlx/postgres" ]
@@ -27,7 +30,9 @@ chrono = [ "sqlx/chrono" ]
2730
uuid = [ "sqlx/uuid" ]
2831

2932
[dependencies]
30-
async-std = { version = "1.4.0", default-features = false }
33+
async-std = { version = "1.4.0", default-features = false, optional = true }
34+
tokio = { version = "0.2", optional = true }
35+
once_cell = { version = "1.3", optional = true }
3136
dotenv = { version = "0.15.0", default-features = false }
3237
futures = { version = "0.3.1", default-features = false }
3338
proc-macro2 = { version = "1.0.6", default-features = false }

sqlx-macros/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use quote::quote;
1010

1111
use syn::parse_macro_input;
1212

13-
use async_std::task;
13+
#[cfg(feature = "runtime-async-std")]
14+
use async_std::task::block_on;
1415

1516
use url::Url;
1617

@@ -24,6 +25,12 @@ mod query_macros;
2425

2526
use query_macros::*;
2627

28+
#[cfg(feature = "runtime-tokio")]
29+
fn block_on<F: std::future::Future>(future: F) -> F::Output {
30+
// TODO: Someone think of something better for async proc macros + tokio
31+
tokio::runtime::Runtime::new().unwrap().block_on(future)
32+
}
33+
2734
fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {
2835
quote!(
2936
macro_rules! macro_result {
@@ -40,7 +47,7 @@ macro_rules! async_macro (
4047
Err(e) => return macro_result(e.to_compile_error()),
4148
};
4249

43-
let res: Result<proc_macro2::TokenStream> = task::block_on(async {
50+
let res: Result<proc_macro2::TokenStream> = block_on(async {
4451
use sqlx::Connect;
4552

4653
let db_url = Url::parse(&dotenv::var("DATABASE_URL").map_err(|_| "DATABASE_URL not set")?)?;

0 commit comments

Comments
 (0)