Remove future from MakeTlsMode

It's unlikely to be useful in practice, and just introduces more
complexity.
This commit is contained in:
Steven Fackler 2019-01-05 22:03:13 -08:00
parent 0ae7670e05
commit 940cbb8d4b
5 changed files with 44 additions and 107 deletions
postgres/src
tokio-postgres-openssl/src
tokio-postgres/src

View File

@ -18,7 +18,6 @@ impl Client {
T: MakeTlsMode<Socket> + 'static + Send, T: MakeTlsMode<Socket> + 'static + Send,
T::TlsMode: Send, T::TlsMode: Send,
T::Stream: Send, T::Stream: Send,
T::Future: Send,
<T::TlsMode as TlsMode<Socket>>::Future: Send, <T::TlsMode as TlsMode<Socket>>::Future: Send,
{ {
params.parse::<Config>()?.connect(tls_mode) params.parse::<Config>()?.connect(tls_mode)

View File

@ -97,7 +97,6 @@ impl Config {
T: MakeTlsMode<Socket> + 'static + Send, T: MakeTlsMode<Socket> + 'static + Send,
T::TlsMode: Send, T::TlsMode: Send,
T::Stream: Send, T::Stream: Send,
T::Future: Send,
<T::TlsMode as TlsMode<Socket>>::Future: Send, <T::TlsMode as TlsMode<Socket>>::Future: Send,
{ {
let connect = self.0.connect(tls_mode); let connect = self.0.connect(tls_mode);

View File

@ -1,7 +1,5 @@
#![warn(rust_2018_idioms, clippy::all)] #![warn(rust_2018_idioms, clippy::all)]
#[cfg(feature = "runtime")]
use futures::future::{self, FutureResult};
use futures::{try_ready, Async, Future, Poll}; use futures::{try_ready, Async, Future, Poll};
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
use openssl::error::ErrorStack; use openssl::error::ErrorStack;
@ -44,12 +42,6 @@ impl MakeTlsConnector {
{ {
self.config = Arc::new(f); self.config = Arc::new(f);
} }
fn make_tls_connect_inner(&mut self, domain: &str) -> Result<TlsConnector, ErrorStack> {
let mut ssl = self.connector.configure()?;
(self.config)(&mut ssl)?;
Ok(TlsConnector::new(ssl, domain))
}
} }
#[cfg(feature = "runtime")] #[cfg(feature = "runtime")]
@ -60,10 +52,11 @@ where
type Stream = SslStream<S>; type Stream = SslStream<S>;
type TlsConnect = TlsConnector; type TlsConnect = TlsConnector;
type Error = ErrorStack; type Error = ErrorStack;
type Future = FutureResult<TlsConnector, ErrorStack>;
fn make_tls_connect(&mut self, domain: &str) -> FutureResult<TlsConnector, ErrorStack> { fn make_tls_connect(&mut self, domain: &str) -> Result<TlsConnector, ErrorStack> {
future::result(self.make_tls_connect_inner(domain)) let mut ssl = self.connector.configure()?;
(self.config)(&mut ssl)?;
Ok(TlsConnector::new(ssl, domain))
} }
} }

View File

@ -1,4 +1,4 @@
use futures::{try_ready, Async, Future, Poll}; use futures::{Async, Future, Poll};
use state_machine_future::{transition, RentToOwn, StateMachineFuture}; use state_machine_future::{transition, RentToOwn, StateMachineFuture};
use crate::proto::{Client, ConnectOnceFuture, Connection}; use crate::proto::{Client, ConnectOnceFuture, Connection};
@ -9,19 +9,12 @@ pub enum Connect<T>
where where
T: MakeTlsMode<Socket>, T: MakeTlsMode<Socket>,
{ {
#[state_machine_future(start, transitions(MakingTlsMode))] #[state_machine_future(start, transitions(Connecting))]
Start { Start {
make_tls_mode: T, make_tls_mode: T,
config: Result<Config, Error>, config: Result<Config, Error>,
}, },
#[state_machine_future(transitions(Connecting))] #[state_machine_future(transitions(Finished))]
MakingTlsMode {
future: T::Future,
idx: usize,
make_tls_mode: T,
config: Config,
},
#[state_machine_future(transitions(MakingTlsMode, Finished))]
Connecting { Connecting {
future: ConnectOnceFuture<T::TlsMode>, future: ConnectOnceFuture<T::TlsMode>,
idx: usize, idx: usize,
@ -57,58 +50,48 @@ where
#[cfg(unix)] #[cfg(unix)]
Host::Unix(_) => "", Host::Unix(_) => "",
}; };
let future = state.make_tls_mode.make_tls_mode(hostname); let tls_mode = state
.make_tls_mode
.make_tls_mode(hostname)
.map_err(|e| Error::tls(e.into()))?;
transition!(MakingTlsMode { transition!(Connecting {
future, future: ConnectOnceFuture::new(0, tls_mode, config.clone()),
idx: 0, idx: 0,
make_tls_mode: state.make_tls_mode, make_tls_mode: state.make_tls_mode,
config, config,
}) })
} }
fn poll_making_tls_mode<'a>(
state: &'a mut RentToOwn<'a, MakingTlsMode<T>>,
) -> Poll<AfterMakingTlsMode<T>, Error> {
let tls_mode = try_ready!(state.future.poll().map_err(|e| Error::tls(e.into())));
let state = state.take();
transition!(Connecting {
future: ConnectOnceFuture::new(state.idx, tls_mode, state.config.clone()),
idx: state.idx,
make_tls_mode: state.make_tls_mode,
config: state.config,
})
}
fn poll_connecting<'a>( fn poll_connecting<'a>(
state: &'a mut RentToOwn<'a, Connecting<T>>, state: &'a mut RentToOwn<'a, Connecting<T>>,
) -> Poll<AfterConnecting<T>, Error> { ) -> Poll<AfterConnecting<T>, Error> {
match state.future.poll() { loop {
Ok(Async::Ready(r)) => transition!(Finished(r)), match state.future.poll() {
Ok(Async::NotReady) => Ok(Async::NotReady), Ok(Async::Ready(r)) => transition!(Finished(r)),
Err(e) => { Ok(Async::NotReady) => return Ok(Async::NotReady),
let mut state = state.take(); Err(e) => {
let idx = state.idx + 1; let state = &mut **state;
state.idx += 1;
let host = match state.config.0.host.get(idx) { let host = match state.config.0.host.get(state.idx) {
Some(host) => host, Some(host) => host,
None => return Err(e), None => return Err(e),
}; };
let hostname = match host { let hostname = match host {
Host::Tcp(host) => &**host, Host::Tcp(host) => &**host,
#[cfg(unix)] #[cfg(unix)]
Host::Unix(_) => "", Host::Unix(_) => "",
}; };
let future = state.make_tls_mode.make_tls_mode(hostname); let tls_mode = state
.make_tls_mode
.make_tls_mode(hostname)
.map_err(|e| Error::tls(e.into()))?;
transition!(MakingTlsMode { state.future =
future, ConnectOnceFuture::new(state.idx, tls_mode, state.config.clone());
idx, }
make_tls_mode: state.make_tls_mode,
config: state.config,
})
} }
} }
} }

View File

@ -30,9 +30,8 @@ pub trait MakeTlsMode<S> {
type Stream: AsyncRead + AsyncWrite; type Stream: AsyncRead + AsyncWrite;
type TlsMode: TlsMode<S, Stream = Self::Stream>; type TlsMode: TlsMode<S, Stream = Self::Stream>;
type Error: Into<Box<dyn Error + Sync + Send>>; type Error: Into<Box<dyn Error + Sync + Send>>;
type Future: Future<Item = Self::TlsMode, Error = Self::Error>;
fn make_tls_mode(&mut self, domain: &str) -> Self::Future; fn make_tls_mode(&mut self, domain: &str) -> Result<Self::TlsMode, Self::Error>;
} }
pub trait TlsMode<S> { pub trait TlsMode<S> {
@ -50,9 +49,8 @@ pub trait MakeTlsConnect<S> {
type Stream: AsyncRead + AsyncWrite; type Stream: AsyncRead + AsyncWrite;
type TlsConnect: TlsConnect<S, Stream = Self::Stream>; type TlsConnect: TlsConnect<S, Stream = Self::Stream>;
type Error: Into<Box<dyn Error + Sync + Send>>; type Error: Into<Box<dyn Error + Sync + Send>>;
type Future: Future<Item = Self::TlsConnect, Error = Self::Error>;
fn make_tls_connect(&mut self, domain: &str) -> Self::Future; fn make_tls_connect(&mut self, domain: &str) -> Result<Self::TlsConnect, Self::Error>;
} }
pub trait TlsConnect<S> { pub trait TlsConnect<S> {
@ -74,10 +72,9 @@ where
type Stream = S; type Stream = S;
type TlsMode = NoTls; type TlsMode = NoTls;
type Error = Void; type Error = Void;
type Future = FutureResult<NoTls, Void>;
fn make_tls_mode(&mut self, _: &str) -> FutureResult<NoTls, Void> { fn make_tls_mode(&mut self, _: &str) -> Result<NoTls, Void> {
future::ok(NoTls) Ok(NoTls)
} }
} }
@ -112,26 +109,9 @@ where
type Stream = MaybeTlsStream<T::Stream, S>; type Stream = MaybeTlsStream<T::Stream, S>;
type TlsMode = PreferTls<T::TlsConnect>; type TlsMode = PreferTls<T::TlsConnect>;
type Error = T::Error; type Error = T::Error;
type Future = MakePreferTlsFuture<T::Future>;
fn make_tls_mode(&mut self, domain: &str) -> MakePreferTlsFuture<T::Future> { fn make_tls_mode(&mut self, domain: &str) -> Result<PreferTls<T::TlsConnect>, T::Error> {
MakePreferTlsFuture(self.0.make_tls_connect(domain)) self.0.make_tls_connect(domain).map(PreferTls)
}
}
#[cfg(feature = "runtime")]
pub struct MakePreferTlsFuture<F>(F);
#[cfg(feature = "runtime")]
impl<F> Future for MakePreferTlsFuture<F>
where
F: Future,
{
type Item = PreferTls<F::Item>;
type Error = F::Error;
fn poll(&mut self) -> Poll<PreferTls<F::Item>, F::Error> {
self.0.poll().map(|f| f.map(PreferTls))
} }
} }
@ -282,26 +262,9 @@ where
type Stream = T::Stream; type Stream = T::Stream;
type TlsMode = RequireTls<T::TlsConnect>; type TlsMode = RequireTls<T::TlsConnect>;
type Error = T::Error; type Error = T::Error;
type Future = MakeRequireTlsFuture<T::Future>;
fn make_tls_mode(&mut self, domain: &str) -> MakeRequireTlsFuture<T::Future> { fn make_tls_mode(&mut self, domain: &str) -> Result<RequireTls<T::TlsConnect>, T::Error> {
MakeRequireTlsFuture(self.0.make_tls_connect(domain)) self.0.make_tls_connect(domain).map(RequireTls)
}
}
#[cfg(feature = "runtime")]
pub struct MakeRequireTlsFuture<F>(F);
#[cfg(feature = "runtime")]
impl<F> Future for MakeRequireTlsFuture<F>
where
F: Future,
{
type Item = RequireTls<F::Item>;
type Error = F::Error;
fn poll(&mut self) -> Poll<RequireTls<F::Item>, F::Error> {
self.0.poll().map(|f| f.map(RequireTls))
} }
} }