Skip to content

Commit 645e576

Browse files
committed
Simplify connector type, drop .boxed_connector() method
1 parent ecbaa7a commit 645e576

File tree

7 files changed

+47
-102
lines changed

7 files changed

+47
-102
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changes
22

3+
## [0.3.1] - 2022-01-10
4+
5+
* Simplify connector type, drop .boxed_connector() method
6+
7+
* Remove openssl and rustls features
8+
39
## [0.3.0] - 2021-12-30
410

511
* Upgrade to ntex 0.5.0

Cargo.toml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-redis"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
authors = ["ntex contributors <[email protected]>"]
55
description = "Redis client"
66
documentation = "https://docs.rs/ntex-redis"
@@ -11,17 +11,8 @@ license = "MIT"
1111
exclude = [".gitignore", ".travis.yml", ".cargo/config"]
1212
edition = "2018"
1313

14-
[features]
15-
default = []
16-
17-
# openssl
18-
openssl = ["ntex/openssl"]
19-
20-
# rustls support
21-
rustls = ["ntex/rustls"]
22-
2314
[dependencies]
24-
ntex = "0.5.0"
15+
ntex = "0.5.8"
2516
itoa = "0.4.5"
2617
btoi = "0.4.2"
2718
log = "0.4"
@@ -30,3 +21,4 @@ derive_more = "0.99"
3021
[dev-dependencies]
3122
rand = "0.8"
3223
env_logger = "0.9"
24+
ntex = { version = "0.5", features = ["tokio"] }

src/client.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::VecDeque;
22
use std::{cell::RefCell, fmt, future::Future, pin::Pin, rc::Rc, task::Context, task::Poll};
33

4-
use ntex::io::{utils::OnDisconnect, IoBoxed, IoRef, RecvError};
4+
use ntex::io::{IoBoxed, IoRef, OnDisconnect, RecvError};
55
use ntex::util::{poll_fn, ready, Either, Ready};
66
use ntex::{channel::pool, service::Service};
77

