Skip to content

Commit a233fbf

Browse files
committed
feat(rt): introduce sqlx-rt to centralize supported runtimes
1 parent 086dfec commit a233fbf

3 files changed

Lines changed: 136 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
".",
44
"sqlx-core",
5+
"sqlx-rt",
56
"sqlx-macros",
67
"sqlx-test",
78
"sqlx-cli",

sqlx-rt/Cargo.toml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[package]
2+
name = "sqlx-rt"
3+
version = "0.1.0-pre"
4+
repository = "https://github.com/launchbadge/sqlx"
5+
license = "MIT OR Apache-2.0"
6+
edition = "2018"
7+
8+
[features]
9+
runtime-actix = [ "actix-rt", "actix-threadpool", "tokio", "tokio-native-tls" ]
10+
runtime-async-std = [ "async-std", "async-native-tls" ]
11+
runtime-tokio = [ "tokio", "tokio-native-tls" ]
12+
13+
[dependencies]
14+
async-native-tls = { version = "0.3.3", optional = true }
15+
actix-rt = { version = "1.1.0", optional = true }
16+
actix-threadpool = { version = "0.3.2", optional = true }
17+
async-std = { version = "1.6.0", features = [ "unstable" ], optional = true }
18+
tokio = { version = "0.2.17", optional = true, features = [ "blocking", "fs", "tcp", "uds", "macros", "rt-core", "rt-threaded", "time", "dns", "io-util" ] }
19+
tokio-native-tls = { version = "0.1", optional = true }
20+
native-tls = "0.2.4"

sqlx-rt/src/lib.rs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#[cfg(not(any(
2+
feature = "runtime-actix",
3+
feature = "runtime-async-std",
4+
feature = "runtime-tokio",
5+
)))]
6+
compile_error!(
7+
"one of 'runtime-actix', 'runtime-async-std' or 'runtime-tokio' features must be enabled"
8+
);
9+
10+
#[cfg(any(
11+
all(feature = "runtime-actix", feature = "runtime-async-std"),
12+
all(feature = "runtime-actix", feature = "runtime-tokio"),
13+
all(feature = "runtime-async-std", feature = "runtime-tokio"),
14+
))]
15+
compile_error!(
16+
"only one of 'runtime-actix', 'runtime-async-std' or 'runtime-tokio' features can be enabled"
17+
);
18+
19+
pub use native_tls;
20+
21+
//
22+
// Actix *OR* Tokio
23+
//
24+
25+
#[cfg(all(
26+
not(feature = "runtime-async-std"),
27+
any(feature = "runtime-tokio", feature = "runtime-actix"),
28+
))]
29+
pub use tokio::{
30+
self, fs, io::AsyncRead, io::AsyncReadExt, io::AsyncWrite, io::AsyncWriteExt, net::TcpStream,
31+
task::yield_now,
32+
};
33+
34+
#[cfg(all(
35+
unix,
36+
not(feature = "runtime-async-std"),
37+
any(feature = "runtime-tokio", feature = "runtime-actix"),
38+
))]
39+
pub use tokio::net::UnixStream;
40+
41+
//
42+
// tokio
43+
//
44+
45+
#[cfg(all(
46+
feature = "runtime-tokio",
47+
not(any(feature = "runtime-actix", feature = "runtime-async-std",))
48+
))]
49+
#[macro_export]
50+
macro_rules! blocking {
51+
($($expr:tt)*) => {
52+
$crate::tokio::task::block_in_place(move || { $($expr)* })
53+
};
54+
}
55+
56+
#[cfg(all(feature = "tokio-native-tls", not(feature = "async-native-tls")))]
57+
pub use tokio_native_tls::{TlsConnector, TlsStream};
58+
59+
#[cfg(all(feature = "tokio-native-tls", not(feature = "async-native-tls")))]
60+
pub use native_tls::Error as TlsError;
61+
62+
//
63+
// actix
64+
//
65+
66+
#[cfg(feature = "runtime-actix")]
67+
pub use {actix_rt, actix_threadpool};
68+
69+
#[cfg(all(
70+
feature = "runtime-actix",
71+
not(any(feature = "runtime-tokio", feature = "runtime-async-std",))
72+
))]
73+
#[macro_export]
74+
macro_rules! blocking {
75+
($($expr:tt)*) => {
76+
$crate::actix_threadpool::run(move || { $($expr)* }).await.map_err(|err| match err {
77+
$crate::actix_threadpool::BlockingError::Error(e) => e,
78+
$crate::actix_threadpool::BlockingError::Canceled => panic!("{}", err)
79+
})
80+
};
81+
}
82+
83+
//
84+
// async-std
85+
//
86+
87+
#[cfg(all(
88+
feature = "runtime-async-std",
89+
not(any(feature = "runtime-actix", feature = "runtime-tokio",))
90+
))]
91+
pub use async_std::{
92+
self, fs, io::prelude::ReadExt as AsyncReadExt, io::prelude::WriteExt as AsyncWriteExt,
93+
io::Read as AsyncRead, io::Write as AsyncWrite, net::TcpStream, task::spawn, task::yield_now,
94+
};
95+
96+
#[cfg(all(
97+
feature = "runtime-async-std",
98+
not(any(feature = "runtime-actix", feature = "runtime-tokio",))
99+
))]
100+
#[macro_export]
101+
macro_rules! blocking {
102+
($($expr:tt)*) => {
103+
$crate::async_std::task::spawn_blocking(move || { $($expr)* }).await
104+
};
105+
}
106+
107+
#[cfg(all(
108+
unix,
109+
feature = "runtime-async-std",
110+
not(any(feature = "runtime-actix", feature = "runtime-tokio",))
111+
))]
112+
pub use async_std::os::unix::net::UnixStream;
113+
114+
#[cfg(all(feature = "async-native-tls", not(feature = "tokio-native-tls")))]
115+
pub use async_native_tls::{Error as TlsError, TlsConnector, TlsStream};

0 commit comments

Comments
 (0)