diff --git a/Cargo.lock b/Cargo.lock index 314f9375f4..22f15aee31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1420,6 +1420,7 @@ 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)", + "lazy_static 1.4.0 (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)", diff --git a/sqlx-macros/Cargo.toml b/sqlx-macros/Cargo.toml index 8aefc6ce37..106a68c63d 100644 --- a/sqlx-macros/Cargo.toml +++ b/sqlx-macros/Cargo.toml @@ -34,9 +34,10 @@ 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 } +futures = { version = "0.3.1", default-features = false, features = ["executor"] } proc-macro2 = { version = "1.0.6", default-features = false } sqlx = { version = "0.2.0", default-features = false, path = "../sqlx-core", package = "sqlx-core" } syn = { version = "1.0.11", default-features = false, features = [ "full" ] } quote = { version = "1.0.2", default-features = false } url = { version = "2.1.0", default-features = false } +lazy_static = "1.4.0" diff --git a/sqlx-macros/src/lib.rs b/sqlx-macros/src/lib.rs index 16f8055bbc..4fa5e5a199 100644 --- a/sqlx-macros/src/lib.rs +++ b/sqlx-macros/src/lib.rs @@ -25,10 +25,21 @@ mod query_macros; use query_macros::*; +#[cfg(feature = "runtime-tokio")] +lazy_static::lazy_static! { + static ref BASIC_RUNTIME: tokio::runtime::Runtime = { + tokio::runtime::Builder::new() + .threaded_scheduler() + .enable_io() + .enable_time() + .build() + .expect("failed to build tokio runtime") + }; +} + #[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) + BASIC_RUNTIME.enter(|| futures::executor::block_on(future)) } fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {