Skip to content

Commit 2562c35

Browse files
authored
Merge pull request #55 from udoprog/tokio-runtime-once
Only create tokio runtime once for block_on
2 parents f939374 + 4b22672 commit 2562c35

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,21 @@ 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()
32+
.threaded_scheduler()
33+
.enable_io()
34+
.enable_time()
35+
.build()
36+
.expect("failed to build tokio runtime")
37+
};
38+
}
39+
2840
#[cfg(feature = "runtime-tokio")]
2941
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)
42+
BASIC_RUNTIME.enter(|| futures::executor::block_on(future))
3243
}
3344

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

0 commit comments

Comments
 (0)