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 11, 2019
1 parent a07142d commit 4124c8d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
38 changes: 30 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,31 +259,52 @@ 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)
}
}



impl<S, T> Sealed for S
where
S: tower_service::Service<Uri, Response = T> + Send,
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 4124c8d

Please sign in to comment.