From 2553ea1a7ae3d11f0232a5818949146fa3f68a29 Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Wed, 11 Dec 2019 15:50:36 -0800 Subject: [PATCH] feat(client): expose `hyper::client::connect::Connect` trait alias This is *just* a "trait alias". It is automatically implemented for all `Service`s that have the required bounds. It's purpose being public is to ease setting trait bounds outside of hyper. Therefore, it doesn't have any exposed associated types, to prevent otherwise relying on any super-traits that hyper requires. --- src/client/connect/http.rs | 7 +++++-- src/client/connect/mod.rs | 36 ++++++++++++++++++++++++++++-------- src/client/mod.rs | 4 ---- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/client/connect/http.rs b/src/client/connect/http.rs index 08a08767..e4bdd7ca 100644 --- a/src/client/connect/http.rs +++ b/src/client/connect/http.rs @@ -637,10 +637,13 @@ mod tests { use ::http::Uri; - use super::super::sealed::Connect; + use super::super::sealed::{Connect, ConnectSvc}; use super::HttpConnector; - async fn connect(connector: C, dst: Uri) -> Result + async fn connect( + connector: C, + dst: Uri, + ) -> Result<::Connection, ::Error> where C: Connect, { diff --git a/src/client/connect/mod.rs b/src/client/connect/mod.rs index 5cb051ab..01e2dcf9 100644 --- a/src/client/connect/mod.rs +++ b/src/client/connect/mod.rs @@ -83,6 +83,7 @@ pub mod dns; mod http; #[cfg(feature = "tcp")] pub use self::http::{HttpConnector, HttpInfo}; +pub use self::sealed::Connect; /// Describes a type returned by a connector. pub trait Connection { @@ -258,26 +259,45 @@ pub(super) mod sealed { // The `Sized` bound is to prevent creating `dyn Connect`, since they cannot // fit the `Connect` bounds because of the blanket impl for `Service`. pub trait Connect: Sealed + Sized { - /// The connected IO Stream. - type Transport: AsyncRead + AsyncWrite + Connection; - /// An error occured when trying to connect. - type Error: Into>; - /// A Future that will resolve to the connected Transport. - type Future: Future>; #[doc(hidden)] + type _Svc: ConnectSvc; + #[doc(hidden)] + fn connect(self, internal_only: Internal, dst: Uri) -> ::Future; + } + + pub trait ConnectSvc { + type Connection: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static; + type Error: Into>; + type Future: Future> + Unpin + Send + 'static; + fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future; } impl Connect for S where - S: tower_service::Service + Send, + S: tower_service::Service + Send + 'static, S::Error: Into>, S::Future: Unpin + Send, T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static, { - type Transport = T; + type _Svc = S; + + fn connect(self, _: Internal, dst: Uri) -> crate::service::Oneshot { + crate::service::oneshot(self, dst) + } + } + + impl ConnectSvc for S + where + S: tower_service::Service + Send + 'static, + S::Error: Into>, + S::Future: Unpin + Send, + T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static, + { + type Connection = T; type Error = S::Error; type Future = crate::service::Oneshot; + fn connect(self, _: Internal, dst: Uri) -> Self::Future { crate::service::oneshot(self, dst) } diff --git a/src/client/mod.rs b/src/client/mod.rs index 851e17e6..5f652fb0 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -150,8 +150,6 @@ impl Client<(), Body> { impl Client where C: Connect + Clone + Send + Sync + 'static, - C::Transport: Unpin + Send + 'static, - C::Future: Unpin + Send + 'static, B: Payload + Send + 'static, B::Data: Send, { @@ -548,8 +546,6 @@ where impl tower_service::Service> for Client where C: Connect + Clone + Send + Sync + 'static, - C::Transport: Unpin + Send + 'static, - C::Future: Unpin + Send + 'static, B: Payload + Send + 'static, B::Data: Send, {