Skip to content

Commit 453012c

Browse files
committed
Only create tokio runtime once for block_on
1 parent e5f6bd4 commit 453012c

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
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-macros/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ async-std = { version = "1.4.0", default-features = false, optional = true }
3434
tokio = { version = "0.2", optional = true }
3535
once_cell = { version = "1.3", optional = true }
3636
dotenv = { version = "0.15.0", default-features = false }
37-
futures = { version = "0.3.1", default-features = false }
37+
futures = { version = "0.3.1", default-features = false, features = ["executor"] }
3838
proc-macro2 = { version = "1.0.6", default-features = false }
3939
sqlx = { version = "0.2.0", default-features = false, path = "../sqlx-core", package = "sqlx-core" }
4040
syn = { version = "1.0.11", default-features = false, features = [ "full" ] }
4141
quote = { version = "1.0.2", default-features = false }
4242
url = { version = "2.1.0", default-features = false }
43+
lazy_static = "1.4.0"

sqlx-macros/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ mod query_macros;
2525

2626
use query_macros::*;
2727

28+
#[cfg(feature = "runtime-tokio")]
29+
lazy_static::lazy_static! {
30+
static ref BASIC_RUNTIME: tokio::runtime::Runtime = {
31+
tokio::runtime::Builder::new().basic_scheduler().enable_all().build().expect("failed to build tokio runtime")
32+
};
33+
}
34+
2835
#[cfg(feature = "runtime-tokio")]
2936
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)
37+
BASIC_RUNTIME.enter(|| futures::executor::block_on(future))
3238
}
3339

3440
fn macro_result(tokens: proc_macro2::TokenStream) -> TokenStream {

0 commit comments

Comments
 (0)