@@ -146,11 +146,10 @@ pub struct CommandResult {
146146
impl Future for CommandResult {
147147
type Output = Result<Response, Error>;
148148

149-
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
150-
match Pin::new(&mut self.rx).poll(cx) {
151-
Poll::Ready(Ok(res)) => Poll::Ready(res),
152-
Poll::Ready(Err(_)) => Poll::Ready(Err(Error::PeerGone(None))),
153-
Poll::Pending => Poll::Pending,
149+
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
150+
match ready!(self.rx.poll_recv(cx)) {
151+
Ok(res) => Poll::Ready(res),
152+
Err(_) => Poll::Ready(Err(Error::PeerGone(None))),
154153
}
155154
}
156155
}

src/connector.rs

Lines changed: 26 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
use std::future::Future;
22

33
use ntex::connect::{self, Address, Connect, Connector};
4-
use ntex::io::{utils::Boxed, IoBoxed};
4+
use ntex::io::IoBoxed;
55
use ntex::{service::Service, time::Seconds, util::ByteString, util::PoolId, util::PoolRef};
66

7-
#[cfg(feature = "openssl")]
8-
use ntex::connect::openssl::{self, SslConnector};
9-
10-
#[cfg(feature = "rustls")]
11-
use ntex::connect::rustls::{self, ClientConfig};
12-
137
use super::errors::ConnectError;
148
use super::{cmd, Client, SimpleClient};
159

@@ -27,17 +21,20 @@ where
2721
{
2822
#[allow(clippy::new_ret_no_self)]
2923
/// Create new redis connector
30-
pub fn new(address: A) -> RedisConnector<A, Boxed<Connector<A>, Connect<A>>> {
24+
pub fn new(address: A) -> RedisConnector<A, Connector<A>> {
3125
RedisConnector {
3226
address,
3327
passwords: Vec::new(),
34-
connector: Connector::default().seal(),
28+
connector: Connector::default(),
3529
pool: PoolId::P7.pool_ref(),
3630
}
3731
}
3832
}
3933

40-
impl<A, T> RedisConnector<A, T> {
34+
impl<A, T> RedisConnector<A, T>
35+
where
36+
A: Address + Clone,
37+
{
4138
/// Add redis auth password
4239
pub fn password<U>(mut self, password: U) -> Self
4340
where
@@ -58,23 +55,10 @@ impl<A, T> RedisConnector<A, T> {
5855
}
5956

6057
/// Use custom connector
61-
pub fn connector<Io, U>(self, connector: U) -> RedisConnector<A, Boxed<U, Connect<A>>>
62-
where
63-
U: Service<Connect<A>, Response = Io, Error = connect::ConnectError>,
64-
IoBoxed: From<Io>,
65-
{
66-
RedisConnector {
67-
connector: Boxed::new(connector),
68-
address: self.address,
69-
passwords: self.passwords,
70-
pool: self.pool,
71-
}
72-
}
73-
74-
/// Use custom boxed connector
75-
pub fn boxed_connector<U>(self, connector: U) -> RedisConnector<A, U>
58+
pub fn connector<U>(self, connector: U) -> RedisConnector<A, U>
7659
where
77-
U: Service<Connect<A>, Response = IoBoxed, Error = connect::ConnectError>,
60+
U: Service<Connect<A>, Error = connect::ConnectError>,
61+
IoBoxed: From<U::Response>,
7862
{
7963
RedisConnector {
8064
connector,
@@ -83,90 +67,48 @@ impl<A, T> RedisConnector<A, T> {
8367
pool: self.pool,
8468
}
8569
}
86-
87-
#[cfg(feature = "openssl")]
88-
/// Use openssl connector.
89-
pub fn openssl(
90-
self,
91-
connector: SslConnector,
92-
) -> RedisConnector<A, Boxed<openssl::Connector<A>, Connect<A>>> {
93-
RedisConnector {
94-
address: self.address,
95-
passwords: self.passwords,
96-
connector: Boxed::new(openssl::Connector::new(connector)),
97-
pool: self.pool,
98-
}
99-
}
100-
101-
#[cfg(feature = "rustls")]
102-
/// Use rustls connector.
103-
pub fn rustls(
104-
self,
105-
config: ClientConfig,
106-
) -> RedisConnector<A, Boxed<rustls::Connector<A>, Connect<A>>> {
107-
RedisConnector {
108-
address: self.address,
109-
passwords: self.passwords,
110-
connector: Boxed::new(rustls::Connector::new(config)),
111-
pool: self.pool,
112-
}
113-
}
11470
}
11571

11672
impl<A, T> RedisConnector<A, T>
11773
where
11874
A: Address + Clone,
119-
T: Service<Connect<A>, Response = IoBoxed, Error = connect::ConnectError>,
75+
T: Service<Connect<A>, Error = connect::ConnectError>,
76+
IoBoxed: From<T::Response>,
12077
{
121-
/// Connect to redis server and create shared client
122-
pub fn connect(&self) -> impl Future<Output = Result<Client, ConnectError>> {
78+
fn _connect(&self) -> impl Future<Output = Result<IoBoxed, ConnectError>> {
12379
let pool = self.pool;
12480
let passwords = self.passwords.clone();
12581
let fut = self.connector.call(Connect::new(self.address.clone()));
12682

12783
async move {
128-
let io = fut.await?;
84+
let io = IoBoxed::from(fut.await?);
12985
io.set_memory_pool(pool);
13086
io.set_disconnect_timeout(Seconds::ZERO.into());
13187

132-
let client = Client::new(io);
133-
13488
if passwords.is_empty() {
135-
Ok(client)
89+
Ok(io)
13690
} else {
91+
let client = SimpleClient::new(io);
92+
13793
for password in passwords {
13894
if client.exec(cmd::Auth(password)).await? {
139-
return Ok(client);
95+
return Ok(client.into_inner());
14096
}
14197
}
14298
Err(ConnectError::Unauthorized)
14399
}
144100
}
145101
}
146102

103+
/// Connect to redis server and create shared client
104+
pub fn connect(&self) -> impl Future<Output = Result<Client, ConnectError>> {
105+
let fut = self._connect();
106+
async move { fut.await.map(|io| Client::new(io)) }
107+
}
108+
147109
/// Connect to redis server and create simple client
148110
pub fn connect_simple(&self) -> impl Future<Output = Result<SimpleClient, ConnectError>> {
149-
let pool = self.pool;
150-
let passwords = self.passwords.clone();
151-
let fut = self.connector.call(Connect::new(self.address.clone()));
152-
153-
async move {
154-
let io = fut.await?;
155-
io.set_memory_pool(pool);
156-
io.set_disconnect_timeout(Seconds::ZERO.into());
157-
158-
let client = SimpleClient::new(io);
159-
160-
if passwords.is_empty() {
161-
Ok(client)
162-
} else {
163-
for password in passwords {
164-
if client.exec(cmd::Auth(password)).await? {
165-
return Ok(client);
166-
}
167-
}
168-
Err(ConnectError::Unauthorized)
169-
}
170-
}
111+
let fut = self._connect();
112+
async move { fut.await.map(|io| SimpleClient::new(io)) }
171113
}
172114
}

src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub enum CommandError {
6464
Error(ByteString),
6565

6666
/// A command response parse error
67-
#[display(fmt = "Command output error: {}", _0)]
67+
#[display(fmt = "Command output parse error: {}", _0)]
6868
Output(&'static str, Response),
6969

7070
/// Redis protocol level errors

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
//! Ok(())
3030
//! }
3131
//! ```
32+
#![allow(clippy::return_self_not_must_use)]
33+
3234
mod client;
3335
pub mod cmd;
3436
pub mod codec;

src/simple.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ impl SimpleClient {
4545
})
4646
.await
4747
}
48+
49+
pub(crate) fn into_inner(self) -> IoBoxed {
50+
self.io
51+
}
4852
}

0 commit comments

Comments
 (0)