Skip to content

Commit 940cbb8

Browse files
committed
Remove future from MakeTlsMode
It's unlikely to be useful in practice, and just introduces more complexity.
1 parent 0ae7670 commit 940cbb8

File tree

5 files changed

+44
-107
lines changed

5 files changed

+44
-107
lines changed

postgres/src/client.rs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ impl Client {
1818
T: MakeTlsMode<Socket> + 'static + Send,
1919
T::TlsMode: Send,
2020
T::Stream: Send,
21-
T::Future: Send,
2221
<T::TlsMode as TlsMode<Socket>>::Future: Send,
2322
{
2423
params.parse::<Config>()?.connect(tls_mode)

postgres/src/config.rs

-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ impl Config {
9797
T: MakeTlsMode<Socket> + 'static + Send,
9898
T::TlsMode: Send,
9999
T::Stream: Send,
100-
T::Future: Send,
101100
<T::TlsMode as TlsMode<Socket>>::Future: Send,
102101
{
103102
let connect = self.0.connect(tls_mode);

tokio-postgres-openssl/src/lib.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#![warn(rust_2018_idioms, clippy::all)]
22

3-
#[cfg(feature = "runtime")]
4-
use futures::future::{self, FutureResult};
53
use futures::{try_ready, Async, Future, Poll};
64
#[cfg(feature = "runtime")]
75
use openssl::error::ErrorStack;
@@ -44,12 +42,6 @@ impl MakeTlsConnector {
4442
{
4543
self.config = Arc::new(f);
4644
}
47-
48-
fn make_tls_connect_inner(&mut self, domain: &str) -> Result<TlsConnector, ErrorStack> {
49-
let mut ssl = self.connector.configure()?;
50-
(self.config)(&mut ssl)?;
51-
Ok(TlsConnector::new(ssl, domain))
52-
}
5345
}
5446

5547
#[cfg(feature = "runtime")]
@@ -60,10 +52,11 @@ where
6052
type Stream = SslStream<S>;
6153
type TlsConnect = TlsConnector;
6254
type Error = ErrorStack;
63-
type Future = FutureResult<TlsConnector, ErrorStack>;
6455

65-
fn make_tls_connect(&mut self, domain: &str) -> FutureResult<TlsConnector, ErrorStack> {
66-
future::result(self.make_tls_connect_inner(domain))
56+
fn make_tls_connect(&mut self, domain: &str) -> Result<TlsConnector, ErrorStack> {
57+
let mut ssl = self.connector.configure()?;
58+
(self.config)(&mut ssl)?;
59+
Ok(TlsConnector::new(ssl, domain))
6760
}
6861
}
6962

tokio-postgres/src/proto/connect.rs

+32-49
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use futures::{try_ready, Async, Future, Poll};
1+
use futures::{Async, Future, Poll};
22
use state_machine_future::{transition, RentToOwn, StateMachineFuture};
33

44
use crate::proto::{Client, ConnectOnceFuture, Connection};
@@ -9,19 +9,12 @@ pub enum Connect<T>
99
where
1010
T: MakeTlsMode<Socket>,
1111
{
12-
#[state_machine_future(start, transitions(MakingTlsMode))]
12+
#[state_machine_future(start, transitions(Connecting))]
1313
Start {
1414
make_tls_mode: T,
1515
config: Result<Config, Error>,
1616
},
17-
#[state_machine_future(transitions(Connecting))]
18-
MakingTlsMode {
19-
future: T::Future,
20-
idx: usize,
21-
make_tls_mode: T,
22-
config: Config,
23-
},
24-
#[state_machine_future(transitions(MakingTlsMode, Finished))]
17+
#[state_machine_future(transitions(Finished))]
2518
Connecting {
2619
future: ConnectOnceFuture<T::TlsMode>,
2720
idx: usize,
@@ -57,58 +50,48 @@ where
5750
#[cfg(unix)]
5851
Host::Unix(_) => "",
5952
};
60-
let future = state.make_tls_mode.make_tls_mode(hostname);
53+
let tls_mode = state
54+
.make_tls_mode
55+
.make_tls_mode(hostname)
56+
.map_err(|e| Error::tls(e.into()))?;
6157

62-
transition!(MakingTlsMode {
63-
future,
58+
transition!(Connecting {
59+
future: ConnectOnceFuture::new(0, tls_mode, config.clone()),
6460
idx: 0,
6561
make_tls_mode: state.make_tls_mode,
6662
config,
6763
})
6864
}
6965

70-
fn poll_making_tls_mode<'a>(
71-
state: &'a mut RentToOwn<'a, MakingTlsMode<T>>,
72-
) -> Poll<AfterMakingTlsMode<T>, Error> {
73-
let tls_mode = try_ready!(state.future.poll().map_err(|e| Error::tls(e.into())));
74-
let state = state.take();
75-
76-
transition!(Connecting {
77-
future: ConnectOnceFuture::new(state.idx, tls_mode, state.config.clone()),
78-
idx: state.idx,
79-
make_tls_mode: state.make_tls_mode,
80-
config: state.config,
81-
})
82-
}
83-
8466
fn poll_connecting<'a>(
8567
state: &'a mut RentToOwn<'a, Connecting<T>>,
8668
) -> Poll<AfterConnecting<T>, Error> {
87-
match state.future.poll() {
88-
Ok(Async::Ready(r)) => transition!(Finished(r)),
89-
Ok(Async::NotReady) => Ok(Async::NotReady),
90-
Err(e) => {
91-
let mut state = state.take();
92-
let idx = state.idx + 1;
69+
loop {
70+
match state.future.poll() {
71+
Ok(Async::Ready(r)) => transition!(Finished(r)),
72+
Ok(Async::NotReady) => return Ok(Async::NotReady),
73+
Err(e) => {
74+
let state = &mut **state;
75+
state.idx += 1;
9376

94-
let host = match state.config.0.host.get(idx) {
95-
Some(host) => host,
96-
None => return Err(e),
97-
};
77+
let host = match state.config.0.host.get(state.idx) {
78+
Some(host) => host,
79+
None => return Err(e),
80+
};
9881

99-
let hostname = match host {
100-
Host::Tcp(host) => &**host,
101-
#[cfg(unix)]
102-
Host::Unix(_) => "",
103-
};
104-
let future = state.make_tls_mode.make_tls_mode(hostname);
82+
let hostname = match host {
83+
Host::Tcp(host) => &**host,
84+
#[cfg(unix)]
85+
Host::Unix(_) => "",
86+
};
87+
let tls_mode = state
88+
.make_tls_mode
89+
.make_tls_mode(hostname)
90+
.map_err(|e| Error::tls(e.into()))?;
10591

106-
transition!(MakingTlsMode {
107-
future,
108-
idx,
109-
make_tls_mode: state.make_tls_mode,
110-
config: state.config,
111-
})
92+
state.future =
93+
ConnectOnceFuture::new(state.idx, tls_mode, state.config.clone());
94+
}
11295
}
11396
}
11497
}

tokio-postgres/src/tls.rs

+8-45
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ pub trait MakeTlsMode<S> {
3030
type Stream: AsyncRead + AsyncWrite;
3131
type TlsMode: TlsMode<S, Stream = Self::Stream>;
3232
type Error: Into<Box<dyn Error + Sync + Send>>;
33-
type Future: Future<Item = Self::TlsMode, Error = Self::Error>;
3433

35-
fn make_tls_mode(&mut self, domain: &str) -> Self::Future;
34+
fn make_tls_mode(&mut self, domain: &str) -> Result<Self::TlsMode, Self::Error>;
3635
}
3736

3837
pub trait TlsMode<S> {
@@ -50,9 +49,8 @@ pub trait MakeTlsConnect<S> {
5049
type Stream: AsyncRead + AsyncWrite;
5150
type TlsConnect: TlsConnect<S, Stream = Self::Stream>;
5251
type Error: Into<Box<dyn Error + Sync + Send>>;
53-
type Future: Future<Item = Self::TlsConnect, Error = Self::Error>;
5452

55-
fn make_tls_connect(&mut self, domain: &str) -> Self::Future;
53+
fn make_tls_connect(&mut self, domain: &str) -> Result<Self::TlsConnect, Self::Error>;
5654
}
5755

5856
pub trait TlsConnect<S> {
@@ -74,10 +72,9 @@ where
7472
type Stream = S;
7573
type TlsMode = NoTls;
7674
type Error = Void;
77-
type Future = FutureResult<NoTls, Void>;
7875

79-
fn make_tls_mode(&mut self, _: &str) -> FutureResult<NoTls, Void> {
80-
future::ok(NoTls)
76+
fn make_tls_mode(&mut self, _: &str) -> Result<NoTls, Void> {
77+
Ok(NoTls)
8178
}
8279
}
8380

@@ -112,26 +109,9 @@ where
112109
type Stream = MaybeTlsStream<T::Stream, S>;
113110
type TlsMode = PreferTls<T::TlsConnect>;
114111
type Error = T::Error;
115-
type Future = MakePreferTlsFuture<T::Future>;
116112

117-
fn make_tls_mode(&mut self, domain: &str) -> MakePreferTlsFuture<T::Future> {
118-
MakePreferTlsFuture(self.0.make_tls_connect(domain))
119-
}
120-
}
121-
122-
#[cfg(feature = "runtime")]
123-
pub struct MakePreferTlsFuture<F>(F);
124-
125-
#[cfg(feature = "runtime")]
126-
impl<F> Future for MakePreferTlsFuture<F>
127-
where
128-
F: Future,
129-
{
130-
type Item = PreferTls<F::Item>;
131-
type Error = F::Error;
132-
133-
fn poll(&mut self) -> Poll<PreferTls<F::Item>, F::Error> {
134-
self.0.poll().map(|f| f.map(PreferTls))
113+
fn make_tls_mode(&mut self, domain: &str) -> Result<PreferTls<T::TlsConnect>, T::Error> {
114+
self.0.make_tls_connect(domain).map(PreferTls)
135115
}
136116
}
137117

@@ -282,26 +262,9 @@ where
282262
type Stream = T::Stream;
283263
type TlsMode = RequireTls<T::TlsConnect>;
284264
type Error = T::Error;
285-
type Future = MakeRequireTlsFuture<T::Future>;
286-
287-
fn make_tls_mode(&mut self, domain: &str) -> MakeRequireTlsFuture<T::Future> {
288-
MakeRequireTlsFuture(self.0.make_tls_connect(domain))
289-
}
290-
}
291-
292-
#[cfg(feature = "runtime")]
293-
pub struct MakeRequireTlsFuture<F>(F);
294-
295-
#[cfg(feature = "runtime")]
296-
impl<F> Future for MakeRequireTlsFuture<F>
297-
where
298-
F: Future,
299-
{
300-
type Item = RequireTls<F::Item>;
301-
type Error = F::Error;
302265

303-
fn poll(&mut self) -> Poll<RequireTls<F::Item>, F::Error> {
304-
self.0.poll().map(|f| f.map(RequireTls))
266+
fn make_tls_mode(&mut self, domain: &str) -> Result<RequireTls<T::TlsConnect>, T::Error> {
267+
self.0.make_tls_connect(domain).map(RequireTls)
305268
}
306269
}
307270

0 commit comments

Comments
 (0)