Only create tokio runtime once for block_on

This commit is contained in:
John-John Tedro 2020-01-15 14:49:46 +01:00
parent e5f6bd49f3
commit 453012c9bf
3 changed files with 11 additions and 3 deletions

1
Cargo.lock generated
View file

@ -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)",

View file

@ -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"

View file

@ -25,10 +25,16 @@ 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().basic_scheduler().enable_all().build().expect("failed to build tokio runtime")
};
}
#[cfg(feature = "runtime-tokio")]
fn block_on<F: std::future::Future>(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 {