From 0390ff37d362c4b2067fc2513189affb81628a09 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 4 Sep 2018 09:49:21 -0700 Subject: [PATCH] Revert "use From/Into instead of custom IntoService and IntoNewService traits" This reverts commit 9441624827e18c0f5a90b23a6582c4c1e1a7b29a. --- src/lib.rs | 2 +- src/service/and_then.rs | 9 ++++-- src/service/apply.rs | 7 ++--- src/service/fn_service.rs | 9 ++++-- src/service/fn_state_service.rs | 12 +++++--- src/service/mod.rs | 52 ++++++++++++++++++++++++++++----- src/stream.rs | 6 ++-- 7 files changed, 72 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7369015e1..bae43b6ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,7 +61,7 @@ mod worker; pub use configurable::{IntoNewConfigurableService, NewConfigurableService}; pub use server::Server; -pub use service::{NewServiceExt, ServiceExt}; +pub use service::{IntoNewService, IntoService, NewServiceExt, ServiceExt}; /// Pause accepting incoming connections /// diff --git a/src/service/and_then.rs b/src/service/and_then.rs index 0d4f22373..2d248667f 100644 --- a/src/service/and_then.rs +++ b/src/service/and_then.rs @@ -4,6 +4,8 @@ use std::rc::Rc; use futures::{Async, Future, Poll}; use tower_service::{NewService, Service}; +use super::IntoNewService; + /// `AndThen` service combinator pub struct AndThen { a: A, @@ -112,8 +114,11 @@ where B: NewService, { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self { - Self { a, b: f.into() } + pub fn new>(a: A, f: F) -> Self { + Self { + a, + b: f.into_new_service(), + } } } diff --git a/src/service/apply.rs b/src/service/apply.rs index b688a8c12..58755f6fa 100644 --- a/src/service/apply.rs +++ b/src/service/apply.rs @@ -1,8 +1,7 @@ use std::marker::PhantomData; use futures::{Async, Future, IntoFuture, Poll}; - -use {NewService, Service}; +use {IntoNewService, NewService, Service}; /// `Apply` service combinator pub struct Apply { @@ -63,10 +62,10 @@ where R: IntoFuture, { /// Create new `ApplyNewService` new service instance - pub fn new>(f: F, service: F1) -> Self { + pub fn new>(f: F, service: F1) -> Self { Self { f, - service: service.into(), + service: service.into_new_service(), r: PhantomData, } } diff --git a/src/service/fn_service.rs b/src/service/fn_service.rs index e08770d70..6eee6c9be 100644 --- a/src/service/fn_service.rs +++ b/src/service/fn_service.rs @@ -6,6 +6,8 @@ use futures::{ }; use tower_service::{NewService, Service}; +use super::IntoNewService; + pub struct FnService where F: Fn(Req) -> Fut, @@ -111,13 +113,14 @@ where } } -impl From for FnNewService +impl IntoNewService> + for F where F: Fn(Req) -> Fut + Clone + 'static, Fut: IntoFuture, { - fn from(f: F) -> FnNewService { - FnNewService::new(f) + fn into_new_service(self) -> FnNewService { + FnNewService::new(self) } } diff --git a/src/service/fn_state_service.rs b/src/service/fn_state_service.rs index f423c311d..4a76ee363 100644 --- a/src/service/fn_state_service.rs +++ b/src/service/fn_state_service.rs @@ -3,6 +3,8 @@ use std::marker; use futures::{Async, Future, IntoFuture, Poll}; use tower_service::{NewService, Service}; +use super::IntoNewService; + pub struct FnStateService where F: Fn(&mut S, Req) -> Fut, @@ -111,8 +113,8 @@ where } } -impl From<(F1, F2)> - for FnStateNewService +impl + IntoNewService> for (F1, F2) where S: 'static, F1: Fn(&mut S, Req) -> Fut1 + Clone + 'static, @@ -124,8 +126,10 @@ where Err1: 'static, Err2: 'static, { - fn from(data: (F1, F2)) -> FnStateNewService { - FnStateNewService::new(data.0, data.1) + fn into_new_service( + self, + ) -> FnStateNewService { + FnStateNewService::new(self.0, self.1) } } diff --git a/src/service/mod.rs b/src/service/mod.rs index 1b5811545..e6580f4da 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -31,10 +31,10 @@ pub trait ServiceExt: Service { fn and_then(self, service: F) -> AndThen where Self: Sized, - F: Into, + F: IntoService, B: Service, { - AndThen::new(self, service.into()) + AndThen::new(self, service.into_service()) } fn map(self, f: F) -> Map @@ -68,7 +68,7 @@ pub trait NewServiceExt: NewService { fn and_then(self, new_service: F) -> AndThenNewService where Self: Sized, - F: Into, + F: IntoNewService, B: NewService< Request = Self::Response, Error = Self::Error, @@ -103,15 +103,51 @@ pub trait NewServiceExt: NewService { } } -impl ServiceExt for T where T: Service {} -impl NewServiceExt for T where T: NewService {} +impl ServiceExt for T {} +impl NewServiceExt for T {} -impl From for FnService +/// Trait for types that can be converted to a Service +pub trait IntoService +where + T: Service, +{ + /// Create service + fn into_service(self) -> T; +} + +/// Trait for types that can be converted to a Service +pub trait IntoNewService +where + T: NewService, +{ + /// Create service + fn into_new_service(self) -> T; +} + +impl IntoService for T +where + T: Service, +{ + fn into_service(self) -> T { + self + } +} + +impl IntoNewService for T +where + T: NewService, +{ + fn into_new_service(self) -> T { + self + } +} + +impl IntoService> for F where F: Fn(Req) -> Fut + 'static, Fut: IntoFuture, { - fn from(f: F) -> FnService { - FnService::new(f) + fn into_service(self) -> FnService { + FnService::new(self) } } diff --git a/src/stream.rs b/src/stream.rs index 104d444cf..f0ef120ec 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -2,7 +2,7 @@ use futures::unsync::mpsc; use futures::{Async, Future, Poll, Stream}; use tokio::executor::current_thread::spawn; -use super::Service; +use super::{IntoService, Service}; pub struct StreamDispatcher { stream: S, @@ -18,12 +18,12 @@ where T: Service, Response = (), Error = ()>, T::Future: 'static, { - pub fn new>(stream: S, service: F) -> Self { + pub fn new>(stream: S, service: F) -> Self { let (stop_tx, stop_rx) = mpsc::unbounded(); StreamDispatcher { stream, item: None, - service: service.into(), + service: service.into_service(), stop_rx, stop_tx, }