From bff68c3f76025c752b9df12ebce7e1d9c05e45b9 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Wed, 16 Dec 2020 18:06:31 +0000 Subject: [PATCH] phase 2 / 4 --- actix-service/src/and_then.rs | 102 +++++++++++----------- actix-service/src/and_then_apply_fn.rs | 116 ++++++++++++++----------- actix-service/src/apply.rs | 76 ++++++++-------- actix-service/src/apply_cfg.rs | 76 ++++++++-------- actix-service/src/boxed.rs | 56 +++++------- actix-service/src/fn_service.rs | 107 ++++++++++++----------- actix-service/src/lib.rs | 90 ++++++++----------- actix-service/src/map.rs | 79 ++++++++--------- actix-service/src/map_config.rs | 76 ++++++++-------- actix-service/src/map_err.rs | 75 ++++++++-------- actix-service/src/map_init_err.rs | 31 ++++--- actix-service/src/pipeline.rs | 98 ++++++++++++--------- actix-service/src/then.rs | 100 ++++++++++----------- actix-service/src/transform.rs | 76 +++++++--------- actix-service/src/transform_err.rs | 45 +++++----- 15 files changed, 592 insertions(+), 611 deletions(-) diff --git a/actix-service/src/and_then.rs b/actix-service/src/and_then.rs index caaf8615..4ff24e03 100644 --- a/actix-service/src/and_then.rs +++ b/actix-service/src/and_then.rs @@ -1,8 +1,8 @@ -use std::cell::RefCell; use std::future::Future; use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; +use std::{cell::RefCell, marker::PhantomData}; use super::{Service, ServiceFactory}; @@ -10,34 +10,33 @@ use super::{Service, ServiceFactory}; /// of another service which completes successfully. /// /// This is created by the `Pipeline::and_then` method. -pub(crate) struct AndThenService(Rc>); +pub(crate) struct AndThenService(Rc>, PhantomData); -impl AndThenService { +impl AndThenService { /// Create new `AndThen` combinator pub(crate) fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { - Self(Rc::new(RefCell::new((a, b)))) + Self(Rc::new(RefCell::new((a, b))), PhantomData) } } -impl Clone for AndThenService { +impl Clone for AndThenService { fn clone(&self) -> Self { - AndThenService(self.0.clone()) + AndThenService(self.0.clone(), PhantomData) } } -impl Service for AndThenService +impl Service for AndThenService where - A: Service, - B: Service, + A: Service, + B: Service, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenServiceResponse; + type Future = AndThenServiceResponse; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { let mut srv = self.0.borrow_mut(); @@ -49,7 +48,7 @@ where } } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { AndThenServiceResponse { state: State::A(self.0.borrow_mut().0.call(req), Some(self.0.clone())), } @@ -57,30 +56,30 @@ where } #[pin_project::pin_project] -pub(crate) struct AndThenServiceResponse +pub(crate) struct AndThenServiceResponse where - A: Service, - B: Service, + A: Service, + B: Service, { #[pin] - state: State, + state: State, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - A: Service, - B: Service, + A: Service, + B: Service, { A(#[pin] A::Future, Option>>), B(#[pin] B::Future), Empty, } -impl Future for AndThenServiceResponse +impl Future for AndThenServiceResponse where - A: Service, - B: Service, + A: Service, + B: Service, { type Output = Result; @@ -110,13 +109,13 @@ where } /// `.and_then()` service factory combinator -pub(crate) struct AndThenServiceFactory +pub(crate) struct AndThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + A::Response, Config = A::Config, - Request = A::Response, Error = A::Error, InitError = A::InitError, >, @@ -124,13 +123,13 @@ where inner: Rc<(A, B)>, } -impl AndThenServiceFactory +impl AndThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + A::Response, Config = A::Config, - Request = A::Response, Error = A::Error, InitError = A::InitError, >, @@ -143,25 +142,24 @@ where } } -impl ServiceFactory for AndThenServiceFactory +impl ServiceFactory for AndThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + A::Response, Config = A::Config, - Request = A::Response, Error = A::Error, InitError = A::InitError, >, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; type Config = A::Config; - type Service = AndThenService; + type Service = AndThenService; type InitError = A::InitError; - type Future = AndThenServiceFactoryResponse; + type Future = AndThenServiceFactoryResponse; fn new_service(&self, cfg: A::Config) -> Self::Future { let inner = &*self.inner; @@ -172,13 +170,13 @@ where } } -impl Clone for AndThenServiceFactory +impl Clone for AndThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + A::Response, Config = A::Config, - Request = A::Response, Error = A::Error, InitError = A::InitError, >, @@ -191,10 +189,10 @@ where } #[pin_project::pin_project] -pub(crate) struct AndThenServiceFactoryResponse +pub(crate) struct AndThenServiceFactoryResponse where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory, { #[pin] fut_a: A::Future, @@ -205,10 +203,10 @@ where b: Option, } -impl AndThenServiceFactoryResponse +impl AndThenServiceFactoryResponse where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory, { fn new(fut_a: A::Future, fut_b: B::Future) -> Self { AndThenServiceFactoryResponse { @@ -220,12 +218,12 @@ where } } -impl Future for AndThenServiceFactoryResponse +impl Future for AndThenServiceFactoryResponse where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory, { - type Output = Result, A::InitError>; + type Output = Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -263,8 +261,7 @@ mod tests { struct Srv1(Rc>); - impl Service for Srv1 { - type Request = &'static str; + impl Service<&'static str> for Srv1 { type Response = &'static str; type Error = (); type Future = Ready>; @@ -282,8 +279,7 @@ mod tests { #[derive(Clone)] struct Srv2(Rc>); - impl Service for Srv2 { - type Request = &'static str; + impl Service<&'static str> for Srv2 { type Response = (&'static str, &'static str); type Error = (); type Future = Ready>; diff --git a/actix-service/src/and_then_apply_fn.rs b/actix-service/src/and_then_apply_fn.rs index b62cf505..f0602b3d 100644 --- a/actix-service/src/and_then_apply_fn.rs +++ b/actix-service/src/and_then_apply_fn.rs @@ -8,10 +8,10 @@ use std::task::{Context, Poll}; use crate::{Service, ServiceFactory}; /// `Apply` service combinator -pub(crate) struct AndThenApplyFn +pub(crate) struct AndThenApplyFn where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From + From, @@ -20,10 +20,10 @@ where r: PhantomData<(Fut, Res, Err)>, } -impl AndThenApplyFn +impl AndThenApplyFn where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From + From, @@ -37,10 +37,10 @@ where } } -impl Clone for AndThenApplyFn +impl Clone for AndThenApplyFn where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From + From, @@ -53,18 +53,17 @@ where } } -impl Service for AndThenApplyFn +impl Service for AndThenApplyFn where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From + From, { - type Request = A::Request; type Response = Res; type Error = Err; - type Future = AndThenApplyFnFuture; + type Future = AndThenApplyFnFuture; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { let mut inner = self.srv.borrow_mut(); @@ -76,7 +75,7 @@ where } } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { let fut = self.srv.borrow_mut().0.call(req); AndThenApplyFnFuture { state: State::A(fut, Some(self.srv.clone())), @@ -85,24 +84,24 @@ where } #[pin_project::pin_project] -pub(crate) struct AndThenApplyFnFuture +pub(crate) struct AndThenApplyFnFuture where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From, Err: From, { #[pin] - state: State, + state: State, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From, @@ -113,10 +112,10 @@ where Empty, } -impl Future for AndThenApplyFnFuture +impl Future for AndThenApplyFnFuture where - A: Service, - B: Service, + A: Service, + B: Service>, F: FnMut(A::Response, &mut B) -> Fut, Fut: Future>, Err: From + From, @@ -150,15 +149,19 @@ where } /// `AndThenApplyFn` service factory -pub(crate) struct AndThenApplyFnFactory { +pub(crate) struct AndThenApplyFnFactory { srv: Rc<(A, B, F)>, - r: PhantomData<(Fut, Res, Err)>, + r: PhantomData<(Req, Fut, Res, Err)>, } -impl AndThenApplyFnFactory +impl AndThenApplyFnFactory where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory< + Result, + Config = A::Config, + InitError = A::InitError, + >, F: FnMut(A::Response, &mut B::Service) -> Fut + Clone, Fut: Future>, Err: From + From, @@ -172,7 +175,7 @@ where } } -impl Clone for AndThenApplyFnFactory { +impl Clone for AndThenApplyFnFactory { fn clone(&self) -> Self { Self { srv: self.srv.clone(), @@ -181,22 +184,26 @@ impl Clone for AndThenApplyFnFactory ServiceFactory for AndThenApplyFnFactory +impl ServiceFactory + for AndThenApplyFnFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, - B: ServiceFactory, + B: ServiceFactory< + Result, + Config = A::Config, + InitError = A::InitError, + >, F: FnMut(A::Response, &mut B::Service) -> Fut + Clone, Fut: Future>, Err: From + From, { - type Request = A::Request; type Response = Res; type Error = Err; - type Service = AndThenApplyFn; + type Service = AndThenApplyFn; type Config = A::Config; type InitError = A::InitError; - type Future = AndThenApplyFnFactoryResponse; + type Future = AndThenApplyFnFactoryResponse; fn new_service(&self, cfg: A::Config) -> Self::Future { let srv = &*self.srv; @@ -211,10 +218,14 @@ where } #[pin_project::pin_project] -pub(crate) struct AndThenApplyFnFactoryResponse +pub(crate) struct AndThenApplyFnFactoryResponse where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory< + Result, + Config = A::Config, + InitError = A::InitError, + >, F: FnMut(A::Response, &mut B::Service) -> Fut + Clone, Fut: Future>, Err: From, @@ -229,16 +240,21 @@ where b: Option, } -impl Future for AndThenApplyFnFactoryResponse +impl Future + for AndThenApplyFnFactoryResponse where - A: ServiceFactory, - B: ServiceFactory, + A: ServiceFactory, + B: ServiceFactory< + Result, + Config = A::Config, + InitError = A::InitError, + >, F: FnMut(A::Response, &mut B::Service) -> Fut + Clone, Fut: Future>, Err: From + From, { type Output = - Result, A::InitError>; + Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -280,8 +296,7 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = Ready>; @@ -291,16 +306,17 @@ mod tests { } #[allow(clippy::unit_arg)] - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: ()) -> Self::Future { ok(req) } } #[actix_rt::test] async fn test_service() { - let mut srv = pipeline(ok).and_then_apply_fn(Srv, |req: &'static str, s| { - s.call(()).map_ok(move |res| (req, res)) - }); + let mut srv = pipeline(|| async { Ok(2) }) + .and_then_apply_fn(Srv, |req: &'static str, s| { + s.call(()).map_ok(move |res| (req, res)) + }); let res = lazy(|cx| srv.poll_ready(cx)).await; assert_eq!(res, Poll::Ready(Ok(()))); diff --git a/actix-service/src/apply.rs b/actix-service/src/apply.rs index d1f8ff3a..b6f6ee07 100644 --- a/actix-service/src/apply.rs +++ b/actix-service/src/apply.rs @@ -6,43 +6,46 @@ use std::task::{Context, Poll}; use super::{IntoService, IntoServiceFactory, Service, ServiceFactory}; /// Apply transform function to a service. -pub fn apply_fn(service: U, f: F) -> Apply +pub fn apply_fn( + service: U, + f: F, +) -> Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> R, R: Future>, - U: IntoService, + U: IntoService, { Apply::new(service.into_service(), f) } /// Service factory that produces `apply_fn` service. -pub fn apply_fn_factory( +pub fn apply_fn_factory( service: U, f: F, -) -> ApplyServiceFactory +) -> ApplyServiceFactory where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R + Clone, R: Future>, - U: IntoServiceFactory, + U: IntoServiceFactory, { ApplyServiceFactory::new(service.into_factory(), f) } /// `Apply` service combinator -pub struct Apply +pub struct Apply where - T: Service, + T: Service, { service: T, f: F, r: PhantomData<(In, Out, R)>, } -impl Apply +impl Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> R, R: Future>, { @@ -56,9 +59,9 @@ where } } -impl Clone for Apply +impl Clone for Apply where - T: Service + Clone, + T: Service + Clone, F: FnMut(In, &mut T) -> R + Clone, R: Future>, { @@ -71,13 +74,13 @@ where } } -impl Service for Apply +impl Service for Apply where - T: Service, + T: Service, F: FnMut(In, &mut T) -> R, R: Future>, { - type Request = In; + // type Request = In; type Response = Out; type Error = Err; type Future = R; @@ -92,9 +95,9 @@ where } /// `apply()` service factory -pub struct ApplyServiceFactory +pub struct ApplyServiceFactory where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R + Clone, R: Future>, { @@ -103,9 +106,9 @@ where r: PhantomData<(R, In, Out)>, } -impl ApplyServiceFactory +impl ApplyServiceFactory where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R + Clone, R: Future>, { @@ -119,9 +122,9 @@ where } } -impl Clone for ApplyServiceFactory +impl Clone for ApplyServiceFactory where - T: ServiceFactory + Clone, + T: ServiceFactory + Clone, F: FnMut(In, &mut T::Service) -> R + Clone, R: Future>, { @@ -134,20 +137,21 @@ where } } -impl ServiceFactory for ApplyServiceFactory +impl ServiceFactory + for ApplyServiceFactory where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R + Clone, R: Future>, { - type Request = In; + // type Request = In; type Response = Out; type Error = Err; type Config = T::Config; - type Service = Apply; + type Service = Apply; type InitError = T::InitError; - type Future = ApplyServiceFactoryResponse; + type Future = ApplyServiceFactoryResponse; fn new_service(&self, cfg: T::Config) -> Self::Future { ApplyServiceFactoryResponse::new(self.service.new_service(cfg), self.f.clone()) @@ -155,9 +159,9 @@ where } #[pin_project::pin_project] -pub struct ApplyServiceFactoryResponse +pub struct ApplyServiceFactoryResponse where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R, R: Future>, { @@ -167,9 +171,9 @@ where r: PhantomData<(In, Out)>, } -impl ApplyServiceFactoryResponse +impl ApplyServiceFactoryResponse where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R, R: Future>, { @@ -182,13 +186,14 @@ where } } -impl Future for ApplyServiceFactoryResponse +impl Future + for ApplyServiceFactoryResponse where - T: ServiceFactory, + T: ServiceFactory, F: FnMut(In, &mut T::Service) -> R, R: Future>, { - type Output = Result, T::InitError>; + type Output = Result, T::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -213,8 +218,7 @@ mod tests { #[derive(Clone)] struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = Ready>; diff --git a/actix-service/src/apply_cfg.rs b/actix-service/src/apply_cfg.rs index 0d984309..61132ebb 100644 --- a/actix-service/src/apply_cfg.rs +++ b/actix-service/src/apply_cfg.rs @@ -8,12 +8,12 @@ use std::task::{Context, Poll}; use crate::{Service, ServiceFactory}; /// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory -pub fn apply_cfg( +pub fn apply_cfg( srv: T, f: F, ) -> impl ServiceFactory< + Req, Config = C, - Request = S::Request, Response = S::Response, Error = S::Error, Service = S, @@ -22,9 +22,9 @@ pub fn apply_cfg( > + Clone where F: FnMut(C, &mut T) -> R, - T: Service, + T: Service, R: Future>, - S: Service, + S: Service, { ApplyConfigService { srv: Rc::new(RefCell::new((srv, f))), @@ -35,12 +35,12 @@ where /// Convert `Fn(Config, &mut Service1) -> Future` fn to a service factory /// /// Service1 get constructed from `T` factory. -pub fn apply_cfg_factory( +pub fn apply_cfg_factory( factory: T, f: F, ) -> impl ServiceFactory< + Req, Config = C, - Request = S::Request, Response = S::Response, Error = S::Error, Service = S, @@ -48,10 +48,10 @@ pub fn apply_cfg_factory( > + Clone where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, T::InitError: From, R: Future>, - S: Service, + S: Service, { ApplyConfigServiceFactory { srv: Rc::new(RefCell::new((factory, f))), @@ -60,23 +60,23 @@ where } /// Convert `Fn(Config, &mut Server) -> Future` fn to NewService\ -struct ApplyConfigService +struct ApplyConfigService where F: FnMut(C, &mut T) -> R, - T: Service, + T: Service, R: Future>, - S: Service, + S: Service, { srv: Rc>, _t: PhantomData<(C, R, S)>, } -impl Clone for ApplyConfigService +impl Clone for ApplyConfigService where F: FnMut(C, &mut T) -> R, - T: Service, + T: Service, R: Future>, - S: Service, + S: Service, { fn clone(&self) -> Self { ApplyConfigService { @@ -86,15 +86,14 @@ where } } -impl ServiceFactory for ApplyConfigService +impl ServiceFactory for ApplyConfigService where F: FnMut(C, &mut T) -> R, - T: Service, + T: Service, R: Future>, - S: Service, + S: Service, { type Config = C; - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; @@ -109,23 +108,23 @@ where } /// Convert `Fn(&Config) -> Future` fn to NewService -struct ApplyConfigServiceFactory +struct ApplyConfigServiceFactory where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, R: Future>, - S: Service, + S: Service, { srv: Rc>, _t: PhantomData<(C, R, S)>, } -impl Clone for ApplyConfigServiceFactory +impl Clone for ApplyConfigServiceFactory where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, R: Future>, - S: Service, + S: Service, { fn clone(&self) -> Self { Self { @@ -135,22 +134,21 @@ where } } -impl ServiceFactory for ApplyConfigServiceFactory +impl ServiceFactory for ApplyConfigServiceFactory where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, T::InitError: From, R: Future>, - S: Service, + S: Service, { type Config = C; - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Service = S; type InitError = T::InitError; - type Future = ApplyConfigServiceFactoryResponse; + type Future = ApplyConfigServiceFactoryResponse; fn new_service(&self, cfg: C) -> Self::Future { ApplyConfigServiceFactoryResponse { @@ -162,40 +160,40 @@ where } #[pin_project::pin_project] -struct ApplyConfigServiceFactoryResponse +struct ApplyConfigServiceFactoryResponse where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, T::InitError: From, R: Future>, - S: Service, + S: Service, { cfg: Option, store: Rc>, #[pin] - state: State, + state: State, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - T: ServiceFactory, + T: ServiceFactory, T::InitError: From, R: Future>, - S: Service, + S: Service, { A(#[pin] T::Future), B(T::Service), C(#[pin] R), } -impl Future for ApplyConfigServiceFactoryResponse +impl Future for ApplyConfigServiceFactoryResponse where F: FnMut(C, &mut T::Service) -> R, - T: ServiceFactory, + T: ServiceFactory, T::InitError: From, R: Future>, - S: Service, + S: Service, { type Output = Result; diff --git a/actix-service/src/boxed.rs b/actix-service/src/boxed.rs index e630f81c..acf796b8 100644 --- a/actix-service/src/boxed.rs +++ b/actix-service/src/boxed.rs @@ -9,17 +9,17 @@ use crate::{Service, ServiceFactory}; pub type BoxFuture = Pin>>>; pub type BoxService = - Box>>; + Box>>; -pub struct BoxServiceFactory(Inner); +pub struct BoxServiceFactory(Inner); /// Create boxed service factory -pub fn factory( +pub fn factory( factory: T, -) -> BoxServiceFactory +) -> BoxServiceFactory where - T: ServiceFactory + 'static, - T::Request: 'static, + T: ServiceFactory + 'static, + Req: 'static, T::Response: 'static, T::Service: 'static, T::Future: 'static, @@ -33,18 +33,18 @@ where } /// Create boxed service -pub fn service(service: T) -> BoxService +pub fn service(service: S) -> BoxService where - T: Service + 'static, - T::Future: 'static, + S: Service + 'static, + S::Future: 'static, { Box::new(ServiceWrapper(service)) } type Inner = Box< dyn ServiceFactory< + Req, Config = C, - Request = Req, Response = Res, Error = Err, InitError = InitErr, @@ -53,14 +53,14 @@ type Inner = Box< >, >; -impl ServiceFactory for BoxServiceFactory +impl ServiceFactory + for BoxServiceFactory where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, { - type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; @@ -74,29 +74,22 @@ where } } -struct FactoryWrapper { +struct FactoryWrapper, Req> { factory: T, _t: std::marker::PhantomData, } -impl ServiceFactory for FactoryWrapper +impl ServiceFactory for FactoryWrapper where Req: 'static, Res: 'static, Err: 'static, InitErr: 'static, - T: ServiceFactory< - Config = C, - Request = Req, - Response = Res, - Error = Err, - InitError = InitErr, - >, + T: ServiceFactory, T::Future: 'static, T::Service: 'static, - ::Future: 'static, + >::Future: 'static, { - type Request = Req; type Response = Res; type Error = Err; type InitError = InitErr; @@ -113,24 +106,23 @@ where } } -struct ServiceWrapper(T); +struct ServiceWrapper, Req>(S); -impl ServiceWrapper +impl ServiceWrapper where - T: Service + 'static, - T::Future: 'static, + S: Service + 'static, + S::Future: 'static, { - fn boxed(service: T) -> BoxService { + fn boxed(service: S) -> BoxService { Box::new(ServiceWrapper(service)) } } -impl Service for ServiceWrapper +impl Service for ServiceWrapper where - T: Service, + T: Service, T::Future: 'static, { - type Request = Req; type Response = Res; type Error = Err; type Future = BoxFuture; @@ -139,7 +131,7 @@ where self.0.poll_ready(ctx) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { Box::pin(self.0.call(req)) } } diff --git a/actix-service/src/fn_service.rs b/actix-service/src/fn_service.rs index 08690a5d..5a2a1b60 100644 --- a/actix-service/src/fn_service.rs +++ b/actix-service/src/fn_service.rs @@ -53,9 +53,11 @@ where /// Ok(()) /// } /// ``` -pub fn fn_factory(f: F) -> FnServiceNoConfig +pub fn fn_factory( + f: F, +) -> FnServiceNoConfig where - Srv: Service, + Srv: Service, F: Fn() -> Fut, Fut: Future>, { @@ -92,13 +94,13 @@ where /// Ok(()) /// } /// ``` -pub fn fn_factory_with_config( +pub fn fn_factory_with_config( f: F, -) -> FnServiceConfig +) -> FnServiceConfig where F: Fn(Cfg) -> Fut, Fut: Future>, - Srv: Service, + Srv: Service, { FnServiceConfig::new(f) } @@ -132,12 +134,11 @@ where } } -impl Service for FnService +impl Service for FnService where F: FnMut(Req) -> Fut, Fut: Future>, { - type Request = Req; type Response = Res; type Error = Err; type Future = Fut; @@ -151,7 +152,7 @@ where } } -impl IntoService> for F +impl IntoService, Req> for F where F: FnMut(Req) -> Fut, Fut: Future>, @@ -190,12 +191,11 @@ where } } -impl Service for FnServiceFactory +impl Service for FnServiceFactory where F: FnMut(Req) -> Fut + Clone, Fut: Future>, { - type Request = Req; type Response = Res; type Error = Err; type Future = Fut; @@ -204,17 +204,17 @@ where Poll::Ready(Ok(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { (self.f)(req) } } -impl ServiceFactory for FnServiceFactory +impl ServiceFactory + for FnServiceFactory where F: FnMut(Req) -> Fut + Clone, Fut: Future>, { - type Request = Req; type Response = Res; type Error = Err; @@ -229,7 +229,7 @@ where } impl - IntoServiceFactory> for F + IntoServiceFactory, Req> for F where F: Fn(Req) -> Fut + Clone, Fut: Future>, @@ -240,32 +240,32 @@ where } /// Convert `Fn(&Config) -> Future` fn to NewService -pub struct FnServiceConfig +pub struct FnServiceConfig where F: Fn(Cfg) -> Fut, Fut: Future>, - Srv: Service, + Srv: Service, { f: F, _t: PhantomData<(Fut, Cfg, Srv, Err)>, } -impl FnServiceConfig +impl FnServiceConfig where F: Fn(Cfg) -> Fut, Fut: Future>, - Srv: Service, + Srv: Service, { fn new(f: F) -> Self { FnServiceConfig { f, _t: PhantomData } } } -impl Clone for FnServiceConfig +impl Clone for FnServiceConfig where F: Fn(Cfg) -> Fut + Clone, Fut: Future>, - Srv: Service, + Srv: Service, { fn clone(&self) -> Self { FnServiceConfig { @@ -275,13 +275,13 @@ where } } -impl ServiceFactory for FnServiceConfig +impl ServiceFactory + for FnServiceConfig where F: Fn(Cfg) -> Fut, Fut: Future>, - Srv: Service, + Srv: Service, { - type Request = Srv::Request; type Response = Srv::Response; type Error = Srv::Error; @@ -296,64 +296,65 @@ where } /// Converter for `Fn() -> Future` fn -pub struct FnServiceNoConfig +pub struct FnServiceNoConfig where - F: Fn() -> R, - S: Service, - R: Future>, + F: Fn() -> Fut, + Srv: Service, + Fut: Future>, { f: F, - _t: PhantomData, + _t: PhantomData, } -impl FnServiceNoConfig +impl FnServiceNoConfig where - F: Fn() -> R, - R: Future>, - S: Service, + F: Fn() -> Fut, + Fut: Future>, + Srv: Service, { fn new(f: F) -> Self { Self { f, _t: PhantomData } } } -impl ServiceFactory for FnServiceNoConfig +impl ServiceFactory + for FnServiceNoConfig where - F: Fn() -> R, - R: Future>, - S: Service, + F: Fn() -> Fut, + Fut: Future>, + Srv: Service, { - type Request = S::Request; - type Response = S::Response; - type Error = S::Error; - type Service = S; - type Config = C; - type InitError = E; - type Future = R; + type Response = Srv::Response; + type Error = Srv::Error; + type Service = Srv; + type Config = Cfg; + type InitError = Err; + type Future = Fut; - fn new_service(&self, _: C) -> Self::Future { + fn new_service(&self, _: Cfg) -> Self::Future { (self.f)() } } -impl Clone for FnServiceNoConfig +impl Clone for FnServiceNoConfig where - F: Fn() -> R + Clone, - R: Future>, - S: Service, + F: Fn() -> Fut + Clone, + Fut: Future>, + Srv: Service, { fn clone(&self) -> Self { Self::new(self.f.clone()) } } -impl IntoServiceFactory> for F +impl + IntoServiceFactory, Req> for F where - F: Fn() -> R, - R: Future>, - S: Service, + F: Fn() -> Fut, + Fut: Future>, + Srv: Service, { - fn into_factory(self) -> FnServiceNoConfig { + fn into_factory(self) -> FnServiceNoConfig { FnServiceNoConfig::new(self) } } diff --git a/actix-service/src/lib.rs b/actix-service/src/lib.rs index 96c878e3..6067595c 100644 --- a/actix-service/src/lib.rs +++ b/actix-service/src/lib.rs @@ -72,14 +72,11 @@ pub use self::transform::{apply, Transform}; /// ```rust,ignore /// async fn my_service(req: u8) -> Result; /// ``` -pub trait Service { - /// Requests handled by the service. - type Request; - +pub trait Service { /// Responses given by the service. type Response; - /// Errors produced by the service. + /// Errors produced by the service when polling readiness or executing call. type Error; /// The future response value. @@ -109,7 +106,7 @@ pub trait Service { /// /// Calling `call` without calling `poll_ready` is permitted. The /// implementation must be resilient to this fact. - fn call(&mut self, req: Self::Request) -> Self::Future; + fn call(&mut self, req: Req) -> Self::Future; /// Map this service's output to a different type, returning a new service /// of the resulting type. @@ -120,7 +117,7 @@ pub trait Service { /// Note that this function consumes the receiving service and returns a /// wrapped version of it, similar to the existing `map` methods in the /// standard library. - fn map(self, f: F) -> crate::dev::Map + fn map(self, f: F) -> crate::dev::Map where Self: Sized, F: FnMut(Self::Response) -> R, @@ -136,7 +133,7 @@ pub trait Service { /// /// Note that this function consumes the receiving service and returns a /// wrapped version of it. - fn map_err(self, f: F) -> crate::dev::MapErr + fn map_err(self, f: F) -> crate::dev::MapErr where Self: Sized, F: Fn(Self::Error) -> E, @@ -154,10 +151,7 @@ pub trait Service { /// requests on that new TCP stream. /// /// `Config` is a service factory configuration type. -pub trait ServiceFactory { - /// Requests handled by the created services. - type Request; - +pub trait ServiceFactory { /// Responses given by the created services. type Response; @@ -168,11 +162,7 @@ pub trait ServiceFactory { type Config; /// The kind of `Service` created by this factory. - type Service: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Service: Service; /// Errors potentially raised while building a service. type InitError; @@ -185,7 +175,7 @@ pub trait ServiceFactory { /// Map this service's output to a different type, returning a new service /// of the resulting type. - fn map(self, f: F) -> crate::map::MapServiceFactory + fn map(self, f: F) -> crate::map::MapServiceFactory where Self: Sized, F: FnMut(Self::Response) -> R + Clone, @@ -194,7 +184,7 @@ pub trait ServiceFactory { } /// Map this service's error to a different error, returning a new service. - fn map_err(self, f: F) -> crate::map_err::MapErrServiceFactory + fn map_err(self, f: F) -> crate::map_err::MapErrServiceFactory where Self: Sized, F: Fn(Self::Error) -> E + Clone, @@ -203,7 +193,7 @@ pub trait ServiceFactory { } /// Map this factory's init error to a different error, returning a new service. - fn map_init_err(self, f: F) -> crate::map_init_err::MapInitErr + fn map_init_err(self, f: F) -> crate::map_init_err::MapInitErr where Self: Sized, F: Fn(Self::InitError) -> E + Clone, @@ -212,11 +202,10 @@ pub trait ServiceFactory { } } -impl<'a, S> Service for &'a mut S +impl<'a, S, Req> Service for &'a mut S where - S: Service + 'a, + S: Service + 'a, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -225,16 +214,15 @@ where (**self).poll_ready(ctx) } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: Req) -> S::Future { (**self).call(request) } } -impl Service for Box +impl Service for Box where - S: Service + ?Sized, + S: Service + ?Sized, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -243,16 +231,15 @@ where (**self).poll_ready(ctx) } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: Req) -> S::Future { (**self).call(request) } } -impl Service for RefCell +impl Service for RefCell where - S: Service, + S: Service, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -261,16 +248,15 @@ where self.borrow_mut().poll_ready(ctx) } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: Req) -> S::Future { self.borrow_mut().call(request) } } -impl Service for Rc> +impl Service for Rc> where - S: Service, + S: Service, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Future = S::Future; @@ -279,16 +265,15 @@ where self.borrow_mut().poll_ready(ctx) } - fn call(&mut self, request: Self::Request) -> S::Future { + fn call(&mut self, request: Req) -> S::Future { (&mut (**self).borrow_mut()).call(request) } } -impl ServiceFactory for Rc +impl ServiceFactory for Rc where - S: ServiceFactory, + S: ServiceFactory, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Config = S::Config; @@ -301,11 +286,10 @@ where } } -impl ServiceFactory for Arc +impl ServiceFactory for Arc where - S: ServiceFactory, + S: ServiceFactory, { - type Request = S::Request; type Response = S::Response; type Error = S::Error; type Config = S::Config; @@ -319,35 +303,35 @@ where } /// Trait for types that can be converted to a `Service` -pub trait IntoService +pub trait IntoService where - T: Service, + T: Service, { /// Convert to a `Service` fn into_service(self) -> T; } /// Trait for types that can be converted to a `ServiceFactory` -pub trait IntoServiceFactory +pub trait IntoServiceFactory where - T: ServiceFactory, + T: ServiceFactory, { /// Convert `Self` to a `ServiceFactory` fn into_factory(self) -> T; } -impl IntoService for T +impl IntoService for T where - T: Service, + T: Service, { fn into_service(self) -> T { self } } -impl IntoServiceFactory for T +impl IntoServiceFactory for T where - T: ServiceFactory, + T: ServiceFactory, { fn into_factory(self) -> T { self @@ -355,10 +339,10 @@ where } /// Convert object of type `T` to a service `S` -pub fn into_service(tp: T) -> S +pub fn into_service(tp: T) -> S where - S: Service, - T: IntoService, + S: Service, + T: IntoService, { tp.into_service() } diff --git a/actix-service/src/map.rs b/actix-service/src/map.rs index ec3520a1..05a9401a 100644 --- a/actix-service/src/map.rs +++ b/actix-service/src/map.rs @@ -8,18 +8,18 @@ use super::{Service, ServiceFactory}; /// Service for the `map` combinator, changing the type of a service's response. /// /// This is created by the `ServiceExt::map` method. -pub struct Map { +pub struct Map { service: A, f: F, - _t: PhantomData, + _t: PhantomData<(Req, Res)>, } -impl Map { +impl Map { /// Create new `Map` combinator pub(crate) fn new(service: A, f: F) -> Self where - A: Service, - F: FnMut(A::Response) -> Response, + A: Service, + F: FnMut(A::Response) -> Res, { Self { service, @@ -29,7 +29,7 @@ impl Map { } } -impl Clone for Map +impl Clone for Map where A: Clone, F: Clone, @@ -43,52 +43,51 @@ where } } -impl Service for Map +impl Service for Map where - A: Service, - F: FnMut(A::Response) -> Response + Clone, + A: Service, + F: FnMut(A::Response) -> Res + Clone, { - type Request = A::Request; - type Response = Response; + type Response = Res; type Error = A::Error; - type Future = MapFuture; + type Future = MapFuture; fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx) } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { MapFuture::new(self.service.call(req), self.f.clone()) } } #[pin_project::pin_project] -pub struct MapFuture +pub struct MapFuture where - A: Service, - F: FnMut(A::Response) -> Response, + A: Service, + F: FnMut(A::Response) -> Res, { f: F, #[pin] fut: A::Future, } -impl MapFuture +impl MapFuture where - A: Service, - F: FnMut(A::Response) -> Response, + A: Service, + F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { MapFuture { f, fut } } } -impl Future for MapFuture +impl Future for MapFuture where - A: Service, - F: FnMut(A::Response) -> Response, + A: Service, + F: FnMut(A::Response) -> Res, { - type Output = Result; + type Output = Result; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -102,17 +101,17 @@ where } /// `MapNewService` new service combinator -pub struct MapServiceFactory { +pub struct MapServiceFactory { a: A, f: F, r: PhantomData, } -impl MapServiceFactory { +impl MapServiceFactory { /// Create new `Map` new service instance pub(crate) fn new(a: A, f: F) -> Self where - A: ServiceFactory, + A: ServiceFactory, F: FnMut(A::Response) -> Res, { Self { @@ -123,7 +122,7 @@ impl MapServiceFactory { } } -impl Clone for MapServiceFactory +impl Clone for MapServiceFactory where A: Clone, F: Clone, @@ -137,19 +136,18 @@ where } } -impl ServiceFactory for MapServiceFactory +impl ServiceFactory for MapServiceFactory where - A: ServiceFactory, + A: ServiceFactory, F: FnMut(A::Response) -> Res + Clone, { - type Request = A::Request; type Response = Res; type Error = A::Error; type Config = A::Config; - type Service = Map; + type Service = Map; type InitError = A::InitError; - type Future = MapServiceFuture; + type Future = MapServiceFuture; fn new_service(&self, cfg: A::Config) -> Self::Future { MapServiceFuture::new(self.a.new_service(cfg), self.f.clone()) @@ -157,9 +155,9 @@ where } #[pin_project::pin_project] -pub struct MapServiceFuture +pub struct MapServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: FnMut(A::Response) -> Res, { #[pin] @@ -167,9 +165,9 @@ where f: Option, } -impl MapServiceFuture +impl MapServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: FnMut(A::Response) -> Res, { fn new(fut: A::Future, f: F) -> Self { @@ -177,12 +175,12 @@ where } } -impl Future for MapServiceFuture +impl Future for MapServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: FnMut(A::Response) -> Res, { - type Output = Result, A::InitError>; + type Output = Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -204,8 +202,7 @@ mod tests { struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = Ready>; diff --git a/actix-service/src/map_config.rs b/actix-service/src/map_config.rs index 7a1a1b89..93abf49e 100644 --- a/actix-service/src/map_config.rs +++ b/actix-service/src/map_config.rs @@ -6,37 +6,37 @@ use super::{IntoServiceFactory, ServiceFactory}; /// /// Note that this function consumes the receiving service factory and returns /// a wrapped version of it. -pub fn map_config(factory: U, f: F) -> MapConfig +pub fn map_config(factory: U, f: F) -> MapConfig where - T: ServiceFactory, - U: IntoServiceFactory, + T: ServiceFactory, + U: IntoServiceFactory, F: Fn(C) -> T::Config, { MapConfig::new(factory.into_factory(), f) } /// Replace config with unit -pub fn unit_config(factory: U) -> UnitConfig +pub fn unit_config(factory: U) -> UnitConfig where - T: ServiceFactory, - U: IntoServiceFactory, + T: ServiceFactory, + U: IntoServiceFactory, { UnitConfig::new(factory.into_factory()) } /// `map_config()` adapter service factory -pub struct MapConfig { +pub struct MapConfig { a: A, f: F, - e: PhantomData, + e: PhantomData<(C, Req)>, } -impl MapConfig { +impl MapConfig { /// Create new `MapConfig` combinator - pub(crate) fn new(a: A, f: F) -> Self + pub(crate) fn new(a: T, f: F) -> Self where - A: ServiceFactory, - F: Fn(C) -> A::Config, + T: ServiceFactory, + F: Fn(C) -> T::Config, { Self { a, @@ -46,9 +46,9 @@ impl MapConfig { } } -impl Clone for MapConfig +impl Clone for MapConfig where - A: Clone, + T: Clone, F: Clone, { fn clone(&self) -> Self { @@ -60,19 +60,18 @@ where } } -impl ServiceFactory for MapConfig +impl ServiceFactory for MapConfig where - A: ServiceFactory, - F: Fn(C) -> A::Config, + T: ServiceFactory, + F: Fn(C) -> T::Config, { - type Request = A::Request; - type Response = A::Response; - type Error = A::Error; + type Response = T::Response; + type Error = T::Error; type Config = C; - type Service = A::Service; - type InitError = A::InitError; - type Future = A::Future; + type Service = T::Service; + type InitError = T::InitError; + type Future = T::Future; fn new_service(&self, cfg: C) -> Self::Future { self.a.new_service((self.f)(cfg)) @@ -80,24 +79,24 @@ where } /// `unit_config()` config combinator -pub struct UnitConfig { - a: A, - e: PhantomData, +pub struct UnitConfig { + a: T, + e: PhantomData<(C, Req)>, } -impl UnitConfig +impl UnitConfig where - A: ServiceFactory, + T: ServiceFactory, { /// Create new `UnitConfig` combinator - pub(crate) fn new(a: A) -> Self { + pub(crate) fn new(a: T) -> Self { Self { a, e: PhantomData } } } -impl Clone for UnitConfig +impl Clone for UnitConfig where - A: Clone, + T: Clone, { fn clone(&self) -> Self { Self { @@ -107,18 +106,17 @@ where } } -impl ServiceFactory for UnitConfig +impl ServiceFactory for UnitConfig where - A: ServiceFactory, + T: ServiceFactory, { - type Request = A::Request; - type Response = A::Response; - type Error = A::Error; + type Response = T::Response; + type Error = T::Error; type Config = C; - type Service = A::Service; - type InitError = A::InitError; - type Future = A::Future; + type Service = T::Service; + type InitError = T::InitError; + type Future = T::Future; fn new_service(&self, _: C) -> Self::Future { self.a.new_service(()) diff --git a/actix-service/src/map_err.rs b/actix-service/src/map_err.rs index ee7145c3..970cf714 100644 --- a/actix-service/src/map_err.rs +++ b/actix-service/src/map_err.rs @@ -9,18 +9,18 @@ use super::{Service, ServiceFactory}; /// error. /// /// This is created by the `ServiceExt::map_err` method. -pub struct MapErr { - service: A, +pub struct MapErr { + service: S, f: F, _t: PhantomData, } -impl MapErr { +impl MapErr { /// Create new `MapErr` combinator - pub(crate) fn new(service: A, f: F) -> Self + pub(crate) fn new(service: S, f: F) -> Self where - A: Service, - F: Fn(A::Error) -> E, + S: Service, + F: Fn(S::Error) -> E, { Self { service, @@ -30,9 +30,9 @@ impl MapErr { } } -impl Clone for MapErr +impl Clone for MapErr where - A: Clone, + S: Clone, F: Clone, { fn clone(&self) -> Self { @@ -44,29 +44,28 @@ where } } -impl Service for MapErr +impl Service for MapErr where - A: Service, + A: Service, F: Fn(A::Error) -> E + Clone, { - type Request = A::Request; type Response = A::Response; type Error = E; - type Future = MapErrFuture; + type Future = MapErrFuture; fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll> { self.service.poll_ready(ctx).map_err(&self.f) } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { MapErrFuture::new(self.service.call(req), self.f.clone()) } } #[pin_project::pin_project] -pub struct MapErrFuture +pub struct MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { f: F, @@ -74,9 +73,9 @@ where fut: A::Future, } -impl MapErrFuture +impl MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -84,9 +83,9 @@ where } } -impl Future for MapErrFuture +impl Future for MapErrFuture where - A: Service, + A: Service, F: Fn(A::Error) -> E, { type Output = Result; @@ -101,9 +100,9 @@ where /// service's error. /// /// This is created by the `NewServiceExt::map_err` method. -pub struct MapErrServiceFactory +pub struct MapErrServiceFactory where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E + Clone, { a: A, @@ -111,9 +110,9 @@ where e: PhantomData, } -impl MapErrServiceFactory +impl MapErrServiceFactory where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E + Clone, { /// Create new `MapErr` new service instance @@ -126,9 +125,9 @@ where } } -impl Clone for MapErrServiceFactory +impl Clone for MapErrServiceFactory where - A: ServiceFactory + Clone, + A: ServiceFactory + Clone, F: Fn(A::Error) -> E + Clone, { fn clone(&self) -> Self { @@ -140,19 +139,18 @@ where } } -impl ServiceFactory for MapErrServiceFactory +impl ServiceFactory for MapErrServiceFactory where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E + Clone, { - type Request = A::Request; type Response = A::Response; type Error = E; type Config = A::Config; - type Service = MapErr; + type Service = MapErr; type InitError = A::InitError; - type Future = MapErrServiceFuture; + type Future = MapErrServiceFuture; fn new_service(&self, cfg: A::Config) -> Self::Future { MapErrServiceFuture::new(self.a.new_service(cfg), self.f.clone()) @@ -160,9 +158,9 @@ where } #[pin_project::pin_project] -pub struct MapErrServiceFuture +pub struct MapErrServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E, { #[pin] @@ -170,9 +168,9 @@ where f: F, } -impl MapErrServiceFuture +impl MapErrServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -180,12 +178,12 @@ where } } -impl Future for MapErrServiceFuture +impl Future for MapErrServiceFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::Error) -> E + Clone, { - type Output = Result, A::InitError>; + type Output = Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -206,8 +204,7 @@ mod tests { struct Srv; - impl Service for Srv { - type Request = (); + impl Service<()> for Srv { type Response = (); type Error = (); type Future = Ready>; diff --git a/actix-service/src/map_init_err.rs b/actix-service/src/map_init_err.rs index b1eec072..f71f3add 100644 --- a/actix-service/src/map_init_err.rs +++ b/actix-service/src/map_init_err.rs @@ -6,16 +6,16 @@ use std::task::{Context, Poll}; use super::ServiceFactory; /// `MapInitErr` service combinator -pub struct MapInitErr { +pub struct MapInitErr { a: A, f: F, - e: PhantomData, + e: PhantomData<(Req, Err)>, } -impl MapInitErr +impl MapInitErr where - A: ServiceFactory, - F: Fn(A::InitError) -> E, + A: ServiceFactory, + F: Fn(A::InitError) -> Err, { /// Create new `MapInitErr` combinator pub(crate) fn new(a: A, f: F) -> Self { @@ -27,7 +27,7 @@ where } } -impl Clone for MapInitErr +impl Clone for MapInitErr where A: Clone, F: Clone, @@ -41,19 +41,18 @@ where } } -impl ServiceFactory for MapInitErr +impl ServiceFactory for MapInitErr where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::InitError) -> E + Clone, { - type Request = A::Request; type Response = A::Response; type Error = A::Error; type Config = A::Config; type Service = A::Service; type InitError = E; - type Future = MapInitErrFuture; + type Future = MapInitErrFuture; fn new_service(&self, cfg: A::Config) -> Self::Future { MapInitErrFuture::new(self.a.new_service(cfg), self.f.clone()) @@ -61,9 +60,9 @@ where } #[pin_project::pin_project] -pub struct MapInitErrFuture +pub struct MapInitErrFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::InitError) -> E, { f: F, @@ -71,9 +70,9 @@ where fut: A::Future, } -impl MapInitErrFuture +impl MapInitErrFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::InitError) -> E, { fn new(fut: A::Future, f: F) -> Self { @@ -81,9 +80,9 @@ where } } -impl Future for MapInitErrFuture +impl Future for MapInitErrFuture where - A: ServiceFactory, + A: ServiceFactory, F: Fn(A::InitError) -> E, { type Output = Result; diff --git a/actix-service/src/pipeline.rs b/actix-service/src/pipeline.rs index 75cd6af9..31ad705a 100644 --- a/actix-service/src/pipeline.rs +++ b/actix-service/src/pipeline.rs @@ -1,5 +1,5 @@ -use std::future::Future; use std::task::{Context, Poll}; +use std::{future::Future, marker::PhantomData}; use crate::and_then::{AndThenService, AndThenServiceFactory}; use crate::and_then_apply_fn::{AndThenApplyFn, AndThenApplyFnFactory}; @@ -10,33 +10,36 @@ use crate::then::{ThenService, ThenServiceFactory}; use crate::{IntoService, IntoServiceFactory, Service, ServiceFactory}; /// Construct new pipeline with one service in pipeline chain. -pub fn pipeline(service: F) -> Pipeline +pub fn pipeline(service: F) -> Pipeline where - F: IntoService, - T: Service, + F: IntoService, + T: Service, { Pipeline { service: service.into_service(), + _phantom: PhantomData, } } /// Construct new pipeline factory with one service factory. -pub fn pipeline_factory(factory: F) -> PipelineFactory +pub fn pipeline_factory(factory: F) -> PipelineFactory where - T: ServiceFactory, - F: IntoServiceFactory, + T: ServiceFactory, + F: IntoServiceFactory, { PipelineFactory { factory: factory.into_factory(), + _phantom: PhantomData, } } /// Pipeline service - pipeline allows to compose multiple service into one service. -pub struct Pipeline { +pub struct Pipeline { service: T, + _phantom: PhantomData, } -impl Pipeline { +impl, Req> Pipeline { /// Call another service after call to this one has resolved successfully. /// /// This function can be used to chain two services together and ensure that @@ -49,16 +52,15 @@ impl Pipeline { pub fn and_then( self, service: F, - ) -> Pipeline< - impl Service + Clone, - > + ) -> Pipeline + Clone, Req> where Self: Sized, - F: IntoService, - U: Service, + F: IntoService, + U: Service, { Pipeline { service: AndThenService::new(self.service, service.into_service()), + _phantom: PhantomData, } } @@ -70,17 +72,18 @@ impl Pipeline { self, service: I, f: F, - ) -> Pipeline + Clone> + ) -> Pipeline + Clone, Req> where Self: Sized, - I: IntoService, - U: Service, + I: IntoService, + U: Service, F: FnMut(T::Response, &mut U) -> Fut, Fut: Future>, Err: From + From, { Pipeline { service: AndThenApplyFn::new(self.service, service.into_service(), f), + _phantom: PhantomData, } } @@ -92,16 +95,15 @@ impl Pipeline { pub fn then( self, service: F, - ) -> Pipeline< - impl Service + Clone, - > + ) -> Pipeline + Clone, Req> where Self: Sized, - F: IntoService, - U: Service, Error = T::Error>, + F: IntoService, + U: Service, Error = T::Error>, { Pipeline { service: ThenService::new(self.service, service.into_service()), + _phantom: PhantomData, } } @@ -114,13 +116,14 @@ impl Pipeline { /// Note that this function consumes the receiving service and returns a /// wrapped version of it, similar to the existing `map` methods in the /// standard library. - pub fn map(self, f: F) -> Pipeline> + pub fn map(self, f: F) -> Pipeline, Req> where Self: Sized, F: FnMut(T::Response) -> R, { Pipeline { service: Map::new(self.service, f), + _phantom: PhantomData, } } @@ -132,30 +135,31 @@ impl Pipeline { /// /// Note that this function consumes the receiving service and returns a /// wrapped version of it. - pub fn map_err(self, f: F) -> Pipeline> + pub fn map_err(self, f: F) -> Pipeline, Req> where Self: Sized, F: Fn(T::Error) -> E, { Pipeline { service: MapErr::new(self.service, f), + _phantom: PhantomData, } } } -impl Clone for Pipeline +impl Clone for Pipeline where T: Clone, { fn clone(&self) -> Self { Pipeline { service: self.service.clone(), + _phantom: PhantomData, } } } -impl Service for Pipeline { - type Request = T::Request; +impl, Req> Service for Pipeline { type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -166,17 +170,18 @@ impl Service for Pipeline { } #[inline] - fn call(&mut self, req: T::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { self.service.call(req) } } /// Pipeline factory -pub struct PipelineFactory { +pub struct PipelineFactory { factory: T, + _phantom: PhantomData, } -impl PipelineFactory { +impl, Req> PipelineFactory { /// Call another service after call to this one has resolved successfully. pub fn and_then( self, @@ -194,20 +199,22 @@ impl PipelineFactory { Error = T::Error, > + Clone, > + Clone, + Req, > where Self: Sized, T::Config: Clone, - F: IntoServiceFactory, + F: IntoServiceFactory, U: ServiceFactory< + T::Response, Config = T::Config, - Request = T::Response, Error = T::Error, InitError = T::InitError, >, { PipelineFactory { factory: AndThenServiceFactory::new(self.factory, factory.into_factory()), + _phantom: PhantomData, } } @@ -228,18 +235,20 @@ impl PipelineFactory { InitError = T::InitError, Service = impl Service + Clone, > + Clone, + Req, > where Self: Sized, T::Config: Clone, - I: IntoServiceFactory, - U: ServiceFactory, + I: IntoServiceFactory, + U: ServiceFactory, F: FnMut(T::Response, &mut U::Service) -> Fut + Clone, Fut: Future>, Err: From + From, { PipelineFactory { factory: AndThenApplyFnFactory::new(self.factory, factory.into_factory(), f), + _phantom: PhantomData, } } @@ -265,72 +274,77 @@ impl PipelineFactory { Error = T::Error, > + Clone, > + Clone, + Req, > where Self: Sized, T::Config: Clone, - F: IntoServiceFactory, + F: IntoServiceFactory, U: ServiceFactory< + Result, Config = T::Config, - Request = Result, Error = T::Error, InitError = T::InitError, >, { PipelineFactory { factory: ThenServiceFactory::new(self.factory, factory.into_factory()), + _phantom: PhantomData, } } /// Map this service's output to a different type, returning a new service /// of the resulting type. - pub fn map(self, f: F) -> PipelineFactory> + pub fn map(self, f: F) -> PipelineFactory, Req> where Self: Sized, F: FnMut(T::Response) -> R + Clone, { PipelineFactory { factory: MapServiceFactory::new(self.factory, f), + _phantom: PhantomData, } } /// Map this service's error to a different error, returning a new service. - pub fn map_err(self, f: F) -> PipelineFactory> + pub fn map_err(self, f: F) -> PipelineFactory, Req> where Self: Sized, F: Fn(T::Error) -> E + Clone, { PipelineFactory { factory: MapErrServiceFactory::new(self.factory, f), + _phantom: PhantomData, } } /// Map this factory's init error to a different error, returning a new service. - pub fn map_init_err(self, f: F) -> PipelineFactory> + pub fn map_init_err(self, f: F) -> PipelineFactory, Req> where Self: Sized, F: Fn(T::InitError) -> E + Clone, { PipelineFactory { factory: MapInitErr::new(self.factory, f), + _phantom: PhantomData, } } } -impl Clone for PipelineFactory +impl Clone for PipelineFactory where T: Clone, { fn clone(&self) -> Self { PipelineFactory { factory: self.factory.clone(), + _phantom: PhantomData, } } } -impl ServiceFactory for PipelineFactory { +impl, Req> ServiceFactory for PipelineFactory { type Config = T::Config; - type Request = T::Request; type Response = T::Response; type Error = T::Error; type Service = T::Service; diff --git a/actix-service/src/then.rs b/actix-service/src/then.rs index 3da46cbb..021e5484 100644 --- a/actix-service/src/then.rs +++ b/actix-service/src/then.rs @@ -1,8 +1,8 @@ -use std::cell::RefCell; use std::future::Future; use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; +use std::{cell::RefCell, marker::PhantomData}; use super::{Service, ServiceFactory}; @@ -10,34 +10,33 @@ use super::{Service, ServiceFactory}; /// another service. /// /// This is created by the `Pipeline::then` method. -pub(crate) struct ThenService(Rc>); +pub(crate) struct ThenService(Rc>, PhantomData); -impl ThenService { +impl ThenService { /// Create new `.then()` combinator - pub(crate) fn new(a: A, b: B) -> ThenService + pub(crate) fn new(a: A, b: B) -> ThenService where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { - Self(Rc::new(RefCell::new((a, b)))) + Self(Rc::new(RefCell::new((a, b))), PhantomData) } } -impl Clone for ThenService { +impl Clone for ThenService { fn clone(&self) -> Self { - ThenService(self.0.clone()) + ThenService(self.0.clone(), PhantomData) } } -impl Service for ThenService +impl Service for ThenService where - A: Service, - B: Service, Error = A::Error>, + A: Service, + B: Service, Error = A::Error>, { - type Request = A::Request; type Response = B::Response; type Error = B::Error; - type Future = ThenServiceResponse; + type Future = ThenServiceResponse; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { let mut srv = self.0.borrow_mut(); @@ -49,7 +48,7 @@ where } } - fn call(&mut self, req: A::Request) -> Self::Future { + fn call(&mut self, req: Req) -> Self::Future { ThenServiceResponse { state: State::A(self.0.borrow_mut().0.call(req), Some(self.0.clone())), } @@ -57,30 +56,30 @@ where } #[pin_project::pin_project] -pub(crate) struct ThenServiceResponse +pub(crate) struct ThenServiceResponse where - A: Service, - B: Service>, + A: Service, + B: Service>, { #[pin] - state: State, + state: State, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - A: Service, - B: Service>, + A: Service, + B: Service>, { A(#[pin] A::Future, Option>>), B(#[pin] B::Future), Empty, } -impl Future for ThenServiceResponse +impl Future for ThenServiceResponse where - A: Service, - B: Service>, + A: Service, + B: Service>, { type Output = Result; @@ -110,44 +109,43 @@ where } /// `.then()` service factory combinator -pub(crate) struct ThenServiceFactory(Rc<(A, B)>); +pub(crate) struct ThenServiceFactory(Rc<(A, B)>, PhantomData); -impl ThenServiceFactory +impl ThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + Result, Config = A::Config, - Request = Result, Error = A::Error, InitError = A::InitError, >, { /// Create new `AndThen` combinator pub(crate) fn new(a: A, b: B) -> Self { - Self(Rc::new((a, b))) + Self(Rc::new((a, b)), PhantomData) } } -impl ServiceFactory for ThenServiceFactory +impl ServiceFactory for ThenServiceFactory where - A: ServiceFactory, + A: ServiceFactory, A::Config: Clone, B: ServiceFactory< + Result, Config = A::Config, - Request = Result, Error = A::Error, InitError = A::InitError, >, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; type Config = A::Config; - type Service = ThenService; + type Service = ThenService; type InitError = A::InitError; - type Future = ThenServiceFactoryResponse; + type Future = ThenServiceFactoryResponse; fn new_service(&self, cfg: A::Config) -> Self::Future { let srv = &*self.0; @@ -155,19 +153,19 @@ where } } -impl Clone for ThenServiceFactory { +impl Clone for ThenServiceFactory { fn clone(&self) -> Self { - Self(self.0.clone()) + Self(self.0.clone(), PhantomData) } } #[pin_project::pin_project] -pub(crate) struct ThenServiceFactoryResponse +pub(crate) struct ThenServiceFactoryResponse where - A: ServiceFactory, + A: ServiceFactory, B: ServiceFactory< + Result, Config = A::Config, - Request = Result, Error = A::Error, InitError = A::InitError, >, @@ -180,12 +178,12 @@ where b: Option, } -impl ThenServiceFactoryResponse +impl ThenServiceFactoryResponse where - A: ServiceFactory, + A: ServiceFactory, B: ServiceFactory< + Result, Config = A::Config, - Request = Result, Error = A::Error, InitError = A::InitError, >, @@ -200,17 +198,17 @@ where } } -impl Future for ThenServiceFactoryResponse +impl Future for ThenServiceFactoryResponse where - A: ServiceFactory, + A: ServiceFactory, B: ServiceFactory< + Result, Config = A::Config, - Request = Result, Error = A::Error, InitError = A::InitError, >, { - type Output = Result, A::InitError>; + type Output = Result, A::InitError>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); @@ -249,8 +247,7 @@ mod tests { #[derive(Clone)] struct Srv1(Rc>); - impl Service for Srv1 { - type Request = Result<&'static str, &'static str>; + impl Service> for Srv1 { type Response = &'static str; type Error = (); type Future = Ready>; @@ -270,8 +267,7 @@ mod tests { struct Srv2(Rc>); - impl Service for Srv2 { - type Request = Result<&'static str, ()>; + impl Service> for Srv2 { type Response = (&'static str, &'static str); type Error = (); type Future = Ready>; diff --git a/actix-service/src/transform.rs b/actix-service/src/transform.rs index 0ebfc5b7..7f0634e7 100644 --- a/actix-service/src/transform.rs +++ b/actix-service/src/transform.rs @@ -1,18 +1,18 @@ -use std::future::Future; use std::pin::Pin; use std::rc::Rc; use std::sync::Arc; use std::task::{Context, Poll}; +use std::{future::Future, marker::PhantomData}; use crate::transform_err::TransformMapInitErr; use crate::{IntoServiceFactory, Service, ServiceFactory}; /// Apply transform to a service. -pub fn apply(t: T, factory: U) -> ApplyTransform +pub fn apply(t: T, factory: U) -> ApplyTransform where - S: ServiceFactory, - T: Transform, - U: IntoServiceFactory, + S: ServiceFactory, + T: Transform, + U: IntoServiceFactory, { ApplyTransform::new(t, factory.into_factory()) } @@ -89,10 +89,7 @@ where /// } /// } /// ``` -pub trait Transform { - /// Requests handled by the service. - type Request; - +pub trait Transform { /// Responses given by the service. type Response; @@ -100,11 +97,7 @@ pub trait Transform { type Error; /// The `TransformService` value created by this factory - type Transform: Service< - Request = Self::Request, - Response = Self::Response, - Error = Self::Error, - >; + type Transform: Service; /// Errors produced while building a transform service. type InitError; @@ -117,7 +110,7 @@ pub trait Transform { /// Map this transform's factory error to a different error, /// returning a new transform service factory. - fn map_init_err(self, f: F) -> TransformMapInitErr + fn map_init_err(self, f: F) -> TransformMapInitErr where Self: Sized, F: Fn(Self::InitError) -> E + Clone, @@ -126,11 +119,10 @@ pub trait Transform { } } -impl Transform for Rc +impl Transform for Rc where - T: Transform, + T: Transform, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; @@ -142,11 +134,10 @@ where } } -impl Transform for Arc +impl Transform for Arc where - T: Transform, + T: Transform, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type InitError = T::InitError; @@ -159,38 +150,37 @@ where } /// `Apply` transform to new service -pub struct ApplyTransform(Rc<(T, S)>); +pub struct ApplyTransform(Rc<(T, S)>, PhantomData); -impl ApplyTransform +impl ApplyTransform where - S: ServiceFactory, - T: Transform, + S: ServiceFactory, + T: Transform, { /// Create new `ApplyTransform` new service instance fn new(t: T, service: S) -> Self { - Self(Rc::new((t, service))) + Self(Rc::new((t, service)), PhantomData) } } -impl Clone for ApplyTransform { +impl Clone for ApplyTransform { fn clone(&self) -> Self { - ApplyTransform(self.0.clone()) + ApplyTransform(self.0.clone(), PhantomData) } } -impl ServiceFactory for ApplyTransform +impl ServiceFactory for ApplyTransform where - S: ServiceFactory, - T: Transform, + S: ServiceFactory, + T: Transform, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type Config = S::Config; type Service = T::Transform; type InitError = T::InitError; - type Future = ApplyTransformFuture; + type Future = ApplyTransformFuture; fn new_service(&self, cfg: S::Config) -> Self::Future { ApplyTransformFuture { @@ -201,30 +191,30 @@ where } #[pin_project::pin_project] -pub struct ApplyTransformFuture +pub struct ApplyTransformFuture where - S: ServiceFactory, - T: Transform, + S: ServiceFactory, + T: Transform, { store: Rc<(T, S)>, #[pin] - state: ApplyTransformFutureState, + state: ApplyTransformFutureState, } #[pin_project::pin_project(project = ApplyTransformFutureStateProj)] -pub enum ApplyTransformFutureState +pub enum ApplyTransformFutureState where - S: ServiceFactory, - T: Transform, + S: ServiceFactory, + T: Transform, { A(#[pin] S::Future), B(#[pin] T::Future), } -impl Future for ApplyTransformFuture +impl Future for ApplyTransformFuture where - S: ServiceFactory, - T: Transform, + S: ServiceFactory, + T: Transform, { type Output = Result; diff --git a/actix-service/src/transform_err.rs b/actix-service/src/transform_err.rs index 9d306f0c..1d1b9576 100644 --- a/actix-service/src/transform_err.rs +++ b/actix-service/src/transform_err.rs @@ -9,65 +9,64 @@ use super::Transform; /// transform's init error. /// /// This is created by the `Transform::map_init_err` method. -pub struct TransformMapInitErr { - t: T, - f: F, - e: PhantomData<(S, E)>, +pub struct TransformMapInitErr { + transform: T, + mapper: F, + _phantom: PhantomData<(S, Req, E)>, } -impl TransformMapInitErr { +impl TransformMapInitErr { pub(crate) fn new(t: T, f: F) -> Self where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E, { Self { - t, - f, - e: PhantomData, + transform: t, + mapper: f, + _phantom: PhantomData, } } } -impl Clone for TransformMapInitErr +impl Clone for TransformMapInitErr where T: Clone, F: Clone, { fn clone(&self) -> Self { Self { - t: self.t.clone(), - f: self.f.clone(), - e: PhantomData, + transform: self.transform.clone(), + mapper: self.mapper.clone(), + _phantom: PhantomData, } } } -impl Transform for TransformMapInitErr +impl Transform for TransformMapInitErr where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E + Clone, { - type Request = T::Request; type Response = T::Response; type Error = T::Error; type Transform = T::Transform; type InitError = E; - type Future = TransformMapInitErrFuture; + type Future = TransformMapInitErrFuture; fn new_transform(&self, service: S) -> Self::Future { TransformMapInitErrFuture { - fut: self.t.new_transform(service), - f: self.f.clone(), + fut: self.transform.new_transform(service), + f: self.mapper.clone(), } } } #[pin_project::pin_project] -pub struct TransformMapInitErrFuture +pub struct TransformMapInitErrFuture where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E, { #[pin] @@ -75,9 +74,9 @@ where f: F, } -impl Future for TransformMapInitErrFuture +impl Future for TransformMapInitErrFuture where - T: Transform, + T: Transform, F: Fn(T::InitError) -> E + Clone, { type Output = Result;