Skip to content

Commit

Permalink
feat(client): expose hyper::client::connect::Connect trait alias
Browse files Browse the repository at this point in the history
This is *just* a "trait alias". It is automatically implemented for all
`Service<Uri>`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.
  • Loading branch information
seanmonstar committed Dec 12, 2019
1 parent a07142d commit 2553ea1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/client/connect/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C>(connector: C, dst: Uri) -> Result<C::Transport, C::Error>
async fn connect<C>(
connector: C,
dst: Uri,
) -> Result<<C::_Svc as ConnectSvc>::Connection, <C::_Svc as ConnectSvc>::Error>
where
C: Connect,
{
Expand Down
36 changes: 28 additions & 8 deletions src/client/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Box<dyn StdError + Send + Sync>>;
/// A Future that will resolve to the connected Transport.
type Future: Future<Output = Result<Self::Transport, Self::Error>>;
#[doc(hidden)]
type _Svc: ConnectSvc;
#[doc(hidden)]
fn connect(self, internal_only: Internal, dst: Uri) -> <Self::_Svc as ConnectSvc>::Future;
}

pub trait ConnectSvc {
type Connection: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static;
type Error: Into<Box<dyn StdError + Send + Sync>>;
type Future: Future<Output = Result<Self::Connection, Self::Error>> + Unpin + Send + 'static;

fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future;
}

impl<S, T> Connect for S
where
S: tower_service::Service<Uri, Response = T> + Send,
S: tower_service::Service<Uri, Response = T> + Send + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::Future: Unpin + Send,
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
{
type _Svc = S;

fn connect(self, _: Internal, dst: Uri) -> crate::service::Oneshot<S, Uri> {
crate::service::oneshot(self, dst)
}
}

impl<S, T> ConnectSvc for S
where
S: tower_service::Service<Uri, Response = T> + Send + 'static,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
S::Future: Unpin + Send,
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
{
type Transport = T;
type Connection = T;
type Error = S::Error;
type Future = crate::service::Oneshot<S, Uri>;

fn connect(self, _: Internal, dst: Uri) -> Self::Future {
crate::service::oneshot(self, dst)
}
Expand Down
4 changes: 0 additions & 4 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ impl Client<(), Body> {
impl<C, B> Client<C, B>
where
C: Connect + Clone + Send + Sync + 'static,
C::Transport: Unpin + Send + 'static,
C::Future: Unpin + Send + 'static,
B: Payload + Send + 'static,
B::Data: Send,
{
Expand Down Expand Up @@ -548,8 +546,6 @@ where
impl<C, B> tower_service::Service<Request<B>> for Client<C, B>
where
C: Connect + Clone + Send + Sync + 'static,
C::Transport: Unpin + Send + 'static,
C::Future: Unpin + Send + 'static,
B: Payload + Send + 'static,
B::Data: Send,
{
Expand Down

0 comments on commit 2553ea1

Please sign in to comment.