diff --git a/.github/workflows/postgres.yml b/.github/workflows/postgres.yml index 8beab1867..89480dad1 100644 --- a/.github/workflows/postgres.yml +++ b/.github/workflows/postgres.yml @@ -50,9 +50,15 @@ jobs: # ----------------------------------------------------- # Check that we build with TLS support (TODO: we need a postgres image with SSL certs to test) - - run: cargo check -p sqlx-core --no-default-features --features 'postgres macros uuid chrono tls' + - run: cargo check -p sqlx-core --no-default-features --features 'runtime-async-std postgres macros uuid chrono tls' - - run: cargo test -p sqlx --no-default-features --features 'postgres macros uuid chrono' + # Test on async-std + - run: cargo test -p sqlx --no-default-features --features 'runtime-async-std postgres macros uuid chrono' + env: + DATABASE_URL: postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres + + # Test on tokio + - run: cargo test -p sqlx --no-default-features --features 'runtime-tokio postgres macros uuid chrono' env: DATABASE_URL: postgres://postgres:postgres@localhost:${{ job.services.postgres.ports[5432] }}/postgres diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 1af5892a2..a72bea6dd 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -32,9 +32,13 @@ jobs: # ----------------------------------------------------- - - run: cargo check --all-features + - run: cargo check --no-default-features --features 'chrono uuid postgres mysql macros tls runtime-async-std' - - run: cargo test -p sqlx-core --all-features + - run: cargo check --no-default-features --features 'chrono uuid postgres mysql macros tls runtime-tokio' + + - run: cargo test -p sqlx-core --no-default-features --features 'chrono uuid postgres mysql macros tls runtime-async-std' + + - run: cargo test -p sqlx-core --no-default-features --features 'chrono uuid postgres mysql macros tls runtime-tokio' # Rust ------------------------------------------------ diff --git a/Cargo.lock b/Cargo.lock index 53110d27a..959918e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,6 +13,11 @@ name = "anyhow" version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "arc-swap" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "arrayref" version = "0.3.5" @@ -40,6 +45,7 @@ dependencies = [ "async-std 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "thiserror 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -216,6 +222,11 @@ dependencies = [ "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bytes" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "c2-chacha" version = "0.2.3" @@ -842,6 +853,17 @@ dependencies = [ "winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "mio-named-pipes" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "mio-uds" version = "0.6.7" @@ -863,6 +885,15 @@ dependencies = [ "ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "miow" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "native-tls" version = "0.2.3" @@ -1276,6 +1307,15 @@ dependencies = [ "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "signal-hook-registry" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "slab" version = "0.4.2" @@ -1294,6 +1334,17 @@ name = "smallvec" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "socket2" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "spin" version = "0.5.2" @@ -1310,6 +1361,7 @@ dependencies = [ "futures 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "sqlx-core 0.1.4", "sqlx-macros 0.1.1", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1339,6 +1391,7 @@ dependencies = [ "rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", "sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", "sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1490,6 +1543,29 @@ dependencies = [ "tokio-timer 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", + "fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", + "futures-core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pin-project-lite 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-buf" version = "0.1.1" @@ -1528,6 +1604,15 @@ dependencies = [ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-macros" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-reactor" version = "0.1.11" @@ -1729,6 +1814,7 @@ dependencies = [ [metadata] "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" "checksum anyhow 1.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "7825f6833612eb2414095684fcf6c635becf3ce97fe48cf6421321e93bfbd53c" +"checksum arc-swap 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d7b8a9123b8027467bce0099fe556c628a53c8d83df0507084c31e9ba2e39aff" "checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" "checksum async-attributes 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "efd3d156917d94862e779f356c5acae312b08fd3121e792c857d7928c8088423" @@ -1752,6 +1838,7 @@ dependencies = [ "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" +"checksum bytes 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "10004c15deb332055f7a4a208190aed362cf9a7c2f6ab70a305fba50e1105f38" "checksum c2-chacha 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "214238caa1bf3a496ec3392968969cab8549f96ff30652c9e56885329315f6bb" "checksum cc 1.0.50 (registry+https://github.com/rust-lang/crates.io-index)" = "95e28fa049fda1c330bcf9d723be7663a899c4679724b34c81e9f5a326aab8cd" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" @@ -1826,8 +1913,10 @@ dependencies = [ "checksum memoffset 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "75189eb85871ea5c2e2c15abbdd541185f63b408415e5051f5cac122d8c774b9" "checksum mime 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" "checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" +"checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" +"checksum miow 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "396aa0f2003d7df8395cb93e09871561ccc3e785f0acb369170e8cc74ddf9226" "checksum native-tls 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4b2df1a4c22fd44a62147fd8f13dd0f95c9d8ca7b2610299b2a2f9cf8964274e" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum num-bigint 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f6f115de20ad793e857f76da2563ff4a09fbcfd6fe93cca0c5d996ab5f3ee38d" @@ -1878,9 +1967,11 @@ dependencies = [ "checksum serde_qs 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d43eef44996bbe16e99ac720e1577eefa16f7b76b5172165c98ced20ae9903e1" "checksum sha-1 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" +"checksum signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" "checksum smallvec 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44e59e0c9fa00817912ae6e4e6e3c4fe04455e75699d06eedc7d85917ed8e8f4" +"checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" "checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" "checksum string 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d24114bfcceb867ca7f71a0d3fe45d45619ec47a6fbfa98cb14e14250bfa5d6d" "checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" @@ -1893,10 +1984,12 @@ dependencies = [ "checksum tide 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13c99b1991db81e611a2614cd1b07fec89ae33c5f755e1f8eb70826fb5af0eea" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)" = "5a09c0b5bb588872ab2f09afa13ee6e9dac11e10a0ec9e8e3ba39a5a5d530af6" +"checksum tokio 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ffa2fdcfa937b20cb3c822a635ceecd5fc1a27a6a474527e5516aa24b8c8820a" "checksum tokio-buf 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8fb220f46c53859a4b7ec083e41dec9778ff0b1851c0942b211edb89e0ccdc46" "checksum tokio-current-thread 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d16217cad7f1b840c5a97dfb3c43b0c871fef423a6e8d2118c604e843662a443" "checksum tokio-executor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ca6df436c42b0c3330a82d855d2ef017cd793090ad550a6bc2184f4b933532ab" "checksum tokio-io 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5090db468dad16e1a7a54c8c67280c5e4b544f3d3e018f0b913b400261f85926" +"checksum tokio-macros 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "50a61f268a3db2acee8dcab514efc813dc6dbe8a00e86076f935f94304b59a7a" "checksum tokio-reactor 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "6732fe6b53c8d11178dcb77ac6d9682af27fc6d4cb87789449152e5377377146" "checksum tokio-sync 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d06554cce1ae4a50f42fba8023918afa931413aded705b560e29600ccf7c6d76" "checksum tokio-tcp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1d14b10654be682ac43efee27401d792507e30fd8d26389e1da3b185de2e4119" diff --git a/Cargo.toml b/Cargo.toml index 855d7b56c..0998fd367 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,9 +28,13 @@ authors = [ all-features = true [features] -default = [ "macros" ] +default = [ "macros", "runtime-async-std" ] macros = [ "sqlx-macros" ] -tls = ["sqlx-core/tls"] +tls = [ "sqlx-core/tls" ] + +# runtime +runtime-async-std = [ "sqlx-core/runtime-async-std" ] +runtime-tokio = [ "sqlx-core/runtime-tokio" ] # database postgres = [ "sqlx-core/postgres", "sqlx-macros/postgres" ] @@ -49,6 +53,7 @@ anyhow = "1.0.26" futures = "0.3.1" env_logger = "0.7" async-std = { version = "1.4.0", features = [ "attributes" ] } +tokio = { version = "0.2.0", features = [ "full" ] } dotenv = "0.15.0" [[test]] diff --git a/release.toml b/release.toml deleted file mode 100644 index 404963859..000000000 --- a/release.toml +++ /dev/null @@ -1,10 +0,0 @@ -# cargo-release -# https://github.com/sunng87/cargo-release/blob/master/release.toml - -no-dev-version = true -tag-name = "{{crate_name}}-v{{version}}" -pre-release-commit-message = "(cargo-release) {{crate_name}}: v{{version}}" -pre-release-replacements = [ - {file="Cargo.toml", search="sqlx-core = \\{ version = \"0.1.1\"", replace="sqlx-core = { version = \"={{version}}\""}, - {file="CHANGELOG.md", search="## Unreleased", replace="## Unreleased\n\n## {{version}} - {{date}}"}, -] diff --git a/sqlx-core/Cargo.toml b/sqlx-core/Cargo.toml index bb38bdf6c..29503b96d 100644 --- a/sqlx-core/Cargo.toml +++ b/sqlx-core/Cargo.toml @@ -20,11 +20,14 @@ default = [] unstable = [] postgres = [ "md-5", "sha2", "base64", "sha-1", "rand", "hmac" ] mysql = [ "sha-1", "sha2", "generic-array", "num-bigint", "base64", "digest", "rand" ] -tls = ["async-native-tls"] +tls = [ "async-native-tls" ] +runtime-async-std = [ "async-native-tls/runtime-async-std", "async-std" ] +runtime-tokio = [ "async-native-tls/runtime-tokio", "tokio" ] [dependencies] -async-native-tls = { version = "0.3", optional = true } -async-std = "1.4.0" +async-native-tls = { version = "0.3", default-features = false, optional = true } +async-std = { version = "1.4.0", optional = true } +tokio = { version = "0.2", default-features = false, features = [ "dns", "fs", "time", "tcp" ], optional = true } async-stream = { version = "0.2.0", default-features = false } base64 = { version = "0.11.0", default-features = false, optional = true, features = [ "std" ] } bitflags = { version = "1.2.1", default-features = false } diff --git a/sqlx-core/release.toml b/sqlx-core/release.toml deleted file mode 100644 index 445143641..000000000 --- a/sqlx-core/release.toml +++ /dev/null @@ -1,4 +0,0 @@ -# cargo-release -# https://github.com/sunng87/cargo-release/blob/master/release.toml - -pre-release-replacements = [] diff --git a/sqlx-core/src/io/buf_stream.rs b/sqlx-core/src/io/buf_stream.rs index b9ffda3cd..3c93b9652 100644 --- a/sqlx-core/src/io/buf_stream.rs +++ b/sqlx-core/src/io/buf_stream.rs @@ -1,10 +1,8 @@ -use async_std::io::{ - prelude::{ReadExt, WriteExt}, - Read, Write, -}; use std::io; use std::ops::{Deref, DerefMut}; +use crate::runtime::{AsyncRead, AsyncReadExt, AsyncWriteExt, AsyncWrite}; + const RBUF_SIZE: usize = 8 * 1024; pub struct BufStream { @@ -24,7 +22,7 @@ pub struct BufStream { impl BufStream where - S: Read + Write + Unpin, + S: AsyncRead + AsyncWrite + Unpin, { pub fn new(stream: S) -> Self { Self { diff --git a/sqlx-core/src/io/mod.rs b/sqlx-core/src/io/mod.rs index 304c11b65..ff825306a 100644 --- a/sqlx-core/src/io/mod.rs +++ b/sqlx-core/src/io/mod.rs @@ -4,7 +4,6 @@ mod buf_stream; mod buf; mod buf_mut; mod byte_str; - mod tls; pub use self::{ diff --git a/sqlx-core/src/io/tls.rs b/sqlx-core/src/io/tls.rs index 7743ebdcc..e6b29eb5d 100644 --- a/sqlx-core/src/io/tls.rs +++ b/sqlx-core/src/io/tls.rs @@ -1,10 +1,10 @@ use std::io::{IoSlice, IoSliceMut}; use std::pin::Pin; +use std::net::Shutdown; +use std::io; use std::task::{Context, Poll}; -use async_std::io::{self, Read, Write}; -use async_std::net::{Shutdown, TcpStream}; - +use crate::runtime::{TcpStream, AsyncRead, AsyncWrite}; use crate::url::Url; use self::Inner::*; @@ -57,7 +57,7 @@ impl MaybeTlsStream { Ok(()) } - pub fn shutdown(&self, how: Shutdown) -> io::Result<()> { + pub fn shutdown(&self, how:Shutdown) -> io::Result<()> { match self.inner { NotTls(ref conn) => conn.shutdown(how), #[cfg(feature = "tls")] @@ -81,7 +81,7 @@ macro_rules! forward_pin ( ) ); -impl Read for MaybeTlsStream { +impl AsyncRead for MaybeTlsStream { fn poll_read( mut self: Pin<&mut Self>, cx: &mut Context, @@ -90,6 +90,7 @@ impl Read for MaybeTlsStream { forward_pin!(self.poll_read(cx, buf)) } + #[cfg(feature = "runtime-async-std")] fn poll_read_vectored( mut self: Pin<&mut Self>, cx: &mut Context, @@ -99,7 +100,7 @@ impl Read for MaybeTlsStream { } } -impl Write for MaybeTlsStream { +impl AsyncWrite for MaybeTlsStream { fn poll_write( mut self: Pin<&mut Self>, cx: &mut Context, @@ -112,10 +113,17 @@ impl Write for MaybeTlsStream { forward_pin!(self.poll_flush(cx)) } + #[cfg(feature = "runtime-async-std")] fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { forward_pin!(self.poll_close(cx)) } + #[cfg(feature = "runtime-tokio")] + fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll> { + forward_pin!(self.poll_shutdown(cx)) + } + + #[cfg(feature = "runtime-async-std")] fn poll_write_vectored( mut self: Pin<&mut Self>, cx: &mut Context, diff --git a/sqlx-core/src/lib.rs b/sqlx-core/src/lib.rs index aee2cae10..5e6048f18 100644 --- a/sqlx-core/src/lib.rs +++ b/sqlx-core/src/lib.rs @@ -19,6 +19,9 @@ mod query_as; mod transaction; mod url; +#[doc(hidden)] +pub mod runtime; + #[macro_use] pub mod arguments; diff --git a/sqlx-core/src/mysql/connection.rs b/sqlx-core/src/mysql/connection.rs index d357e2d1e..54ee0424f 100644 --- a/sqlx-core/src/mysql/connection.rs +++ b/sqlx-core/src/mysql/connection.rs @@ -1,7 +1,7 @@ use std::convert::TryInto; use std::io; -use async_std::net::Shutdown; +use std::net::Shutdown; use byteorder::{ByteOrder, LittleEndian}; use futures_core::future::BoxFuture; use sha1::Sha1; @@ -452,7 +452,7 @@ impl MySqlConnection { invalid_hostnames: bool, ) -> crate::Result<()> { use async_native_tls::{Certificate, TlsConnector}; - use async_std::fs; + use crate::runtime::fs; let mut connector = TlsConnector::new() .danger_accept_invalid_certs(ca_file.is_none()) diff --git a/sqlx-core/src/pool/inner.rs b/sqlx-core/src/pool/inner.rs index 52f3a8763..611d97931 100644 --- a/sqlx-core/src/pool/inner.rs +++ b/sqlx-core/src/pool/inner.rs @@ -3,10 +3,10 @@ use std::sync::atomic::{AtomicBool, AtomicU32, Ordering}; use std::sync::Arc; use std::time::Instant; -use async_std::{future::timeout, task}; use crossbeam_queue::{ArrayQueue, SegQueue}; use futures_channel::oneshot::{channel, Sender}; +use crate::runtime::{sleep, spawn, timeout, yield_now}; use super::{Idle, Live, Options}; use crate::{ connection::{Connect, Connection}, @@ -90,7 +90,7 @@ where self.size.fetch_sub(1, Ordering::AcqRel); } - task::yield_now().await + yield_now().await } } @@ -341,7 +341,7 @@ where let pool = Arc::clone(&pool); - task::spawn(async move { + spawn(async move { while !pool.is_closed.load(Ordering::Acquire) { // reap at most the current size minus the minimum idle let max_reaped = pool @@ -368,7 +368,7 @@ where pool.size.fetch_sub(1, Ordering::AcqRel); } - task::sleep(period).await; + sleep(period).await; } }); } diff --git a/sqlx-core/src/postgres/connection.rs b/sqlx-core/src/postgres/connection.rs index 2be7d6d7a..188d847d8 100644 --- a/sqlx-core/src/postgres/connection.rs +++ b/sqlx-core/src/postgres/connection.rs @@ -1,6 +1,6 @@ use std::convert::TryInto; -use async_std::net::Shutdown; +use std::net::Shutdown; use byteorder::NetworkEndian; use futures_core::future::BoxFuture; use hmac::{Hmac, Mac}; @@ -440,7 +440,7 @@ async fn read_root_certificate(url: &Url) -> crate::Result 0 { if let Some(mut inner) = self.inner.take() { - task::spawn(async move { + spawn(async move { let res = inner.send("ROLLBACK").await; // If the rollback failed we need to close the inner connection diff --git a/sqlx-macros/src/query_macros/input.rs b/sqlx-macros/src/query_macros/input.rs index 5c6f912e8..462fa8356 100644 --- a/sqlx-macros/src/query_macros/input.rs +++ b/sqlx-macros/src/query_macros/input.rs @@ -1,6 +1,6 @@ use std::env; -use async_std::fs; +use sqlx::runtime::fs; use proc_macro2::{Ident, Span, TokenStream}; use syn::parse::{Parse, ParseStream}; use syn::punctuated::Punctuated; diff --git a/src/lib.rs b/src/lib.rs index 2df1ea045..5d249e341 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,11 @@ #![allow(dead_code)] +#[cfg(not(any(feature = "runtime-tokio", feature = "runtime-async-std")))] +compile_error!("one of 'runtime-async-std' or 'runtime-tokio' features must be enabled"); + +#[cfg(all(feature = "runtime-tokio", feature = "runtime-async-std"))] +compile_error!("only one of 'runtime-async-std' or 'runtime-tokio' features must be enabled"); + // Modules pub use sqlx_core::{arguments, decode, describe, encode, error, pool, row, types}; diff --git a/test.sh b/test.sh deleted file mode 100755 index 48a86abb7..000000000 --- a/test.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/usr/bin/env bash -set -e - -# Core -cargo test -p sqlx-core --all-features - -# Postgres -env DATABASE_URL="postgres://" cargo test -p sqlx --no-default-features --features 'postgres macros uuid chrono' - -# MySQL (requires sqlx database) -env DATABASE_URL="mysql:///sqlx" cargo test -p sqlx --no-default-features --features 'mysql chrono' diff --git a/tests/mysql-types-chrono.rs b/tests/mysql-types-chrono.rs index f8eecc9c9..72a148a22 100644 --- a/tests/mysql-types-chrono.rs +++ b/tests/mysql-types-chrono.rs @@ -5,7 +5,8 @@ async fn connect() -> anyhow::Result { Ok(MySqlConnection::open(dotenv::var("DATABASE_URL")?).await?) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_chrono_date() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -25,7 +26,8 @@ async fn mysql_chrono_date() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_chrono_date_time() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -43,7 +45,8 @@ async fn mysql_chrono_date_time() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_chrono_time() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -60,7 +63,8 @@ async fn mysql_chrono_time() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_chrono_timestamp() -> anyhow::Result<()> { let mut conn = connect().await?; diff --git a/tests/mysql-types.rs b/tests/mysql-types.rs index 23f7f13a3..fc66b7ac3 100644 --- a/tests/mysql-types.rs +++ b/tests/mysql-types.rs @@ -6,7 +6,8 @@ async fn connect() -> anyhow::Result { macro_rules! test { ($name:ident: $ty:ty: $($text:literal == $value:expr),+) => { - #[async_std::test] + #[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn $name () -> anyhow::Result<()> { let mut conn = connect().await?; @@ -45,7 +46,8 @@ test!(mysql_longlong: i64: "2141512" == 2141512_i64); test!(mysql_string: String: "'helloworld'" == "helloworld"); -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_bytes() -> anyhow::Result<()> { let mut conn = connect().await?; diff --git a/tests/mysql.rs b/tests/mysql.rs index 0085b2eb0..cb946fc33 100644 --- a/tests/mysql.rs +++ b/tests/mysql.rs @@ -1,7 +1,8 @@ use futures::TryStreamExt; use sqlx::{Connection as _, Executor as _, MySqlConnection, MySqlPool, Row as _}; -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn it_connects() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -14,7 +15,8 @@ async fn it_connects() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn it_executes() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -48,7 +50,8 @@ CREATE TEMPORARY TABLE users (id INTEGER PRIMARY KEY) Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn pool_immediately_fails_with_db_error() -> anyhow::Result<()> { // Malform the database url by changing the password let url = url()?.replace("password", "not-the-password"); @@ -71,7 +74,8 @@ async fn pool_immediately_fails_with_db_error() -> anyhow::Result<()> { } #[cfg(feature = "macros")] -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn macro_select_from_cte() -> anyhow::Result<()> { let mut conn = connect().await?; let account = @@ -86,7 +90,8 @@ async fn macro_select_from_cte() -> anyhow::Result<()> { } #[cfg(feature = "macros")] -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn macro_select_from_cte_bind() -> anyhow::Result<()> { let mut conn = connect().await?; let account = sqlx::query!( diff --git a/tests/postgres-macros.rs b/tests/postgres-macros.rs index e33d3e4eb..b8211f36c 100644 --- a/tests/postgres-macros.rs +++ b/tests/postgres-macros.rs @@ -1,6 +1,9 @@ -#[async_std::test] -async fn test_query() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +use sqlx::{PgConnection, Connect}; + +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query() -> anyhow::Result<()> { + let mut conn = connect().await?; let account = sqlx::query!( "SELECT * from (VALUES (1, 'Herp Derpinson')) accounts(id, name) where id = $1", @@ -14,9 +17,10 @@ async fn test_query() -> sqlx::Result<()> { Ok(()) } -#[async_std::test] -async fn test_query_file() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query_file() -> anyhow::Result<()> { + let mut conn = connect().await?; let account = sqlx::query_file!("tests/test-query.sql") .fetch_one(&mut conn) @@ -33,9 +37,10 @@ struct Account { name: Option, } -#[async_std::test] -async fn test_query_as() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query_as() -> anyhow::Result<()> { + let mut conn = connect().await?; let account = sqlx::query_as!( Account, @@ -51,9 +56,10 @@ async fn test_query_as() -> sqlx::Result<()> { Ok(()) } -#[async_std::test] -async fn test_query_file_as() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_query_file_as() -> anyhow::Result<()> { + let mut conn = connect().await?; let account = sqlx::query_file_as!(Account, "tests/test-query.sql") .fetch_one(&mut conn) @@ -64,9 +70,10 @@ async fn test_query_file_as() -> sqlx::Result<()> { Ok(()) } -#[async_std::test] -async fn query_by_string() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn query_by_string() -> anyhow::Result<()> { + let mut conn = connect().await?; let string = "Hello, world!".to_string(); @@ -84,15 +91,16 @@ async fn query_by_string() -> sqlx::Result<()> { Ok(()) } -#[async_std::test] -async fn test_nullable_err() -> sqlx::Result<()> { +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_nullable_err() -> anyhow::Result<()> { #[derive(Debug)] struct Account { id: i32, name: String, } - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; + let mut conn = connect().await?; let err = sqlx::query_as!( Account, @@ -109,9 +117,10 @@ async fn test_nullable_err() -> sqlx::Result<()> { } } -#[async_std::test] -async fn test_many_args() -> sqlx::Result<()> { - let mut conn = sqlx::postgres::connect(&dotenv::var("DATABASE_URL").unwrap()).await?; +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] +async fn test_many_args() -> anyhow::Result<()> { + let mut conn = connect().await?; // previous implementation would only have supported 10 bind parameters // (this is really gross to test in MySQL) @@ -128,3 +137,10 @@ async fn test_many_args() -> sqlx::Result<()> { Ok(()) } + +async fn connect() -> anyhow::Result { + let _ = dotenv::dotenv(); + let _ = env_logger::try_init(); + + Ok(PgConnection::connect(dotenv::var("DATABASE_URL")?).await?) +} diff --git a/tests/postgres-types-chrono.rs b/tests/postgres-types-chrono.rs index c62421527..49b168ae6 100644 --- a/tests/postgres-types-chrono.rs +++ b/tests/postgres-types-chrono.rs @@ -5,7 +5,8 @@ async fn connect() -> anyhow::Result { Ok(PgConnection::open(dotenv::var("DATABASE_URL")?).await?) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn postgres_chrono_date() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -22,7 +23,8 @@ async fn postgres_chrono_date() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn mysql_chrono_date_time() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -39,7 +41,8 @@ async fn mysql_chrono_date_time() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn postgres_chrono_time() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -56,7 +59,8 @@ async fn postgres_chrono_time() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn postgres_chrono_timestamp_tz() -> anyhow::Result<()> { let mut conn = connect().await?; diff --git a/tests/postgres-types.rs b/tests/postgres-types.rs index 5db8b9ff3..177c8e0ae 100644 --- a/tests/postgres-types.rs +++ b/tests/postgres-types.rs @@ -6,7 +6,8 @@ async fn connect() -> anyhow::Result { macro_rules! test { ($name:ident: $ty:ty: $($text:literal == $value:expr),+) => { - #[async_std::test] + #[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn $name () -> anyhow::Result<()> { let mut conn = connect().await?; @@ -36,7 +37,8 @@ test!(postgres_double: f64: "939399419.1225182::double precision" == 939399419.1 test!(postgres_text: String: "'this is foo'" == "this is foo", "''" == ""); -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn postgres_bytes() -> anyhow::Result<()> { let mut conn = connect().await?; diff --git a/tests/postgres.rs b/tests/postgres.rs index 68ee5340e..ce105e44c 100644 --- a/tests/postgres.rs +++ b/tests/postgres.rs @@ -1,7 +1,8 @@ use futures::TryStreamExt; use sqlx::{postgres::PgConnection, Connection as _, Executor as _, Row as _}; -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn it_connects() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -14,7 +15,8 @@ async fn it_connects() -> anyhow::Result<()> { Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn it_executes() -> anyhow::Result<()> { let mut conn = connect().await?; @@ -48,7 +50,8 @@ CREATE TEMPORARY TABLE users (id INTEGER PRIMARY KEY); Ok(()) } -#[async_std::test] +#[cfg_attr(feature = "runtime-async-std", async_std::test)] +#[cfg_attr(feature = "runtime-tokio", tokio::test)] async fn it_remains_stable_issue_30() -> anyhow::Result<()> { let mut conn = connect().await?;