From 1af149b9e61090556b3df98e21e11930dc9c337d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sat, 9 Feb 2019 20:27:39 -0800 Subject: [PATCH] remove Clone constraint from handler service --- src/h1/dispatcher.rs | 15 ++++++++------- src/h1/service.rs | 22 +++++++++++----------- src/h2/dispatcher.rs | 7 ++++--- src/h2/service.rs | 35 ++++++++++++++++++++--------------- src/request.rs | 9 --------- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/src/h1/dispatcher.rs b/src/h1/dispatcher.rs index 667e674c5..06d4312a4 100644 --- a/src/h1/dispatcher.rs +++ b/src/h1/dispatcher.rs @@ -5,6 +5,7 @@ use std::time::Instant; use actix_codec::{AsyncRead, AsyncWrite, Framed}; use actix_service::Service; +use actix_utils::cloneable::CloneableService; use bitflags::bitflags; use futures::{try_ready, Async, Future, Poll, Sink, Stream}; use log::{debug, error, trace}; @@ -35,18 +36,18 @@ bitflags! { } /// Dispatcher for HTTP/1.1 protocol -pub struct Dispatcher +pub struct Dispatcher where S::Error: Debug, { inner: Option>, } -struct InnerDispatcher +struct InnerDispatcher where S::Error: Debug, { - service: S, + service: CloneableService, flags: Flags, framed: Framed, error: Option>, @@ -85,13 +86,13 @@ impl State { impl Dispatcher where T: AsyncRead + AsyncWrite, - S: Service, + S: Service + 'static, S::Error: Debug, S::Response: Into>, B: MessageBody, { /// Create http/1 dispatcher. - pub fn new(stream: T, config: ServiceConfig, service: S) -> Self { + pub fn new(stream: T, config: ServiceConfig, service: CloneableService) -> Self { Dispatcher::with_timeout(stream, config, None, service) } @@ -100,7 +101,7 @@ where stream: T, config: ServiceConfig, timeout: Option, - service: S, + service: CloneableService, ) -> Self { let keepalive = config.keep_alive_enabled(); let flags = if keepalive { @@ -140,7 +141,7 @@ where impl InnerDispatcher where T: AsyncRead + AsyncWrite, - S: Service, + S: Service + 'static, S::Error: Debug, S::Response: Into>, B: MessageBody, diff --git a/src/h1/service.rs b/src/h1/service.rs index 02a4b15d6..169a80ebc 100644 --- a/src/h1/service.rs +++ b/src/h1/service.rs @@ -4,6 +4,7 @@ use std::net; use actix_codec::{AsyncRead, AsyncWrite, Framed}; use actix_service::{IntoNewService, NewService, Service}; +use actix_utils::cloneable::CloneableService; use futures::future::{ok, FutureResult}; use futures::{try_ready, Async, Future, Poll, Stream}; use log::error; @@ -28,10 +29,10 @@ pub struct H1Service { impl H1Service where - S: NewService> + Clone, - S::Service: Clone, + S: NewService>, S::Error: Debug, S::Response: Into>, + S::Service: 'static, B: MessageBody, { /// Create new `HttpService` instance. @@ -54,10 +55,10 @@ where impl NewService for H1Service where T: AsyncRead + AsyncWrite, - S: NewService + Clone, - S::Service: Clone, + S: NewService, S::Error: Debug, S::Response: Into>, + S::Service: 'static, B: MessageBody, { type Request = T; @@ -93,7 +94,6 @@ pub struct H1ServiceBuilder { impl H1ServiceBuilder where S: NewService, - S::Service: Clone, S::Error: Debug, { /// Create instance of `ServiceConfigBuilder` @@ -217,7 +217,7 @@ impl Future for H1ServiceResponse where T: AsyncRead + AsyncWrite, S: NewService, - S::Service: Clone, + S::Service: 'static, S::Error: Debug, S::Response: Into>, B: MessageBody, @@ -235,22 +235,22 @@ where } /// `Service` implementation for HTTP1 transport -pub struct H1ServiceHandler { - srv: S, +pub struct H1ServiceHandler { + srv: CloneableService, cfg: ServiceConfig, _t: PhantomData<(T, B)>, } impl H1ServiceHandler where - S: Service + Clone, + S: Service, S::Error: Debug, S::Response: Into>, B: MessageBody, { fn new(cfg: ServiceConfig, srv: S) -> H1ServiceHandler { H1ServiceHandler { - srv, + srv: CloneableService::new(srv), cfg, _t: PhantomData, } @@ -260,7 +260,7 @@ where impl Service for H1ServiceHandler where T: AsyncRead + AsyncWrite, - S: Service + Clone, + S: Service, S::Error: Debug, S::Response: Into>, B: MessageBody, diff --git a/src/h2/dispatcher.rs b/src/h2/dispatcher.rs index b7339fa1d..5a8c4b855 100644 --- a/src/h2/dispatcher.rs +++ b/src/h2/dispatcher.rs @@ -5,6 +5,7 @@ use std::{fmt, mem}; use actix_codec::{AsyncRead, AsyncWrite}; use actix_service::Service; +use actix_utils::cloneable::CloneableService; use bitflags::bitflags; use bytes::{Bytes, BytesMut}; use futures::{try_ready, Async, Future, Poll, Sink, Stream}; @@ -37,9 +38,9 @@ bitflags! { } /// Dispatcher for HTTP/2 protocol -pub struct Dispatcher { +pub struct Dispatcher { flags: Flags, - service: S, + service: CloneableService, connection: Connection, config: ServiceConfig, ka_expire: Instant, @@ -56,7 +57,7 @@ where B: MessageBody + 'static, { pub fn new( - service: S, + service: CloneableService, connection: Connection, config: ServiceConfig, timeout: Option, diff --git a/src/h2/service.rs b/src/h2/service.rs index a1526375f..16b7e4956 100644 --- a/src/h2/service.rs +++ b/src/h2/service.rs @@ -4,6 +4,7 @@ use std::{io, net}; use actix_codec::{AsyncRead, AsyncWrite, Framed}; use actix_service::{IntoNewService, NewService, Service}; +use actix_utils::cloneable::CloneableService; use bytes::Bytes; use futures::future::{ok, FutureResult}; use futures::{try_ready, Async, Future, Poll, Stream}; @@ -30,8 +31,8 @@ pub struct H2Service { impl H2Service where - S: NewService> + Clone, - S::Service: Clone + 'static, + S: NewService>, + S::Service: 'static, S::Error: Into + Debug + 'static, S::Response: Into>, B: MessageBody + 'static, @@ -56,8 +57,8 @@ where impl NewService for H2Service where T: AsyncRead + AsyncWrite, - S: NewService> + Clone, - S::Service: Clone + 'static, + S: NewService>, + S::Service: 'static, S::Error: Into + Debug, S::Response: Into>, B: MessageBody + 'static, @@ -95,7 +96,7 @@ pub struct H2ServiceBuilder { impl H2ServiceBuilder where S: NewService>, - S::Service: Clone + 'static, + S::Service: 'static, S::Error: Into + Debug + 'static, { /// Create instance of `H2ServiceBuilder` @@ -238,7 +239,7 @@ impl Future for H2ServiceResponse where T: AsyncRead + AsyncWrite, S: NewService>, - S::Service: Clone + 'static, + S::Service: 'static, S::Response: Into>, S::Error: Into + Debug, B: MessageBody + 'static, @@ -256,23 +257,23 @@ where } /// `Service` implementation for http/2 transport -pub struct H2ServiceHandler { - srv: S, +pub struct H2ServiceHandler { + srv: CloneableService, cfg: ServiceConfig, _t: PhantomData<(T, B)>, } impl H2ServiceHandler where - S: Service> + Clone + 'static, + S: Service> + 'static, S::Error: Into + Debug, S::Response: Into>, B: MessageBody + 'static, { fn new(cfg: ServiceConfig, srv: S) -> H2ServiceHandler { H2ServiceHandler { - srv, cfg, + srv: CloneableService::new(srv), _t: PhantomData, } } @@ -281,7 +282,7 @@ where impl Service for H2ServiceHandler where T: AsyncRead + AsyncWrite, - S: Service> + Clone + 'static, + S: Service> + 'static, S::Error: Into + Debug, S::Response: Into>, B: MessageBody + 'static, @@ -309,15 +310,19 @@ where } } -enum State { +enum State { Incoming(Dispatcher), - Handshake(Option, Option, Handshake), + Handshake( + Option>, + Option, + Handshake, + ), } pub struct H2ServiceHandlerResponse where T: AsyncRead + AsyncWrite, - S: Service> + Clone + 'static, + S: Service> + 'static, S::Error: Into + Debug, S::Response: Into>, B: MessageBody + 'static, @@ -328,7 +333,7 @@ where impl Future for H2ServiceHandlerResponse where T: AsyncRead + AsyncWrite, - S: Service> + Clone, + S: Service> + 'static, S::Error: Into + Debug, S::Response: Into>, B: MessageBody, diff --git a/src/request.rs b/src/request.rs index 0064de4e0..b9c35c7ac 100644 --- a/src/request.rs +++ b/src/request.rs @@ -153,15 +153,6 @@ impl Request { } self.head().method == Method::CONNECT } - - // #[doc(hidden)] - // /// Note: this method should be called only as part of clone operation - // /// of wrapper type. - // pub fn clone_request(&self) -> Self { - // Request { - // inner: self.inner.clone(), - // } - // } } impl fmt::Debug for Request {