diff --git a/sqlx-rt/src/lib.rs b/sqlx-rt/src/lib.rs index fb9d823fe9..83d99e7789 100644 --- a/sqlx-rt/src/lib.rs +++ b/sqlx-rt/src/lib.rs @@ -38,6 +38,40 @@ pub use tokio::{ ))] pub use tokio::net::UnixStream; +#[cfg(all( + any(feature = "_rt-tokio", feature = "_rt-actix"), + not(feature = "_rt-async-std"), +))] +pub use tokio_runtime::{block_on, enter_runtime}; + +#[cfg(any(feature = "_rt-tokio", feature = "_rt-actix"))] +mod tokio_runtime { + use once_cell::sync::Lazy; + use tokio::runtime::{self, Runtime}; + + // lazily initialize a global runtime once for multiple invocations of the macros + static RUNTIME: Lazy = Lazy::new(|| { + runtime::Builder::new() + // `.basic_scheduler()` requires calling `Runtime::block_on()` which needs mutability + .threaded_scheduler() + .enable_io() + .enable_time() + .build() + .expect("failed to initialize Tokio runtime") + }); + + pub fn block_on(future: F) -> F::Output { + RUNTIME.enter(|| RUNTIME.handle().block_on(future)) + } + + pub fn enter_runtime(f: F) -> R + where + F: FnOnce() -> R, + { + RUNTIME.enter(f) + } +} + #[cfg(all( feature = "_tls-native-tls", any(feature = "_rt-tokio", feature = "_rt-actix"), @@ -113,9 +147,6 @@ macro_rules! blocking { ))] pub use async_std::os::unix::net::UnixStream; -#[cfg(all(feature = "async-native-tls", not(feature = "tokio-native-tls")))] -pub use async_native_tls::{TlsConnector, TlsStream}; - #[cfg(all( feature = "_rt-async-std", not(any(feature = "_rt-actix", feature = "_rt-tokio")), @@ -134,36 +165,5 @@ where f() } -#[cfg(all( - any(feature = "_rt-tokio", feature = "_rt-actix"), - not(feature = "_rt-async-std"), -))] -pub use tokio_runtime::{block_on, enter_runtime}; - -#[cfg(any(feature = "_rt-tokio", feature = "_rt-actix"))] -mod tokio_runtime { - use once_cell::sync::Lazy; - use tokio::runtime::{self, Runtime}; - - // lazily initialize a global runtime once for multiple invocations of the macros - static RUNTIME: Lazy = Lazy::new(|| { - runtime::Builder::new() - // `.basic_scheduler()` requires calling `Runtime::block_on()` which needs mutability - .threaded_scheduler() - .enable_io() - .enable_time() - .build() - .expect("failed to initialize Tokio runtime") - }); - - pub fn block_on(future: F) -> F::Output { - RUNTIME.enter(|| RUNTIME.handle().block_on(future)) - } - - pub fn enter_runtime(f: F) -> R - where - F: FnOnce() -> R, - { - RUNTIME.enter(f) - } -} +#[cfg(all(feature = "async-native-tls", not(feature = "tokio-native-tls")))] +pub use async_native_tls::{TlsConnector, TlsStream};