diff --git a/Cargo.toml b/Cargo.toml index ec492b23..80d4765f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ ".", "sqlx-core", + "sqlx-rt", "sqlx-macros", "sqlx-test", "sqlx-cli", diff --git a/sqlx-rt/Cargo.toml b/sqlx-rt/Cargo.toml new file mode 100644 index 00000000..c90a9d81 --- /dev/null +++ b/sqlx-rt/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "sqlx-rt" +version = "0.1.0-pre" +repository = "https://github.com/launchbadge/sqlx" +license = "MIT OR Apache-2.0" +edition = "2018" + +[features] +runtime-actix = [ "actix-rt", "actix-threadpool", "tokio", "tokio-native-tls" ] +runtime-async-std = [ "async-std", "async-native-tls" ] +runtime-tokio = [ "tokio", "tokio-native-tls" ] + +[dependencies] +async-native-tls = { version = "0.3.3", optional = true } +actix-rt = { version = "1.1.0", optional = true } +actix-threadpool = { version = "0.3.2", optional = true } +async-std = { version = "1.6.0", features = [ "unstable" ], optional = true } +tokio = { version = "0.2.17", optional = true, features = [ "blocking", "fs", "tcp", "uds", "macros", "rt-core", "rt-threaded", "time", "dns", "io-util" ] } +tokio-native-tls = { version = "0.1", optional = true } +native-tls = "0.2.4" diff --git a/sqlx-rt/src/lib.rs b/sqlx-rt/src/lib.rs new file mode 100644 index 00000000..072fb181 --- /dev/null +++ b/sqlx-rt/src/lib.rs @@ -0,0 +1,115 @@ +#[cfg(not(any( + feature = "runtime-actix", + feature = "runtime-async-std", + feature = "runtime-tokio", +)))] +compile_error!( + "one of 'runtime-actix', 'runtime-async-std' or 'runtime-tokio' features must be enabled" +); + +#[cfg(any( + all(feature = "runtime-actix", feature = "runtime-async-std"), + all(feature = "runtime-actix", feature = "runtime-tokio"), + all(feature = "runtime-async-std", feature = "runtime-tokio"), +))] +compile_error!( + "only one of 'runtime-actix', 'runtime-async-std' or 'runtime-tokio' features can be enabled" +); + +pub use native_tls; + +// +// Actix *OR* Tokio +// + +#[cfg(all( + not(feature = "runtime-async-std"), + any(feature = "runtime-tokio", feature = "runtime-actix"), +))] +pub use tokio::{ + self, fs, io::AsyncRead, io::AsyncReadExt, io::AsyncWrite, io::AsyncWriteExt, net::TcpStream, + task::yield_now, +}; + +#[cfg(all( + unix, + not(feature = "runtime-async-std"), + any(feature = "runtime-tokio", feature = "runtime-actix"), +))] +pub use tokio::net::UnixStream; + +// +// tokio +// + +#[cfg(all( + feature = "runtime-tokio", + not(any(feature = "runtime-actix", feature = "runtime-async-std",)) +))] +#[macro_export] +macro_rules! blocking { + ($($expr:tt)*) => { + $crate::tokio::task::block_in_place(move || { $($expr)* }) + }; +} + +#[cfg(all(feature = "tokio-native-tls", not(feature = "async-native-tls")))] +pub use tokio_native_tls::{TlsConnector, TlsStream}; + +#[cfg(all(feature = "tokio-native-tls", not(feature = "async-native-tls")))] +pub use native_tls::Error as TlsError; + +// +// actix +// + +#[cfg(feature = "runtime-actix")] +pub use {actix_rt, actix_threadpool}; + +#[cfg(all( + feature = "runtime-actix", + not(any(feature = "runtime-tokio", feature = "runtime-async-std",)) +))] +#[macro_export] +macro_rules! blocking { + ($($expr:tt)*) => { + $crate::actix_threadpool::run(move || { $($expr)* }).await.map_err(|err| match err { + $crate::actix_threadpool::BlockingError::Error(e) => e, + $crate::actix_threadpool::BlockingError::Canceled => panic!("{}", err) + }) + }; +} + +// +// async-std +// + +#[cfg(all( + feature = "runtime-async-std", + not(any(feature = "runtime-actix", feature = "runtime-tokio",)) +))] +pub use async_std::{ + self, fs, io::prelude::ReadExt as AsyncReadExt, io::prelude::WriteExt as AsyncWriteExt, + io::Read as AsyncRead, io::Write as AsyncWrite, net::TcpStream, task::spawn, task::yield_now, +}; + +#[cfg(all( + feature = "runtime-async-std", + not(any(feature = "runtime-actix", feature = "runtime-tokio",)) +))] +#[macro_export] +macro_rules! blocking { + ($($expr:tt)*) => { + $crate::async_std::task::spawn_blocking(move || { $($expr)* }).await + }; +} + +#[cfg(all( + unix, + feature = "runtime-async-std", + not(any(feature = "runtime-actix", feature = "runtime-tokio",)) +))] +pub use async_std::os::unix::net::UnixStream; + +#[cfg(all(feature = "async-native-tls", not(feature = "tokio-native-tls")))] +pub use async_native_tls::{Error as TlsError, TlsConnector, TlsStream};