From 5003c00efbdeb8ef76b8137a3d604d7ba4c16d4d Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 30 Nov 2018 11:57:57 -0800 Subject: [PATCH] use new Service and NewService traits --- Cargo.toml | 4 +-- rustfmt.toml | 2 +- src/client/connector.rs | 56 ++++++++++++---------------------------- src/client/pipeline.rs | 2 +- src/client/pool.rs | 11 ++++---- src/client/request.rs | 2 +- src/h1/dispatcher.rs | 14 +++++----- src/h1/service.rs | 34 +++++++++++------------- src/service.rs | 16 +++++------- src/test.rs | 9 +++---- src/ws/client/service.rs | 13 +++++----- src/ws/service.rs | 8 +++--- src/ws/transport.rs | 10 +++---- 13 files changed, 73 insertions(+), 108 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b2c7c084..b1dd69ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,8 +45,8 @@ rust-tls = ["rustls", "actix-net/rust-tls"] [dependencies] actix = "0.7.5" -actix-net = "0.2.3" -#actix-net = { git="https://github.com/actix/actix-net.git" } +#actix-net = "0.3.0" +actix-net = { git="https://github.com/actix/actix-net.git" } base64 = "0.9" bitflags = "1.0" diff --git a/rustfmt.toml b/rustfmt.toml index 4fff285e..5fcaaca0 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,5 +1,5 @@ max_width = 89 reorder_imports = true #wrap_comments = true -fn_args_density = "Compressed" +#fn_args_density = "Compressed" #use_small_heuristics = false diff --git a/src/client/connector.rs b/src/client/connector.rs index 42cba9de..3729ce39 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -134,11 +134,8 @@ impl Connector { /// Finish configuration process and create connector service. pub fn service( self, - ) -> impl Service< - Request = Connect, - Response = impl Connection, - Error = ConnectorError, - > + Clone { + ) -> impl Service + Clone + { #[cfg(not(feature = "ssl"))] { let connector = TimeoutService::new( @@ -216,7 +213,7 @@ mod connect_impl { pub(crate) struct InnerConnector where Io: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { pub(crate) tcp_pool: ConnectionPool, } @@ -224,8 +221,7 @@ mod connect_impl { impl Clone for InnerConnector where Io: AsyncRead + AsyncWrite + 'static, - T: Service - + Clone, + T: Service + Clone, { fn clone(&self) -> Self { InnerConnector { @@ -234,16 +230,15 @@ mod connect_impl { } } - impl Service for InnerConnector + impl Service for InnerConnector where Io: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { - type Request = Connect; type Response = IoConnection; type Error = ConnectorError; type Future = Either< - as Service>::Future, + as Service>::Future, FutureResult, ConnectorError>, >; @@ -251,7 +246,7 @@ mod connect_impl { self.tcp_pool.poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Connect) -> Self::Future { if req.is_secure() { Either::B(err(ConnectorError::SslIsNotSupported)) } else if let Err(e) = req.validate() { @@ -295,16 +290,8 @@ mod connect_impl { where Io1: AsyncRead + AsyncWrite + 'static, Io2: AsyncRead + AsyncWrite + 'static, - T1: Service< - Request = Connect, - Response = (Connect, Io1), - Error = ConnectorError, - > + Clone, - T2: Service< - Request = Connect, - Response = (Connect, Io2), - Error = ConnectorError, - > + Clone, + T1: Service + Clone, + T2: Service + Clone, { fn clone(&self) -> Self { InnerConnector { @@ -318,18 +305,9 @@ mod connect_impl { where Io1: AsyncRead + AsyncWrite + 'static, Io2: AsyncRead + AsyncWrite + 'static, - T1: Service< - Request = Connect, - Response = (Connect, Io1), - Error = ConnectorError, - >, - T2: Service< - Request = Connect, - Response = (Connect, Io2), - Error = ConnectorError, - >, + T1: Service, + T2: Service, { - type Request = Connect; type Response = IoEither, IoConnection>; type Error = ConnectorError; type Future = Either< @@ -344,7 +322,7 @@ mod connect_impl { self.tcp_pool.poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Connect) -> Self::Future { if let Err(e) = req.validate() { Either::A(err(e)) } else if req.is_secure() { @@ -364,7 +342,7 @@ mod connect_impl { pub(crate) struct InnerConnectorResponseA where Io1: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { fut: as Service>::Future, _t: PhantomData, @@ -372,7 +350,7 @@ mod connect_impl { impl Future for InnerConnectorResponseA where - T: Service, + T: Service, Io1: AsyncRead + AsyncWrite + 'static, Io2: AsyncRead + AsyncWrite + 'static, { @@ -390,7 +368,7 @@ mod connect_impl { pub(crate) struct InnerConnectorResponseB where Io2: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { fut: as Service>::Future, _t: PhantomData, @@ -398,7 +376,7 @@ mod connect_impl { impl Future for InnerConnectorResponseB where - T: Service, + T: Service, Io1: AsyncRead + AsyncWrite + 'static, Io2: AsyncRead + AsyncWrite + 'static, { diff --git a/src/client/pipeline.rs b/src/client/pipeline.rs index e1d8421e..e7526550 100644 --- a/src/client/pipeline.rs +++ b/src/client/pipeline.rs @@ -21,7 +21,7 @@ pub(crate) fn send_request( connector: &mut T, ) -> impl Future where - T: Service, + T: Service, B: MessageBody, I: Connection, { diff --git a/src/client/pool.rs b/src/client/pool.rs index 44008f34..decf8019 100644 --- a/src/client/pool.rs +++ b/src/client/pool.rs @@ -47,7 +47,7 @@ pub(crate) struct ConnectionPool( impl ConnectionPool where Io: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { pub(crate) fn new( connector: T, @@ -83,12 +83,11 @@ where } } -impl Service for ConnectionPool +impl Service for ConnectionPool where Io: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, { - type Request = Connect; type Response = IoConnection; type Error = ConnectorError; type Future = Either< @@ -100,7 +99,7 @@ where self.0.poll_ready() } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Connect) -> Self::Future { let key = req.key(); // acquire connection @@ -456,7 +455,7 @@ where impl Future for ConnectorPoolSupport where Io: AsyncRead + AsyncWrite + 'static, - T: Service, + T: Service, T::Future: 'static, { type Item = (); diff --git a/src/client/request.rs b/src/client/request.rs index dd29d797..e71c3ffd 100644 --- a/src/client/request.rs +++ b/src/client/request.rs @@ -177,7 +177,7 @@ where connector: &mut T, ) -> impl Future where - T: Service, + T: Service, I: Connection, { pipeline::send_request(self.head, self.body, connector) diff --git a/src/h1/dispatcher.rs b/src/h1/dispatcher.rs index 48c8e710..5e2742aa 100644 --- a/src/h1/dispatcher.rs +++ b/src/h1/dispatcher.rs @@ -36,14 +36,14 @@ bitflags! { } /// Dispatcher for HTTP/1.1 protocol -pub struct Dispatcher +pub struct Dispatcher, B: MessageBody> where S::Error: Debug, { inner: Option>, } -struct InnerDispatcher +struct InnerDispatcher, B: MessageBody> where S::Error: Debug, { @@ -67,13 +67,13 @@ enum DispatcherMessage { Error(Response<()>), } -enum State { +enum State, B: MessageBody> { None, ServiceCall(S::Future), SendPayload(ResponseBody), } -impl State { +impl, B: MessageBody> State { fn is_empty(&self) -> bool { if let State::None = self { true @@ -86,7 +86,7 @@ impl State { impl Dispatcher where T: AsyncRead + AsyncWrite, - S: Service>, + S: Service>, S::Error: Debug, B: MessageBody, { @@ -140,7 +140,7 @@ where impl InnerDispatcher where T: AsyncRead + AsyncWrite, - S: Service>, + S: Service>, S::Error: Debug, B: MessageBody, { @@ -463,7 +463,7 @@ where impl Future for Dispatcher where T: AsyncRead + AsyncWrite, - S: Service>, + S: Service>, S::Error: Debug, B: MessageBody, { diff --git a/src/h1/service.rs b/src/h1/service.rs index 0f0452ee..e21d0fb6 100644 --- a/src/h1/service.rs +++ b/src/h1/service.rs @@ -27,13 +27,13 @@ pub struct H1Service { impl H1Service where - S: NewService> + Clone, + S: NewService> + Clone, S::Service: Clone, S::Error: Debug, B: MessageBody, { /// Create new `HttpService` instance. - pub fn new>(service: F) -> Self { + pub fn new>(service: F) -> Self { let cfg = ServiceConfig::new(KeepAlive::Timeout(5), 5000, 0); H1Service { @@ -49,15 +49,14 @@ where } } -impl NewService for H1Service +impl NewService for H1Service where T: AsyncRead + AsyncWrite, - S: NewService> + Clone, + S: NewService> + Clone, S::Service: Clone, S::Error: Debug, B: MessageBody, { - type Request = T; type Response = H1ServiceResult; type Error = DispatchError; type InitError = S::InitError; @@ -89,7 +88,7 @@ pub struct H1ServiceBuilder { impl H1ServiceBuilder where - S: NewService, + S: NewService, S::Service: Clone, S::Error: Debug, { @@ -186,7 +185,7 @@ where pub fn finish(self, service: F) -> H1Service where B: MessageBody, - F: IntoNewService, + F: IntoNewService, { let cfg = ServiceConfig::new( self.keep_alive, @@ -202,7 +201,7 @@ where } #[doc(hidden)] -pub struct H1ServiceResponse { +pub struct H1ServiceResponse, B> { fut: S::Future, cfg: Option, _t: PhantomData<(T, B)>, @@ -211,7 +210,7 @@ pub struct H1ServiceResponse { impl Future for H1ServiceResponse where T: AsyncRead + AsyncWrite, - S: NewService>, + S: NewService>, S::Service: Clone, S::Error: Debug, B: MessageBody, @@ -237,7 +236,7 @@ pub struct H1ServiceHandler { impl H1ServiceHandler where - S: Service> + Clone, + S: Service> + Clone, S::Error: Debug, B: MessageBody, { @@ -250,14 +249,13 @@ where } } -impl Service for H1ServiceHandler +impl Service for H1ServiceHandler where T: AsyncRead + AsyncWrite, - S: Service> + Clone, + S: Service> + Clone, S::Error: Debug, B: MessageBody, { - type Request = T; type Response = H1ServiceResult; type Error = DispatchError; type Future = Dispatcher; @@ -266,7 +264,7 @@ where self.srv.poll_ready().map_err(DispatchError::Service) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { Dispatcher::new(req, self.cfg.clone(), self.srv.clone()) } } @@ -290,11 +288,10 @@ where } } -impl NewService for OneRequest +impl NewService for OneRequest where T: AsyncRead + AsyncWrite, { - type Request = T; type Response = (Request, Framed); type Error = ParseError; type InitError = (); @@ -316,11 +313,10 @@ pub struct OneRequestService { _t: PhantomData, } -impl Service for OneRequestService +impl Service for OneRequestService where T: AsyncRead + AsyncWrite, { - type Request = T; type Response = (Request, Framed); type Error = ParseError; type Future = OneRequestServiceResponse; @@ -329,7 +325,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: T) -> Self::Future { OneRequestServiceResponse { framed: Some(Framed::new(req, Codec::new(self.config.clone()))), } diff --git a/src/service.rs b/src/service.rs index 6a31b6bb..aa507acb 100644 --- a/src/service.rs +++ b/src/service.rs @@ -23,12 +23,11 @@ where } } -impl NewService for SendError +impl NewService)>> for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { - type Request = Result)>; type Response = R; type Error = (E, Framed); type InitError = (); @@ -40,12 +39,11 @@ where } } -impl Service for SendError +impl Service)>> for SendError where T: AsyncRead + AsyncWrite, E: ResponseError, { - type Request = Result)>; type Response = R; type Error = (E, Framed); type Future = Either)>, SendErrorFut>; @@ -54,7 +52,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, req: Self::Request) -> Self::Future { + fn call(&mut self, req: Result)>) -> Self::Future { match req { Ok(r) => Either::A(ok(r)), Err((e, framed)) => { @@ -131,12 +129,11 @@ where } } -impl NewService for SendResponse +impl NewService<(Response, Framed)> for SendResponse where T: AsyncRead + AsyncWrite, B: MessageBody, { - type Request = (Response, Framed); type Response = Framed; type Error = Error; type InitError = (); @@ -148,12 +145,11 @@ where } } -impl Service for SendResponse +impl Service<(Response, Framed)> for SendResponse where T: AsyncRead + AsyncWrite, B: MessageBody, { - type Request = (Response, Framed); type Response = Framed; type Error = Error; type Future = SendResponseFut; @@ -162,7 +158,7 @@ where Ok(Async::Ready(())) } - fn call(&mut self, (res, framed): Self::Request) -> Self::Future { + fn call(&mut self, (res, framed): (Response, Framed)) -> Self::Future { let (res, body) = res.replace_body(()); SendResponseFut { res: Some(Message::Item((res, body.length()))), diff --git a/src/test.rs b/src/test.rs index 84a959c4..3d12e344 100644 --- a/src/test.rs +++ b/src/test.rs @@ -306,8 +306,7 @@ impl TestServer { pub fn with_factory( factory: F, ) -> TestServerRuntime< - impl Service - + Clone, + impl Service + Clone, > { let (tx, rx) = mpsc::channel(); @@ -339,8 +338,8 @@ impl TestServer { } fn new_connector( -) -> impl Service - + Clone { +) -> impl Service + Clone + { #[cfg(feature = "ssl")] { use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode}; @@ -441,7 +440,7 @@ impl TestServerRuntime { impl TestServerRuntime where - T: Service + Clone, + T: Service + Clone, T::Response: Connection, { /// Connect to websocket server at a given path diff --git a/src/ws/client/service.rs b/src/ws/client/service.rs index 94be59f6..68f8032e 100644 --- a/src/ws/client/service.rs +++ b/src/ws/client/service.rs @@ -26,7 +26,7 @@ pub type DefaultClient = Client; /// WebSocket's client pub struct Client where - T: Service, + T: Service, T::Response: AsyncRead + AsyncWrite, { connector: T, @@ -34,7 +34,7 @@ where impl Client where - T: Service, + T: Service, T::Response: AsyncRead + AsyncWrite, { /// Create new websocket's client factory @@ -51,7 +51,7 @@ impl Default for Client { impl Clone for Client where - T: Service + Clone, + T: Service + Clone, T::Response: AsyncRead + AsyncWrite, { fn clone(&self) -> Self { @@ -61,13 +61,12 @@ where } } -impl Service for Client +impl Service for Client where - T: Service, + T: Service, T::Response: AsyncRead + AsyncWrite + 'static, T::Future: 'static, { - type Request = Connect; type Response = Framed; type Error = ClientError; type Future = Either< @@ -79,7 +78,7 @@ where self.connector.poll_ready().map_err(ClientError::from) } - fn call(&mut self, mut req: Self::Request) -> Self::Future { + fn call(&mut self, mut req: Connect) -> Self::Future { if let Some(e) = req.err.take() { Either::A(err(e)) } else if let Some(e) = req.http_err.take() { diff --git a/src/ws/service.rs b/src/ws/service.rs index 9cce4d63..118a2244 100644 --- a/src/ws/service.rs +++ b/src/ws/service.rs @@ -20,8 +20,7 @@ impl Default for VerifyWebSockets { } } -impl NewService for VerifyWebSockets { - type Request = (Request, Framed); +impl NewService<(Request, Framed)> for VerifyWebSockets { type Response = (Request, Framed); type Error = (HandshakeError, Framed); type InitError = (); @@ -33,8 +32,7 @@ impl NewService for VerifyWebSockets { } } -impl Service for VerifyWebSockets { - type Request = (Request, Framed); +impl Service<(Request, Framed)> for VerifyWebSockets { type Response = (Request, Framed); type Error = (HandshakeError, Framed); type Future = FutureResult; @@ -43,7 +41,7 @@ impl Service for VerifyWebSockets { Ok(Async::Ready(())) } - fn call(&mut self, (req, framed): Self::Request) -> Self::Future { + fn call(&mut self, (req, framed): (Request, Framed)) -> Self::Future { match verify_handshake(&req) { Err(e) => Err((e, framed)).into_future(), Ok(_) => Ok((req, framed)).into_future(), diff --git a/src/ws/transport.rs b/src/ws/transport.rs index 102d02b4..8cd79cb0 100644 --- a/src/ws/transport.rs +++ b/src/ws/transport.rs @@ -8,7 +8,7 @@ use super::{Codec, Frame, Message}; pub struct Transport where - S: Service, + S: Service, T: AsyncRead + AsyncWrite, { inner: FramedTransport, @@ -17,17 +17,17 @@ where impl Transport where T: AsyncRead + AsyncWrite, - S: Service, + S: Service, S::Future: 'static, S::Error: 'static, { - pub fn new>(io: T, service: F) -> Self { + pub fn new>(io: T, service: F) -> Self { Transport { inner: FramedTransport::new(Framed::new(io, Codec::new()), service), } } - pub fn with>(framed: Framed, service: F) -> Self { + pub fn with>(framed: Framed, service: F) -> Self { Transport { inner: FramedTransport::new(framed, service), } @@ -37,7 +37,7 @@ where impl Future for Transport where T: AsyncRead + AsyncWrite, - S: Service, + S: Service, S::Future: 'static, S::Error: 'static, {