Skip to content

Commit 84a51a5

Browse files
jan-auermitsuhiko
authored andcommitted
Reduce dependencies without AIO (redis-rs#266)
1 parent 916a59d commit 84a51a5

File tree

8 files changed

+63
-22
lines changed

8 files changed

+63
-22
lines changed

Cargo.toml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ combine = "3.8.1"
2424
bytes = "0.5"
2525
futures-util = { version = "0.3.0", features = ["sink"], default-features = false }
2626
futures-executor = "0.3.0"
27-
pin-project-lite = "0.1"
27+
pin-project-lite = { version = "0.1", optional = true }
2828
tokio-util = { version = "0.2", features = ["codec"] }
29-
tokio = { version = "0.2", features = ["tcp", "uds", "io-util", "sync", "stream"] }
29+
tokio = "0.2"
3030
crc16 = { version = "0.4.0", optional = true }
3131
rand = { version = "0.7.0", optional = true }
3232
r2d2 = { version = "0.8.8", optional = true }
3333

3434
[features]
35-
default = ["geospatial"]
36-
tokio-rt-core = ["tokio/rt-core"]
35+
default = ["geospatial", "aio"]
36+
aio = ["pin-project-lite", "tokio/sync", "tokio/stream", "tokio/tcp", "tokio/uds", "tokio/io-util"]
37+
tokio-rt-core = ["aio", "tokio/rt-core"]
3738
geospatial = []
3839
cluster = ["crc16", "rand"]
3940

@@ -52,10 +53,18 @@ tokio = { version = "0.2", features = ["rt-core", "macros"] }
5253
name = "test_async"
5354
required-features = ["tokio-rt-core"]
5455

56+
[[test]]
57+
name = "parser"
58+
required-features = ["aio"]
59+
5560
[[bench]]
5661
name = "bench_basic"
5762
harness = false
5863

5964
[[example]]
6065
name = "async-multiplexed"
6166
required-features = ["tokio-rt-core"]
67+
68+
[[example]]
69+
name = "async-await"
70+
required-features = ["aio"]

Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ build:
33

44
test:
55
@echo "===================================================================="
6-
@echo "Testing Connection Type TCP"
6+
@echo "Testing Connection Type TCP without features"
7+
@echo "===================================================================="
8+
@REDISRS_SERVER_TYPE=tcp RUST_TEST_THREADS=1 cargo test --no-default-features --tests -- --nocapture
9+
10+
@echo "===================================================================="
11+
@echo "Testing Connection Type TCP with all features"
712
@echo "===================================================================="
813
@REDISRS_SERVER_TYPE=tcp RUST_TEST_THREADS=1 cargo test --all-features -- --nocapture
14+
15+
@echo "===================================================================="
916
@echo "Testing Connection Type UNIX"
1017
@echo "===================================================================="
1118
@REDISRS_SERVER_TYPE=unix cargo test --test parser --test test_basic --test test_types --all-features
19+
1220
@echo "===================================================================="
1321
@echo "Testing Connection Type UNIX SOCKETS"
1422
@echo "===================================================================="

src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::future::Future;
21
use std::time::Duration;
32

43
use crate::connection::{connect, Connection, ConnectionInfo, ConnectionLike, IntoConnectionInfo};
@@ -55,6 +54,7 @@ impl Client {
5554
}
5655

5756
/// Returns an async connection from the client.
57+
#[cfg(feature = "aio")]
5858
pub async fn get_async_connection(&self) -> RedisResult<crate::aio::Connection> {
5959
crate::aio::connect(&self.connection_info).await
6060
}
@@ -80,9 +80,13 @@ impl Client {
8080
///
8181
/// A multiplexed connection can be cloned, allowing requests to be be sent concurrently
8282
/// on the same underlying connection (tcp/unix socket).
83+
#[cfg(feature = "aio")]
8384
pub async fn get_multiplexed_async_connection(
8485
&self,
85-
) -> RedisResult<(crate::aio::MultiplexedConnection, impl Future<Output = ()>)> {
86+
) -> RedisResult<(
87+
crate::aio::MultiplexedConnection,
88+
impl std::future::Future<Output = ()>,
89+
)> {
8690
let con = self.get_async_connection().await?;
8791
Ok(crate::aio::MultiplexedConnection::new(con))
8892
}

src/cmd.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
use std::{io, pin::Pin};
1+
use std::io;
22

33
use crate::connection::ConnectionLike;
44
use crate::types::{
55
from_redis_value, ErrorKind, FromRedisValue, RedisResult, RedisWrite, ToRedisArgs, Value,
66
};
77

8-
use tokio::io::{AsyncWrite, AsyncWriteExt};
8+
#[cfg(feature = "aio")]
9+
use {
10+
std::pin::Pin,
11+
tokio::io::{AsyncWrite, AsyncWriteExt},
12+
};
913

1014
/// An argument to a redis command
1115
#[derive(Clone)]
@@ -162,6 +166,7 @@ where
162166
Ok(())
163167
}
164168

169+
#[cfg(feature = "aio")]
165170
async fn write_command_async<'a, I>(
166171
mut cmd: Pin<&mut (impl ?Sized + AsyncWrite)>,
167172
args: I,
@@ -217,6 +222,7 @@ fn encode_pipeline(cmds: &[Cmd], atomic: bool) -> Vec<u8> {
217222
rv
218223
}
219224

225+
#[cfg(feature = "aio")]
220226
async fn write_pipeline_async(
221227
mut out: Pin<&mut (impl ?Sized + AsyncWrite)>,
222228
cmds: &[Cmd],
@@ -338,6 +344,7 @@ impl Cmd {
338344
cmd
339345
}
340346

347+
#[cfg(feature = "aio")]
341348
pub(crate) async fn write_command_async(
342349
&self,
343350
out: Pin<&mut (impl ?Sized + AsyncWrite)>,
@@ -385,6 +392,7 @@ impl Cmd {
385392

386393
/// Async version of `query`.
387394
#[inline]
395+
#[cfg(feature = "aio")]
388396
pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T>
389397
where
390398
C: crate::aio::ConnectionLike,
@@ -596,6 +604,7 @@ impl Pipeline {
596604
encode_pipeline(&self.commands, self.transaction_mode)
597605
}
598606

607+
#[cfg(feature = "aio")]
599608
pub(crate) async fn write_pipeline_async(
600609
&self,
601610
out: Pin<&mut (impl ?Sized + AsyncWrite)>,
@@ -672,6 +681,7 @@ impl Pipeline {
672681
self.commands.clear();
673682
}
674683

684+
#[cfg(feature = "aio")]
675685
async fn execute_pipelined_async<C>(&self, con: &mut C) -> RedisResult<Value>
676686
where
677687
C: crate::aio::ConnectionLike,
@@ -682,6 +692,7 @@ impl Pipeline {
682692
Ok(self.make_pipeline_results(value))
683693
}
684694

695+
#[cfg(feature = "aio")]
685696
async fn execute_transaction_async<C>(&self, con: &mut C) -> RedisResult<Value>
686697
where
687698
C: crate::aio::ConnectionLike,
@@ -702,6 +713,7 @@ impl Pipeline {
702713

703714
/// Async version of `query`.
704715
#[inline]
716+
#[cfg(feature = "aio")]
705717
pub async fn query_async<C, T: FromRedisValue>(&self, con: &mut C) -> RedisResult<T>
706718
where
707719
C: crate::aio::ConnectionLike,

src/commands.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// can't use rustfmt here because it screws up the file.
22
#![cfg_attr(rustfmt, rustfmt_skip)]
3-
use crate::types::{FromRedisValue, ToRedisArgs, RedisResult, RedisFuture, NumericBehavior};
3+
use crate::types::{FromRedisValue, ToRedisArgs, RedisResult, NumericBehavior};
44
use crate::connection::{ConnectionLike, Msg, Connection};
55
use crate::cmd::{cmd, Cmd, Pipeline, Iter};
66

@@ -160,14 +160,15 @@ macro_rules! implement_commands {
160160
/// assert_eq!(con.get("my_key").await, Ok(42i32));
161161
/// # Ok(()) }
162162
/// ```
163+
#[cfg(feature = "aio")]
163164
pub trait AsyncCommands : crate::aio::ConnectionLike + Send + Sized {
164165
$(
165166
$(#[$attr])*
166167
#[inline]
167168
fn $name<$lifetime, $($tyargs: $ty + Send + Sync + $lifetime,)* RV>(
168169
& $lifetime mut self
169170
$(, $argname: $argty)*
170-
) -> RedisFuture<'a, RV>
171+
) -> crate::types::RedisFuture<'a, RV>
171172
where RV: FromRedisValue,
172173
{
173174
Box::pin(async move { ($body).query_async(self).await })
@@ -1094,6 +1095,7 @@ pub trait PubSubCommands: Sized {
10941095

10951096
impl<T> Commands for T where T: ConnectionLike {}
10961097

1098+
#[cfg(feature = "aio")]
10971099
impl<T> AsyncCommands for T where T: crate::aio::ConnectionLike + Send + ?Sized {}
10981100

10991101
impl PubSubCommands for Connection {

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@
335335
// public api
336336
pub use crate::client::Client;
337337
pub use crate::cmd::{cmd, pack_command, pipe, Arg, Cmd, Iter, Pipeline};
338-
pub use crate::commands::{AsyncCommands, Commands, ControlFlow, PubSubCommands};
338+
pub use crate::commands::{Commands, ControlFlow, PubSubCommands};
339339
pub use crate::connection::{
340340
parse_redis_url, transaction, Connection, ConnectionAddr, ConnectionInfo, ConnectionLike,
341341
IntoConnectionInfo, Msg, PubSub,
@@ -368,8 +368,12 @@ pub use crate::types::{
368368
Value,
369369
};
370370

371+
#[cfg(feature = "aio")]
372+
pub use crate::commands::AsyncCommands;
373+
371374
mod macros;
372375

376+
#[cfg(feature = "aio")]
373377
pub mod aio;
374378

375379
#[cfg(feature = "geospatial")]

src/script.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use sha1::Sha1;
22

3-
use crate::aio;
43
use crate::cmd::cmd;
54
use crate::connection::ConnectionLike;
65
use crate::types::{ErrorKind, FromRedisValue, RedisResult, ToRedisArgs};
@@ -150,9 +149,10 @@ impl<'a> ScriptInvocation<'a> {
150149

151150
/// Asynchronously invokes the script and returns the result.
152151
#[inline]
152+
#[cfg(feature = "aio")]
153153
pub async fn invoke_async<C, T>(&self, con: &mut C) -> RedisResult<T>
154154
where
155-
C: aio::ConnectionLike,
155+
C: crate::aio::ConnectionLike,
156156
T: FromRedisValue,
157157
{
158158
let mut eval_cmd = cmd("EVALSHA");

tests/support/mod.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ use std::path::PathBuf;
1717

1818
use self::futures::Future;
1919

20-
use redis::{RedisResult, Value};
20+
use redis::Value;
2121

2222
pub fn current_thread_runtime() -> tokio::runtime::Runtime {
23-
tokio::runtime::Builder::new()
24-
.basic_scheduler()
25-
.enable_io()
26-
.build()
27-
.unwrap()
23+
let mut builder = tokio::runtime::Builder::new();
24+
25+
#[cfg(feature = "aio")]
26+
builder.enable_io();
27+
28+
builder.basic_scheduler().build().unwrap()
2829
}
2930

3031
pub fn block_on_all<F>(f: F) -> F::Output
@@ -185,7 +186,8 @@ impl TestContext {
185186
self.client.get_connection().unwrap()
186187
}
187188

188-
pub async fn async_connection(&self) -> RedisResult<redis::aio::Connection> {
189+
#[cfg(feature = "aio")]
190+
pub async fn async_connection(&self) -> redis::RedisResult<redis::aio::Connection> {
189191
self.client.get_async_connection().await
190192
}
191193

@@ -196,7 +198,7 @@ impl TestContext {
196198
#[cfg(feature = "tokio-rt-core")]
197199
pub fn multiplexed_async_connection(
198200
&self,
199-
) -> impl Future<Output = RedisResult<redis::aio::MultiplexedConnection>> {
201+
) -> impl Future<Output = redis::RedisResult<redis::aio::MultiplexedConnection>> {
200202
let client = self.client.clone();
201203
async move { client.get_multiplexed_tokio_connection().await }
202204
}

0 commit comments

Comments
 (0)