sqlx_rt/
rt_tokio.rs

1pub use tokio::{
2    self, fs, io::AsyncRead, io::AsyncReadExt, io::AsyncWrite, io::AsyncWriteExt, io::ReadBuf,
3    net::TcpStream, runtime::Handle, sync::Mutex as AsyncMutex, task::spawn, task::yield_now,
4    time::sleep, time::timeout,
5};
6
7#[cfg(unix)]
8pub use tokio::net::UnixStream;
9
10use once_cell::sync::Lazy;
11use tokio::runtime::{self, Runtime};
12
13#[cfg(all(feature = "_tls-native-tls", not(feature = "_tls-rustls")))]
14pub use tokio_native_tls::{TlsConnector, TlsStream};
15
16#[cfg(all(feature = "_tls-rustls", not(feature = "_tls-native-tls")))]
17pub use tokio_rustls::{client::TlsStream, TlsConnector};
18
19// lazily initialize a global runtime once for multiple invocations of the macros
20static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
21    runtime::Builder::new_current_thread()
22        .enable_io()
23        .enable_time()
24        .build()
25        .expect("failed to initialize Tokio runtime")
26});
27
28pub fn block_on<F: std::future::Future>(future: F) -> F::Output {
29    RUNTIME.block_on(future)
30}
31
32pub fn enter_runtime<F, R>(f: F) -> R
33where
34    F: FnOnce() -> R,
35{
36    let _rt = RUNTIME.enter();
37    f()
38}
39
40pub fn test_block_on<F: std::future::Future>(future: F) -> F::Output {
41    // For tests, we want a single runtime per thread for isolation.
42    runtime::Builder::new_current_thread()
43        .enable_all()
44        .build()
45        .expect("failed to initialize Tokio test runtime")
46        .block_on(future)
47}