From ac62e2dbf9f667827f3fcb25f5e076172b6bb410 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 9 Mar 2019 07:27:35 -0800 Subject: [PATCH] revert generic request in actix-utils --- actix-utils/src/cloneable.rs | 11 ++--- actix-utils/src/either.rs | 62 +++++++++++++++++++-------- actix-utils/src/framed.rs | 48 +++++++++++---------- actix-utils/src/inflight.rs | 19 +++++---- actix-utils/src/keepalive.rs | 6 ++- actix-utils/src/order.rs | 83 ++++++++++++++++++++---------------- actix-utils/src/stream.rs | 36 +++++++++------- actix-utils/src/time.rs | 4 +- actix-utils/src/timeout.rs | 23 +++++----- 9 files changed, 173 insertions(+), 119 deletions(-) diff --git a/actix-utils/src/cloneable.rs b/actix-utils/src/cloneable.rs index 16a8a2e3..933e6a1f 100644 --- a/actix-utils/src/cloneable.rs +++ b/actix-utils/src/cloneable.rs @@ -13,9 +13,9 @@ pub struct CloneableService { } impl CloneableService { - pub fn new(service: T) -> Self + pub fn new(service: T) -> Self where - T: Service, + T: Service, { Self { service: Cell::new(service), @@ -33,10 +33,11 @@ impl Clone for CloneableService { } } -impl Service for CloneableService +impl Service for CloneableService where - T: Service + 'static, + T: Service + 'static, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -45,7 +46,7 @@ where self.service.get_mut().poll_ready() } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: T::Request) -> Self::Future { self.service.get_mut().call(req) } } diff --git a/actix-utils/src/either.rs b/actix-utils/src/either.rs index 29bb5c7f..885cd095 100644 --- a/actix-utils/src/either.rs +++ b/actix-utils/src/either.rs @@ -21,11 +21,12 @@ impl Clone for EitherService { } } -impl Service for EitherService +impl Service for EitherService where - A: Service, - B: Service, + A: Service, + B: Service, { + type Request = A::Request; type Response = A::Response; type Error = A::Error; type Future = future::Either; @@ -37,7 +38,7 @@ where } } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: A::Request) -> Self::Future { match self { EitherService::A(ref mut inner) => future::Either::A(inner.call(req)), EitherService::B(ref mut inner) => future::Either::B(inner.call(req)), @@ -52,33 +53,52 @@ pub enum Either { } impl Either { - pub fn new_a(srv: A) -> Self + pub fn new_a(srv: A) -> Self where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + C, + Request = A::Request, + Response = A::Response, + Error = A::Error, + InitError = A::InitError, + >, { Either::A(srv) } - pub fn new_b(srv: B) -> Self + pub fn new_b(srv: B) -> Self where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + C, + Request = A::Request, + Response = A::Response, + Error = A::Error, + InitError = A::InitError, + >, { Either::B(srv) } } -impl NewService for Either +impl NewService for Either where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + C, + Request = A::Request, + Response = A::Response, + Error = A::Error, + InitError = A::InitError, + >, { + type Request = A::Request; type Response = A::Response; type Error = A::Error; type InitError = A::InitError; type Service = EitherService; - type Future = EitherNewService; + type Future = EitherNewService; fn new_service(&self, cfg: &C) -> Self::Future { match self { @@ -98,15 +118,21 @@ impl Clone for Either { } #[doc(hidden)] -pub enum EitherNewService, B: NewService, R, C> { +pub enum EitherNewService, B: NewService, C> { A(::Future), B(::Future), } -impl Future for EitherNewService +impl Future for EitherNewService where - A: NewService, - B: NewService, + A: NewService, + B: NewService< + C, + Request = A::Request, + Response = A::Response, + Error = A::Error, + InitError = A::InitError, + >, { type Item = EitherService; type Error = A::InitError; diff --git a/actix-utils/src/framed.rs b/actix-utils/src/framed.rs index 7b086248..11d4b0e1 100644 --- a/actix-utils/src/framed.rs +++ b/actix-utils/src/framed.rs @@ -23,15 +23,15 @@ pub struct FramedNewService { impl FramedNewService where C: Clone, - S: NewService, C, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new, C>>(factory: F1) -> Self { + pub fn new>(factory: F1) -> Self { Self { factory: factory.into_new_service(), _t: PhantomData, @@ -51,17 +51,18 @@ where } } -impl NewService, C> for FramedNewService +impl NewService for FramedNewService where C: Clone, - S: NewService, C, Response = Response> + Clone, + S: NewService, Response = Response> + Clone, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, { + type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type InitError = S::InitError; @@ -97,17 +98,18 @@ where } } -impl Service> for FramedService +impl Service for FramedService where - S: NewService, C, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, ::Error: std::fmt::Debug, C: Clone, { + type Request = Framed; type Response = FramedTransport; type Error = S::InitError; type Future = FramedServiceResponseFuture; @@ -127,9 +129,9 @@ where #[doc(hidden)] pub struct FramedServiceResponseFuture where - S: NewService, C, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -141,9 +143,9 @@ where impl Future for FramedServiceResponseFuture where - S: NewService, C, Response = Response>, + S: NewService, Response = Response>, S::Error: 'static, - >>::Future: 'static, + ::Future: 'static, T: AsyncRead + AsyncWrite, U: Decoder + Encoder, ::Item: 'static, @@ -180,7 +182,7 @@ impl From for FramedTransportError { /// and pass then to the service. pub struct FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -194,7 +196,7 @@ where inner: Cell::Item, S::Error>>, } -enum TransportState>, U: Encoder + Decoder> { +enum TransportState { Processing, Error(FramedTransportError), FramedError(FramedTransportError), @@ -208,7 +210,7 @@ struct FramedTransportInner { impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -300,7 +302,7 @@ where impl FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -308,7 +310,7 @@ where ::Item: 'static, ::Error: std::fmt::Debug, { - pub fn new>>(framed: Framed, service: F) -> Self { + pub fn new>(framed: Framed, service: F) -> Self { FramedTransport { framed, service: service.into_service(), @@ -346,7 +348,7 @@ where impl Future for FramedTransport where - S: Service, Response = Response>, + S: Service, Response = Response>, S::Error: 'static, S::Future: 'static, T: AsyncRead + AsyncWrite, @@ -406,19 +408,20 @@ where } } -impl NewService for IntoFramed +impl NewService for IntoFramed where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { + type Request = T; type Response = Framed; type Error = (); type InitError = (); type Service = IntoFramedService; type Future = FutureResult; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: &C) -> Self::Future { ok(IntoFramedService { factory: self.factory.clone(), _t: PhantomData, @@ -436,12 +439,13 @@ where _t: PhantomData<(T,)>, } -impl Service for IntoFramedService +impl Service for IntoFramedService where T: AsyncRead + AsyncWrite, F: Fn() -> U + Send + Clone + 'static, U: Encoder + Decoder, { + type Request = T; type Response = Framed; type Error = (); type Future = FutureResult; diff --git a/actix-utils/src/inflight.rs b/actix-utils/src/inflight.rs index 1462e32a..41955f35 100644 --- a/actix-utils/src/inflight.rs +++ b/actix-utils/src/inflight.rs @@ -24,7 +24,8 @@ impl Default for InFlight { } } -impl, R> Transform for InFlight { +impl Transform for InFlight { + type Request = S::Request; type Response = S::Response; type Error = S::Error; type InitError = Void; @@ -50,13 +51,14 @@ impl InFlightService { } } -impl Service for InFlightService +impl Service for InFlightService where - T: Service, + T: Service, { + type Request = T::Request; type Response = T::Response; type Error = T::Error; - type Future = InFlightServiceResponse; + type Future = InFlightServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready()?; @@ -69,7 +71,7 @@ where } } - fn call(&mut self, req: R) -> Self::Future { + fn call(&mut self, req: T::Request) -> Self::Future { InFlightServiceResponse { fut: self.service.call(req), _guard: self.count.get(), @@ -78,12 +80,12 @@ where } #[doc(hidden)] -pub struct InFlightServiceResponse, R> { +pub struct InFlightServiceResponse { fut: T::Future, _guard: CounterGuard, } -impl, R> Future for InFlightServiceResponse { +impl Future for InFlightServiceResponse { type Item = T::Response; type Error = T::Error; @@ -105,7 +107,8 @@ mod tests { struct SleepService(Duration); - impl Service<()> for SleepService { + impl Service for SleepService { + type Request = (); type Response = (); type Error = (); type Future = Box>; diff --git a/actix-utils/src/keepalive.rs b/actix-utils/src/keepalive.rs index 89929db7..81305423 100644 --- a/actix-utils/src/keepalive.rs +++ b/actix-utils/src/keepalive.rs @@ -43,10 +43,11 @@ where } } -impl NewService for KeepAlive +impl NewService<()> for KeepAlive where F: Fn() -> E + Clone, { + type Request = R; type Response = R; type Error = E; type InitError = Void; @@ -88,10 +89,11 @@ where } } -impl Service for KeepAliveService +impl Service for KeepAliveService where F: Fn() -> E, { + type Request = R; type Response = R; type Error = E; type Future = FutureResult; diff --git a/actix-utils/src/order.rs b/actix-utils/src/order.rs index 8b0e6b6e..b267a0b4 100644 --- a/actix-utils/src/order.rs +++ b/actix-utils/src/order.rs @@ -52,39 +52,46 @@ pub struct InOrder { _t: PhantomData, } -impl InOrder { - pub fn new() -> Self - where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, - { - Self { _t: PhantomData } - } - - pub fn service(service: S) -> InOrderService - where - S: Service, - S::Response: 'static, - S::Future: 'static, - S::Error: 'static, - { - InOrderService::new(service) - } -} - -impl Transform for InOrder +impl InOrder where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, { + pub fn new() -> Self { + Self { _t: PhantomData } + } + + pub fn service(service: S) -> InOrderService { + InOrderService::new(service) + } +} + +impl Default for InOrder +where + S: Service, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, +{ + fn default() -> Self { + Self::new() + } +} + +impl Transform for InOrder +where + S: Service, + S::Response: 'static, + S::Future: 'static, + S::Error: 'static, +{ + type Request = S::Request; type Response = S::Response; type Error = InOrderError; type InitError = Void; - type Transform = InOrderService; + type Transform = InOrderService; type Future = FutureResult; fn new_transform(&self, service: S) -> Self::Future { @@ -92,15 +99,15 @@ where } } -pub struct InOrderService, R> { +pub struct InOrderService { service: S, task: Rc, acks: VecDeque>, } -impl InOrderService +impl InOrderService where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, @@ -114,16 +121,17 @@ where } } -impl Service for InOrderService +impl Service for InOrderService where - S: Service, + S: Service, S::Response: 'static, S::Future: 'static, S::Error: 'static, { + type Request = S::Request; type Response = S::Response; type Error = InOrderError; - type Future = InOrderServiceResponse; + type Future = InOrderServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { // poll_ready could be called from different task @@ -148,7 +156,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, request: R) -> Self::Future { + fn call(&mut self, request: S::Request) -> Self::Future { let (tx1, rx1) = oneshot::channel(); let (tx2, rx2) = oneshot::channel(); self.acks.push_back(Record { rx: rx1, tx: tx2 }); @@ -165,11 +173,11 @@ where } #[doc(hidden)] -pub struct InOrderServiceResponse, R> { +pub struct InOrderServiceResponse { rx: oneshot::Receiver>, } -impl, R> Future for InOrderServiceResponse { +impl Future for InOrderServiceResponse { type Item = S::Response; type Error = InOrderError; @@ -196,7 +204,8 @@ mod tests { struct Srv; - impl Service> for Srv { + impl Service for Srv { + type Request = oneshot::Receiver; type Response = usize; type Error = (); type Future = Box>; @@ -210,11 +219,11 @@ mod tests { } } - struct SrvPoll>> { + struct SrvPoll { s: S, } - impl>> Future for SrvPoll { + impl Future for SrvPoll { type Item = (); type Error = (); diff --git a/actix-utils/src/stream.rs b/actix-utils/src/stream.rs index b82a66e8..7274fa56 100644 --- a/actix-utils/src/stream.rs +++ b/actix-utils/src/stream.rs @@ -38,12 +38,12 @@ impl StreamNewService where C: Clone, S: IntoStream, - T: NewService, C, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, { - pub fn new, C>>(factory: F) -> Self { + pub fn new>(factory: F) -> Self { Self { factory: Rc::new(factory.into_new_service()), _t: PhantomData, @@ -60,15 +60,16 @@ impl Clone for StreamNewService { } } -impl NewService for StreamNewService +impl NewService for StreamNewService where C: Clone, S: IntoStream + 'static, - T: NewService, C, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, { + type Request = S; type Response = (); type Error = E; type InitError = E; @@ -90,15 +91,16 @@ pub struct StreamService { _t: PhantomData<(S, E)>, } -impl Service for StreamService +impl Service for StreamService where S: IntoStream + 'static, - T: NewService, C, Response = (), Error = E, InitError = E>, + T: NewService, Response = (), Error = E, InitError = E>, T::Future: 'static, T::Service: 'static, - >>::Future: 'static, + ::Future: 'static, C: Clone, { + type Request = S; type Response = (); type Error = E; type Future = Box>; @@ -119,7 +121,7 @@ where pub struct StreamDispatcher where S: IntoStream + 'static, - T: Service, Response = ()> + 'static, + T: Service, Response = ()> + 'static, T::Future: 'static, { stream: S, @@ -131,13 +133,13 @@ where impl StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { pub fn new(stream: F1, service: F2) -> Self where F1: IntoStream, - F2: IntoService>, + F2: IntoService, { let (err_tx, err_rx) = mpsc::unbounded(); StreamDispatcher { @@ -152,7 +154,7 @@ where impl Future for StreamDispatcher where S: Stream, - T: Service, Response = ()>, + T: Service, Response = ()>, T::Future: 'static, { type Item = (); @@ -230,14 +232,15 @@ impl Clone for TakeItem { } } -impl NewService for TakeItem { +impl NewService for TakeItem { + type Request = T; type Response = (Option, T); type Error = T::Error; type InitError = (); type Service = TakeItemService; type Future = FutureResult; - fn new_service(&self, _: &()) -> Self::Future { + fn new_service(&self, _: &C) -> Self::Future { ok(TakeItemService { _t: PhantomData }) } } @@ -253,7 +256,8 @@ impl Clone for TakeItemService { } } -impl Service for TakeItemService { +impl Service for TakeItemService { + type Request = T; type Response = (Option, T); type Error = T::Error; type Future = TakeItemServiceResponse; diff --git a/actix-utils/src/time.rs b/actix-utils/src/time.rs index 57e09846..c30052d0 100644 --- a/actix-utils/src/time.rs +++ b/actix-utils/src/time.rs @@ -42,6 +42,7 @@ impl Default for LowResTime { } impl NewService<()> for LowResTime { + type Request = (); type Response = Instant; type Error = Void; type InitError = Void; @@ -87,7 +88,8 @@ impl LowResTimeService { } } -impl Service<()> for LowResTimeService { +impl Service for LowResTimeService { + type Request = (); type Response = Instant; type Error = Void; type Future = FutureResult; diff --git a/actix-utils/src/timeout.rs b/actix-utils/src/timeout.rs index 10b90aa5..85d69731 100644 --- a/actix-utils/src/timeout.rs +++ b/actix-utils/src/timeout.rs @@ -80,10 +80,11 @@ impl Clone for Timeout { } } -impl Transform for Timeout +impl Transform for Timeout where - S: Service, + S: Service, { + type Request = S::Request; type Response = S::Response; type Error = TimeoutError; type InitError = E; @@ -111,19 +112,20 @@ impl TimeoutService { } } -impl Service for TimeoutService +impl Service for TimeoutService where - S: Service, + S: Service, { + type Request = S::Request; type Response = S::Response; type Error = TimeoutError; - type Future = TimeoutServiceResponse; + type Future = TimeoutServiceResponse; fn poll_ready(&mut self) -> Poll<(), Self::Error> { self.service.poll_ready().map_err(TimeoutError::Service) } - fn call(&mut self, request: R) -> Self::Future { + fn call(&mut self, request: S::Request) -> Self::Future { TimeoutServiceResponse { fut: self.service.call(request), sleep: Delay::new(clock::now() + self.timeout), @@ -133,14 +135,14 @@ where /// `TimeoutService` response future #[derive(Debug)] -pub struct TimeoutServiceResponse, R> { +pub struct TimeoutServiceResponse { fut: T::Future, sleep: Delay, } -impl Future for TimeoutServiceResponse +impl Future for TimeoutServiceResponse where - T: Service, + T: Service, { type Item = T::Response; type Error = TimeoutError; @@ -175,7 +177,8 @@ mod tests { struct SleepService(Duration); - impl Service<()> for SleepService { + impl Service for SleepService { + type Request = (); type Response = (); type Error = (); type Future = Box>;