Add MakeConnection trait alias (#151)

This commit is contained in:
Lucio Franco 2019-01-28 16:38:13 -05:00 committed by Carl Lerche
parent 1b8dc7149a
commit 61a183fe88
3 changed files with 46 additions and 0 deletions

View File

@ -8,3 +8,4 @@ publish = false
futures = "0.1"
tower-service = { version = "0.2", path = "../tower-service" }
tower-direct-service = { version = "0.1", path = "../tower-direct-service" }
tokio-io = "0.1"

View File

@ -2,12 +2,14 @@
#[macro_use]
extern crate futures;
extern crate tokio_io;
extern crate tower_direct_service;
extern crate tower_service;
pub mod boxed;
pub mod either;
pub mod ext;
mod make_connection;
mod make_service;
pub mod option;
mod service_fn;
@ -15,6 +17,7 @@ mod service_fn;
pub use boxed::BoxService;
pub use either::EitherService;
pub use ext::ServiceExt;
pub use make_connection::MakeConnection;
pub use make_service::MakeService;
pub use option::OptionService;
pub use service_fn::ServiceFn;

View File

@ -0,0 +1,42 @@
use futures::Future;
use tokio_io::{AsyncRead, AsyncWrite};
use tower_service::Service;
/// The MakeConnection trait is used to create transports
///
/// The goal of this service is to allow composable methods for creating
/// `AsyncRead + AsyncWrite` transports. This could mean creating a TLS
/// based connection or using some other method to authenticate the connection.
pub trait MakeConnection<Request> {
/// The transport provided by this service
type Response: AsyncRead + AsyncWrite;
/// Errors produced by the connecting service
type Error;
/// The future that eventually produces the transport
type Future: Future<Item = Self::Response, Error = Self::Error>;
/// Connect and return a transport asynchronously
fn make_connection(&mut self, target: Request) -> Self::Future;
}
impl<S, Request> self::sealed::Sealed<Request> for S where S: Service<Request> {}
impl<C, Request> MakeConnection<Request> for C
where
C: Service<Request>,
C::Response: AsyncRead + AsyncWrite,
{
type Response = C::Response;
type Error = C::Error;
type Future = C::Future;
fn make_connection(&mut self, target: Request) -> Self::Future {
Service::call(self, target)
}
}
mod sealed {
pub trait Sealed<A> {}
}