From c42e859141081e87e78682bd3ef7d82a65adef7b Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Wed, 15 Jan 2020 01:54:46 -0800 Subject: [PATCH] Add support for tokio to sqlx-macros --- .github/workflows/mariadb.yml | 2 +- .github/workflows/mysql.yml | 2 +- Cargo.lock | 8 +++++--- Cargo.toml | 4 ++-- sqlx-macros/Cargo.toml | 7 ++++++- sqlx-macros/src/lib.rs | 11 +++++++++-- 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/.github/workflows/mariadb.yml b/.github/workflows/mariadb.yml index a8421fad..cc653309 100644 --- a/.github/workflows/mariadb.yml +++ b/.github/workflows/mariadb.yml @@ -48,7 +48,7 @@ jobs: # ----------------------------------------------------- - - run: cargo test -p sqlx --no-default-features --features 'mysql macros chrono' + - run: cargo test -p sqlx --no-default-features --features 'runtime-async-std mysql macros chrono' env: DATABASE_URL: mariadb://root:password@localhost:${{ job.services.mariadb.ports[3306] }}/sqlx diff --git a/.github/workflows/mysql.yml b/.github/workflows/mysql.yml index 668a1937..06295cc5 100644 --- a/.github/workflows/mysql.yml +++ b/.github/workflows/mysql.yml @@ -51,7 +51,7 @@ jobs: # ----------------------------------------------------- - - run: cargo test -p sqlx --no-default-features --features 'mysql macros chrono tls' + - run: cargo test -p sqlx --no-default-features --features 'runtime-async-std mysql macros chrono tls' env: # pass the path to the CA that the MySQL service generated # Github Actions' YML parser doesn't handle multiline strings correctly diff --git a/Cargo.lock b/Cargo.lock index 959918e4..f1eca5ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,7 +68,7 @@ dependencies = [ "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", - "once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "once_cell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "pin-utils 0.1.0-alpha.4 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -959,7 +959,7 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.2.0" +version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -1420,10 +1420,12 @@ dependencies = [ "async-std 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "dotenv 0.15.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "once_cell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "sqlx-core 0.1.4", "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1923,7 +1925,7 @@ dependencies = [ "checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" "checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" "checksum num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)" = "76dac5ed2a876980778b8b85f75a71b6cbf0db0b1232ee12f826bccb00d09d72" -"checksum once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "891f486f630e5c5a4916c7e16c4b24a53e78c860b646e9f8e005e4f16847bfed" +"checksum once_cell 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5941ec2d5ee5916c709580d71553b81a633df245bcc73c04dcbd62152ceefc4" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum openssl 0.10.26 (registry+https://github.com/rust-lang/crates.io-index)" = "3a3cc5799d98e1088141b8e01ff760112bbd9f19d850c124500566ca6901a585" "checksum openssl-probe 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de" diff --git a/Cargo.toml b/Cargo.toml index 87424a2a..5df71cdf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,8 +33,8 @@ macros = [ "sqlx-macros" ] tls = [ "sqlx-core/tls" ] # runtime -runtime-async-std = [ "sqlx-core/runtime-async-std" ] -runtime-tokio = [ "sqlx-core/runtime-tokio" ] +runtime-async-std = [ "sqlx-core/runtime-async-std", "sqlx-macros/runtime-async-std" ] +runtime-tokio = [ "sqlx-core/runtime-tokio", "sqlx-macros/runtime-tokio" ] # database postgres = [ "sqlx-core/postgres", "sqlx-macros/postgres" ] diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index 2c8bc02d..db78167d 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -18,6 +18,9 @@ proc-macro = true [features] default = [] +runtime-async-std = [ "sqlx/runtime-async-std", "async-std" ] +runtime-tokio = [ "sqlx/runtime-tokio", "tokio", "once_cell" ] + # database mysql = [ "sqlx/mysql" ] postgres = [ "sqlx/postgres" ] @@ -27,7 +30,9 @@ chrono = [ "sqlx/chrono" ] uuid = [ "sqlx/uuid" ] [dependencies] -async-std = { version = "1.4.0", default-features = false } +async-std = { version = "1.4.0", default-features = false, optional = true } +tokio = { version = "0.2", optional = true } +once_cell = { version = "1.3", optional = true } dotenv = { version = "0.15.0", default-features = false } futures = { version = "0.3.1", default-features = false } proc-macro2 = { version = "1.0.6", default-features = false } diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 0504a667..8e05279d 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -10,7 +10,8 @@ use quote::quote; use syn::parse_macro_input; -use async_std::task; +#[cfg(feature = "runtime-async-std")] +use async_std::task::block_on; use url::Url; @@ -24,6 +25,12 @@ mod query_macros; use query_macros::*; +#[cfg(feature = "runtime-tokio")] +fn block_on(future: F) -> F::Output { + // TODO: Someone think of something better for async proc macros + tokio + tokio::runtime::Runtime::new().unwrap().block_on(future) +} + fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream { quote!( macro_rules! macro_result { @@ -40,7 +47,7 @@ macro_rules! async_macro ( Err(e) => return macro_result(e.to_compile_error()), }; - let res: Result = task::block_on(async { + let res: Result = block_on(async { use sqlx::Connect; let db_url = Url::parse(&dotenv::var("DATABASE_URL").map_err(|_| "DATABASE_URL not set")?)?;