diff --git a/tower-util/Cargo.toml b/tower-util/Cargo.toml index cd8ca70e..d494a5e6 100644 --- a/tower-util/Cargo.toml +++ b/tower-util/Cargo.toml @@ -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" \ No newline at end of file diff --git a/tower-util/src/lib.rs b/tower-util/src/lib.rs index 2f055c71..320234f0 100644 --- a/tower-util/src/lib.rs +++ b/tower-util/src/lib.rs @@ -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; diff --git a/tower-util/src/make_connection.rs b/tower-util/src/make_connection.rs new file mode 100644 index 00000000..6cdcdc01 --- /dev/null +++ b/tower-util/src/make_connection.rs @@ -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 { + /// 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; + + /// Connect and return a transport asynchronously + fn make_connection(&mut self, target: Request) -> Self::Future; +} + +impl self::sealed::Sealed for S where S: Service {} + +impl MakeConnection for C +where + C: Service, + 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 {} +}