From 3384bbfae25be7379435acd05be90fca158722c7 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 17 Sep 2018 15:53:41 -0700 Subject: [PATCH] change apply combinator --- src/service/apply.rs | 6 +++--- src/service/mod.rs | 25 +++++++++++++++---------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/service/apply.rs b/src/service/apply.rs index 11fffce0..7d1bbabc 100644 --- a/src/service/apply.rs +++ b/src/service/apply.rs @@ -19,7 +19,7 @@ where R: IntoFuture, { /// Create new `Apply` combinator - pub fn new(f: F, service: T) -> Self { + pub fn new(service: T, f: F) -> Self { Self { service, f, @@ -79,7 +79,7 @@ where R: IntoFuture, { /// Create new `ApplyNewService` new service instance - pub fn new>(f: F, service: F1) -> Self { + pub fn new>(service: F1, f: F) -> Self { Self { f, service: service.into_new_service(), @@ -161,7 +161,7 @@ where fn poll(&mut self) -> Poll { if let Async::Ready(service) = self.fut.poll()? { - Ok(Async::Ready(Apply::new(self.f.take().unwrap(), service))) + Ok(Async::Ready(Apply::new(service, self.f.take().unwrap()))) } else { Ok(Async::NotReady) } diff --git a/src/service/mod.rs b/src/service/mod.rs index 2a3c7f69..e5fc63a5 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -24,14 +24,16 @@ pub use self::map_init_err::MapInitErr; /// An extension trait for `Service`s that provides a variety of convenient /// adapters pub trait ServiceExt: Service { - fn apply(self, f: F) -> Apply + /// Apply function to specified service and use it as a next service in chain. + fn apply(self, service: S, f: F) -> AndThen> where Self: Sized, - Self::Error: Into<::Error>, - F: Fn(Req, &mut Self) -> R, - R: IntoFuture, + S: Service, + S::Error: Into<::Error>, + F: Fn(Self::Response, &mut S) -> R, + R: IntoFuture, { - Apply::new(f, self) + self.and_then(Apply::new(service, f)) } /// Call another service after call to this one has resolved successfully. @@ -100,14 +102,17 @@ pub trait ServiceExt: Service { } pub trait NewServiceExt: NewService { - fn apply(self, f: F) -> ApplyNewService + fn apply( + self, service: S, f: F, + ) -> AndThenNewService> where Self: Sized, - Self::Error: Into<::Error>, - F: Fn(Req, &mut Self::Service) -> R + Clone, - R: IntoFuture, + S: NewService, + S::Error: Into<::Error>, + F: Fn(Self::Response, &mut S::Service) -> R + Clone, + R: IntoFuture, { - ApplyNewService::new(f, self) + self.and_then(ApplyNewService::new(service, f)) } fn and_then(self, new_service: F) -> AndThenNewService