From 3dbaef3ec1437c5ffa4f13b241841251c587f0f1 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 25 Aug 2018 14:46:05 -0700 Subject: [PATCH] move config to generic type --- src/configurable.rs | 165 ++++++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 76 deletions(-) diff --git a/src/configurable.rs b/src/configurable.rs index 8bce29bf..df6dec1b 100644 --- a/src/configurable.rs +++ b/src/configurable.rs @@ -12,7 +12,7 @@ use service::{AndThen, FnService, MapErr}; /// accepts new TCP streams, obtains a new `Service` value using the /// `NewConfigurableService` trait, and uses that new `Service` value to /// process inbound requests on that new TCP stream. -pub trait NewConfigurableService { +pub trait NewConfigurableService { /// Requests handled by the service type Request; @@ -29,9 +29,6 @@ pub trait NewConfigurableService { Error = Self::Error, >; - /// Pipeline configuration - type Config; - /// Errors produced while building a service. type InitError; @@ -39,23 +36,23 @@ pub trait NewConfigurableService { type Future: Future; /// Create and return a new service value asynchronously. - fn new_service(&self, Self::Config) -> Self::Future; + fn new_service(&self, C) -> Self::Future; - fn and_then(self, new_service: F) -> AndThenNewConfigurableService + fn and_then(self, new_service: F) -> AndThenNewConfigurableService where Self: Sized, - F: IntoNewConfigurableService, + F: IntoNewConfigurableService, B: NewConfigurableService< + C, Request = Self::Response, Error = Self::Error, - Config = Self::Config, InitError = Self::InitError, >, { AndThenNewConfigurableService::new(self, new_service) } - fn map_err(self, f: F) -> MapErrNewConfigurableService + fn map_err(self, f: F) -> MapErrNewConfigurableService where Self: Sized, F: Fn(Self::Error) -> E, @@ -63,7 +60,7 @@ pub trait NewConfigurableService { MapErrNewConfigurableService::new(self, f) } - fn map_init_err(self, f: F) -> MapInitErr + fn map_init_err(self, f: F) -> MapInitErr where Self: Sized, F: Fn(Self::InitError) -> E, @@ -73,17 +70,17 @@ pub trait NewConfigurableService { } /// Trait for types that can be converted to a Service -pub trait IntoNewConfigurableService +pub trait IntoNewConfigurableService where - T: NewConfigurableService, + T: NewConfigurableService, { /// Create service fn into_new_service(self) -> T; } -impl IntoNewConfigurableService for T +impl IntoNewConfigurableService for T where - T: NewConfigurableService, + T: NewConfigurableService, { fn into_new_service(self) -> T { self @@ -121,7 +118,7 @@ where } impl - IntoNewConfigurableService> for F + IntoNewConfigurableService, Cfg> for F where S: Service, F: Fn(Cfg) -> Fut + 'static, @@ -143,7 +140,7 @@ where } } -impl NewConfigurableService +impl NewConfigurableService for Fn2NewConfigurableService where S: Service, @@ -154,7 +151,6 @@ where type Response = S::Response; type Error = S::Error; type Service = S; - type Config = Cfg; type InitError = Err; type Future = Fut::Future; @@ -194,7 +190,7 @@ where } } -impl NewConfigurableService +impl NewConfigurableService for FnNewConfigurableService where F: Fn(Req) -> Fut + Clone, @@ -204,7 +200,6 @@ where type Response = Resp; type Error = Err; type Service = FnService; - type Config = Cfg; type InitError = IErr; type Future = FutureResult; @@ -214,7 +209,7 @@ where } impl - IntoNewConfigurableService> + IntoNewConfigurableService, Cfg> for F where F: Fn(Req) -> Fut + Clone + 'static, @@ -237,46 +232,51 @@ where } /// `AndThenNewConfigurableService` new service combinator -pub struct AndThenNewConfigurableService { +pub struct AndThenNewConfigurableService +where + A: NewConfigurableService, + B: NewConfigurableService +{ a: A, b: B, + c: marker::PhantomData, } -impl AndThenNewConfigurableService +impl AndThenNewConfigurableService where - A: NewConfigurableService, - B: NewConfigurableService, + A: NewConfigurableService, + B: NewConfigurableService, { /// Create new `AndThen` combinator - pub fn new>(a: A, f: F) -> Self { + pub fn new>(a: A, f: F) -> Self { Self { a, b: f.into_new_service(), + c: marker::PhantomData, } } } -impl NewConfigurableService for AndThenNewConfigurableService +impl NewConfigurableService for AndThenNewConfigurableService where A: NewConfigurableService< + C, Response = B::Request, - Config = B::Config, InitError = B::InitError, >, - A::Config: Clone, A::Error: Into, - B: NewConfigurableService, + B: NewConfigurableService, + C: Clone, { type Request = A::Request; type Response = B::Response; type Error = B::Error; type Service = AndThen; - type Config = A::Config; type InitError = A::InitError; - type Future = AndThenNewConfigurableServiceFuture; + type Future = AndThenNewConfigurableServiceFuture; - fn new_service(&self, cfg: A::Config) -> Self::Future { + fn new_service(&self, cfg: C) -> Self::Future { AndThenNewConfigurableServiceFuture::new( self.a.new_service(cfg.clone()), self.b.new_service(cfg), @@ -284,35 +284,37 @@ where } } -impl Clone for AndThenNewConfigurableService +impl Clone for AndThenNewConfigurableService where - A: NewConfigurableService + Clone, + A: NewConfigurableService + Clone, A::Error: Into, - B: NewConfigurableService + Clone, + B: NewConfigurableService + Clone, { fn clone(&self) -> Self { Self { a: self.a.clone(), b: self.b.clone(), + c: marker::PhantomData, } } } -pub struct AndThenNewConfigurableServiceFuture +pub struct AndThenNewConfigurableServiceFuture where - A: NewConfigurableService, - B: NewConfigurableService, + A: NewConfigurableService, + B: NewConfigurableService, { fut_b: B::Future, fut_a: A::Future, a: Option, b: Option, + c: marker::PhantomData, } -impl AndThenNewConfigurableServiceFuture +impl AndThenNewConfigurableServiceFuture where - A: NewConfigurableService, - B: NewConfigurableService, + A: NewConfigurableService, + B: NewConfigurableService, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenNewConfigurableServiceFuture { @@ -320,15 +322,16 @@ where fut_b, a: None, b: None, + c: marker::PhantomData, } } } -impl Future for AndThenNewConfigurableServiceFuture +impl Future for AndThenNewConfigurableServiceFuture where - A: NewConfigurableService, + A: NewConfigurableService, A::Error: Into, - B: NewConfigurableService, + B: NewConfigurableService, { type Item = AndThen; type Error = B::InitError; @@ -354,15 +357,19 @@ where } /// `MapErrNewService` new service combinator -pub struct MapErrNewConfigurableService { +pub struct MapErrNewConfigurableService +where + A: NewConfigurableService +{ a: A, f: F, e: marker::PhantomData, + c: marker::PhantomData, } -impl MapErrNewConfigurableService +impl MapErrNewConfigurableService where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::Error) -> E, { /// Create new `MapErr` new service instance @@ -371,13 +378,14 @@ where a, f, e: marker::PhantomData, + c: marker::PhantomData, } } } -impl Clone for MapErrNewConfigurableService +impl Clone for MapErrNewConfigurableService where - A: NewConfigurableService + Clone, + A: NewConfigurableService + Clone, F: Fn(A::Error) -> E + Clone, { fn clone(&self) -> Self { @@ -385,13 +393,14 @@ where a: self.a.clone(), f: self.f.clone(), e: marker::PhantomData, + c: marker::PhantomData, } } } -impl NewConfigurableService for MapErrNewConfigurableService +impl NewConfigurableService for MapErrNewConfigurableService where - A: NewConfigurableService + Clone, + A: NewConfigurableService + Clone, F: Fn(A::Error) -> E + Clone, { type Request = A::Request; @@ -399,27 +408,26 @@ where type Error = E; type Service = MapErr; - type Config = A::Config; type InitError = A::InitError; - type Future = MapErrNewConfigurableServiceFuture; + type Future = MapErrNewConfigurableServiceFuture; - fn new_service(&self, cfg: Self::Config) -> Self::Future { + fn new_service(&self, cfg: C) -> Self::Future { MapErrNewConfigurableServiceFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapErrNewConfigurableServiceFuture +pub struct MapErrNewConfigurableServiceFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::Error) -> E, { fut: A::Future, f: F, } -impl MapErrNewConfigurableServiceFuture +impl MapErrNewConfigurableServiceFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -427,9 +435,9 @@ where } } -impl Future for MapErrNewConfigurableServiceFuture +impl Future for MapErrNewConfigurableServiceFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::Error) -> E + Clone, { type Item = MapErr; @@ -445,15 +453,19 @@ where } /// `MapInitErr` service combinator -pub struct MapInitErr { +pub struct MapInitErr +where + A: NewConfigurableService +{ a: A, f: F, e: marker::PhantomData, + c: marker::PhantomData, } -impl MapInitErr +impl MapInitErr where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::InitError) -> E, { /// Create new `MapInitErr` combinator @@ -462,13 +474,14 @@ where a, f, e: marker::PhantomData, + c: marker::PhantomData, } } } -impl Clone for MapInitErr +impl Clone for MapInitErr where - A: NewConfigurableService + Clone, + A: NewConfigurableService + Clone, F: Fn(A::InitError) -> E + Clone, { fn clone(&self) -> Self { @@ -476,13 +489,14 @@ where a: self.a.clone(), f: self.f.clone(), e: marker::PhantomData, + c: marker::PhantomData, } } } -impl NewConfigurableService for MapInitErr +impl NewConfigurableService for MapInitErr where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::InitError) -> E + Clone, { type Request = A::Request; @@ -490,27 +504,26 @@ where type Error = A::Error; type Service = A::Service; - type Config = A::Config; type InitError = E; - type Future = MapInitErrFuture; + type Future = MapInitErrFuture; - fn new_service(&self, cfg: Self::Config) -> Self::Future { + fn new_service(&self, cfg: C) -> Self::Future { MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) } } -pub struct MapInitErrFuture +pub struct MapInitErrFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::InitError) -> E, { f: F, fut: A::Future, } -impl MapInitErrFuture +impl MapInitErrFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -518,9 +531,9 @@ where } } -impl Future for MapInitErrFuture +impl Future for MapInitErrFuture where - A: NewConfigurableService, + A: NewConfigurableService, F: Fn(A::InitError) -> E, { type Item = A::Service;