Skip to content

Commit 2553ea1

Browse files
committed
feat(client): expose hyper::client::connect::Connect trait alias
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.
1 parent a07142d commit 2553ea1

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/client/connect/http.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -637,10 +637,13 @@ mod tests {
637637

638638
use ::http::Uri;
639639

640-
use super::super::sealed::Connect;
640+
use super::super::sealed::{Connect, ConnectSvc};
641641
use super::HttpConnector;
642642

643-
async fn connect<C>(connector: C, dst: Uri) -> Result<C::Transport, C::Error>
643+
async fn connect<C>(
644+
connector: C,
645+
dst: Uri,
646+
) -> Result<<C::_Svc as ConnectSvc>::Connection, <C::_Svc as ConnectSvc>::Error>
644647
where
645648
C: Connect,
646649
{

src/client/connect/mod.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub mod dns;
8383
mod http;
8484
#[cfg(feature = "tcp")]
8585
pub use self::http::{HttpConnector, HttpInfo};
86+
pub use self::sealed::Connect;
8687

8788
/// Describes a type returned by a connector.
8889
pub trait Connection {
@@ -258,26 +259,45 @@ pub(super) mod sealed {
258259
// The `Sized` bound is to prevent creating `dyn Connect`, since they cannot
259260
// fit the `Connect` bounds because of the blanket impl for `Service`.
260261
pub trait Connect: Sealed + Sized {
261-
/// The connected IO Stream.
262-
type Transport: AsyncRead + AsyncWrite + Connection;
263-
/// An error occured when trying to connect.
264-
type Error: Into<Box<dyn StdError + Send + Sync>>;
265-
/// A Future that will resolve to the connected Transport.
266-
type Future: Future<Output = Result<Self::Transport, Self::Error>>;
267262
#[doc(hidden)]
263+
type _Svc: ConnectSvc;
264+
#[doc(hidden)]
265+
fn connect(self, internal_only: Internal, dst: Uri) -> <Self::_Svc as ConnectSvc>::Future;
266+
}
267+
268+
pub trait ConnectSvc {
269+
type Connection: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static;
270+
type Error: Into<Box<dyn StdError + Send + Sync>>;
271+
type Future: Future<Output = Result<Self::Connection, Self::Error>> + Unpin + Send + 'static;
272+
268273
fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future;
269274
}
270275

271276
impl<S, T> Connect for S
272277
where
273-
S: tower_service::Service<Uri, Response = T> + Send,
278+
S: tower_service::Service<Uri, Response = T> + Send + 'static,
279+
S::Error: Into<Box<dyn StdError + Send + Sync>>,
280+
S::Future: Unpin + Send,
281+
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
282+
{
283+
type _Svc = S;
284+
285+
fn connect(self, _: Internal, dst: Uri) -> crate::service::Oneshot<S, Uri> {
286+
crate::service::oneshot(self, dst)
287+
}
288+
}
289+
290+
impl<S, T> ConnectSvc for S
291+
where
292+
S: tower_service::Service<Uri, Response = T> + Send + 'static,
274293
S::Error: Into<Box<dyn StdError + Send + Sync>>,
275294
S::Future: Unpin + Send,
276295
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
277296
{
278-
type Transport = T;
297+
type Connection = T;
279298
type Error = S::Error;
280299
type Future = crate::service::Oneshot<S, Uri>;
300+
281301
fn connect(self, _: Internal, dst: Uri) -> Self::Future {
282302
crate::service::oneshot(self, dst)
283303
}

src/client/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ impl Client<(), Body> {
150150
impl<C, B> Client<C, B>
151151
where
152152
C: Connect + Clone + Send + Sync + 'static,
153-
C::Transport: Unpin + Send + 'static,
154-
C::Future: Unpin + Send + 'static,
155153
B: Payload + Send + 'static,
156154
B::Data: Send,
157155
{
@@ -548,8 +546,6 @@ where
548546
impl<C, B> tower_service::Service<Request<B>> for Client<C, B>
549547
where
550548
C: Connect + Clone + Send + Sync + 'static,
551-
C::Transport: Unpin + Send + 'static,
552-
C::Future: Unpin + Send + 'static,
553549
B: Payload + Send + 'static,
554550
B::Data: Send,
555551
{

0 commit comments

Comments
 (0)