1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-10-21 09:23:54 +00:00

use new Service and NewService traits

This commit is contained in:
Nikolay Kim 2018-11-30 11:57:57 -08:00
parent d269904fbf
commit 5003c00efb
13 changed files with 73 additions and 108 deletions

View file

@ -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"

View file

@ -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

View file

@ -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<Connect, Response = impl Connection, Error = ConnectorError> + Clone
{
#[cfg(not(feature = "ssl"))]
{
let connector = TimeoutService::new(
@ -216,7 +213,7 @@ mod connect_impl {
pub(crate) struct InnerConnector<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError>,
{
pub(crate) tcp_pool: ConnectionPool<T, Io>,
}
@ -224,8 +221,7 @@ mod connect_impl {
impl<T, Io> Clone for InnerConnector<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>
+ Clone,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError> + Clone,
{
fn clone(&self) -> Self {
InnerConnector {
@ -234,16 +230,15 @@ mod connect_impl {
}
}
impl<T, Io> Service for InnerConnector<T, Io>
impl<T, Io> Service<Connect> for InnerConnector<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError>,
{
type Request = Connect;
type Response = IoConnection<Io>;
type Error = ConnectorError;
type Future = Either<
<ConnectionPool<T, Io> as Service>::Future,
<ConnectionPool<T, Io> as Service<Connect>>::Future,
FutureResult<IoConnection<Io>, 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<Connect, Response = (Connect, Io1), Error = ConnectorError> + Clone,
T2: Service<Connect, Response = (Connect, Io2), Error = ConnectorError> + 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<Connect, Response = (Connect, Io1), Error = ConnectorError>,
T2: Service<Connect, Response = (Connect, Io2), Error = ConnectorError>,
{
type Request = Connect;
type Response = IoEither<IoConnection<Io1>, IoConnection<Io2>>;
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<T, Io1, Io2>
where
Io1: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io1), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io1), Error = ConnectorError>,
{
fut: <ConnectionPool<T, Io1> as Service>::Future,
_t: PhantomData<Io2>,
@ -372,7 +350,7 @@ mod connect_impl {
impl<T, Io1, Io2> Future for InnerConnectorResponseA<T, Io1, Io2>
where
T: Service<Request = Connect, Response = (Connect, Io1), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io1), Error = ConnectorError>,
Io1: AsyncRead + AsyncWrite + 'static,
Io2: AsyncRead + AsyncWrite + 'static,
{
@ -390,7 +368,7 @@ mod connect_impl {
pub(crate) struct InnerConnectorResponseB<T, Io1, Io2>
where
Io2: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io2), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io2), Error = ConnectorError>,
{
fut: <ConnectionPool<T, Io2> as Service>::Future,
_t: PhantomData<Io1>,
@ -398,7 +376,7 @@ mod connect_impl {
impl<T, Io1, Io2> Future for InnerConnectorResponseB<T, Io1, Io2>
where
T: Service<Request = Connect, Response = (Connect, Io2), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io2), Error = ConnectorError>,
Io1: AsyncRead + AsyncWrite + 'static,
Io2: AsyncRead + AsyncWrite + 'static,
{

View file

@ -21,7 +21,7 @@ pub(crate) fn send_request<T, I, B>(
connector: &mut T,
) -> impl Future<Item = ClientResponse, Error = SendRequestError>
where
T: Service<Request = Connect, Response = I, Error = ConnectorError>,
T: Service<Connect, Response = I, Error = ConnectorError>,
B: MessageBody,
I: Connection,
{

View file

@ -47,7 +47,7 @@ pub(crate) struct ConnectionPool<T, Io: AsyncRead + AsyncWrite + 'static>(
impl<T, Io> ConnectionPool<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError>,
{
pub(crate) fn new(
connector: T,
@ -83,12 +83,11 @@ where
}
}
impl<T, Io> Service for ConnectionPool<T, Io>
impl<T, Io> Service<Connect> for ConnectionPool<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError>,
{
type Request = Connect;
type Response = IoConnection<Io>;
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<T, Io> Future for ConnectorPoolSupport<T, Io>
where
Io: AsyncRead + AsyncWrite + 'static,
T: Service<Request = Connect, Response = (Connect, Io), Error = ConnectorError>,
T: Service<Connect, Response = (Connect, Io), Error = ConnectorError>,
T::Future: 'static,
{
type Item = ();

View file

@ -177,7 +177,7 @@ where
connector: &mut T,
) -> impl Future<Item = ClientResponse, Error = SendRequestError>
where
T: Service<Request = Connect, Response = I, Error = ConnectorError>,
T: Service<Connect, Response = I, Error = ConnectorError>,
I: Connection,
{
pipeline::send_request(self.head, self.body, connector)

View file

@ -36,14 +36,14 @@ bitflags! {
}
/// Dispatcher for HTTP/1.1 protocol
pub struct Dispatcher<T, S: Service, B: MessageBody>
pub struct Dispatcher<T, S: Service<Request>, B: MessageBody>
where
S::Error: Debug,
{
inner: Option<InnerDispatcher<T, S, B>>,
}
struct InnerDispatcher<T, S: Service, B: MessageBody>
struct InnerDispatcher<T, S: Service<Request>, B: MessageBody>
where
S::Error: Debug,
{
@ -67,13 +67,13 @@ enum DispatcherMessage {
Error(Response<()>),
}
enum State<S: Service, B: MessageBody> {
enum State<S: Service<Request>, B: MessageBody> {
None,
ServiceCall(S::Future),
SendPayload(ResponseBody<B>),
}
impl<S: Service, B: MessageBody> State<S, B> {
impl<S: Service<Request>, B: MessageBody> State<S, B> {
fn is_empty(&self) -> bool {
if let State::None = self {
true
@ -86,7 +86,7 @@ impl<S: Service, B: MessageBody> State<S, B> {
impl<T, S, B> Dispatcher<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request, Response = Response<B>>,
S: Service<Request, Response = Response<B>>,
S::Error: Debug,
B: MessageBody,
{
@ -140,7 +140,7 @@ where
impl<T, S, B> InnerDispatcher<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request, Response = Response<B>>,
S: Service<Request, Response = Response<B>>,
S::Error: Debug,
B: MessageBody,
{
@ -463,7 +463,7 @@ where
impl<T, S, B> Future for Dispatcher<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request, Response = Response<B>>,
S: Service<Request, Response = Response<B>>,
S::Error: Debug,
B: MessageBody,
{

View file

@ -27,13 +27,13 @@ pub struct H1Service<T, S, B> {
impl<T, S, B> H1Service<T, S, B>
where
S: NewService<Request = Request, Response = Response<B>> + Clone,
S: NewService<Request, Response = Response<B>> + Clone,
S::Service: Clone,
S::Error: Debug,
B: MessageBody,
{
/// Create new `HttpService` instance.
pub fn new<F: IntoNewService<S>>(service: F) -> Self {
pub fn new<F: IntoNewService<S, Request>>(service: F) -> Self {
let cfg = ServiceConfig::new(KeepAlive::Timeout(5), 5000, 0);
H1Service {
@ -49,15 +49,14 @@ where
}
}
impl<T, S, B> NewService for H1Service<T, S, B>
impl<T, S, B> NewService<T> for H1Service<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: NewService<Request = Request, Response = Response<B>> + Clone,
S: NewService<Request, Response = Response<B>> + Clone,
S::Service: Clone,
S::Error: Debug,
B: MessageBody,
{
type Request = T;
type Response = H1ServiceResult<T>;
type Error = DispatchError<S::Error>;
type InitError = S::InitError;
@ -89,7 +88,7 @@ pub struct H1ServiceBuilder<T, S> {
impl<T, S> H1ServiceBuilder<T, S>
where
S: NewService,
S: NewService<Request>,
S::Service: Clone,
S::Error: Debug,
{
@ -186,7 +185,7 @@ where
pub fn finish<F, B>(self, service: F) -> H1Service<T, S, B>
where
B: MessageBody,
F: IntoNewService<S>,
F: IntoNewService<S, Request>,
{
let cfg = ServiceConfig::new(
self.keep_alive,
@ -202,7 +201,7 @@ where
}
#[doc(hidden)]
pub struct H1ServiceResponse<T, S: NewService, B> {
pub struct H1ServiceResponse<T, S: NewService<Request>, B> {
fut: S::Future,
cfg: Option<ServiceConfig>,
_t: PhantomData<(T, B)>,
@ -211,7 +210,7 @@ pub struct H1ServiceResponse<T, S: NewService, B> {
impl<T, S, B> Future for H1ServiceResponse<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: NewService<Request = Request, Response = Response<B>>,
S: NewService<Request, Response = Response<B>>,
S::Service: Clone,
S::Error: Debug,
B: MessageBody,
@ -237,7 +236,7 @@ pub struct H1ServiceHandler<T, S, B> {
impl<T, S, B> H1ServiceHandler<T, S, B>
where
S: Service<Request = Request, Response = Response<B>> + Clone,
S: Service<Request, Response = Response<B>> + Clone,
S::Error: Debug,
B: MessageBody,
{
@ -250,14 +249,13 @@ where
}
}
impl<T, S, B> Service for H1ServiceHandler<T, S, B>
impl<T, S, B> Service<T> for H1ServiceHandler<T, S, B>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Request, Response = Response<B>> + Clone,
S: Service<Request, Response = Response<B>> + Clone,
S::Error: Debug,
B: MessageBody,
{
type Request = T;
type Response = H1ServiceResult<T>;
type Error = DispatchError<S::Error>;
type Future = Dispatcher<T, S, B>;
@ -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<T> NewService for OneRequest<T>
impl<T> NewService<T> for OneRequest<T>
where
T: AsyncRead + AsyncWrite,
{
type Request = T;
type Response = (Request, Framed<T, Codec>);
type Error = ParseError;
type InitError = ();
@ -316,11 +313,10 @@ pub struct OneRequestService<T> {
_t: PhantomData<T>,
}
impl<T> Service for OneRequestService<T>
impl<T> Service<T> for OneRequestService<T>
where
T: AsyncRead + AsyncWrite,
{
type Request = T;
type Response = (Request, Framed<T, Codec>);
type Error = ParseError;
type Future = OneRequestServiceResponse<T>;
@ -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()))),
}

View file

@ -23,12 +23,11 @@ where
}
}
impl<T, R, E> NewService for SendError<T, R, E>
impl<T, R, E> NewService<Result<R, (E, Framed<T, Codec>)>> for SendError<T, R, E>
where
T: AsyncRead + AsyncWrite,
E: ResponseError,
{
type Request = Result<R, (E, Framed<T, Codec>)>;
type Response = R;
type Error = (E, Framed<T, Codec>);
type InitError = ();
@ -40,12 +39,11 @@ where
}
}
impl<T, R, E> Service for SendError<T, R, E>
impl<T, R, E> Service<Result<R, (E, Framed<T, Codec>)>> for SendError<T, R, E>
where
T: AsyncRead + AsyncWrite,
E: ResponseError,
{
type Request = Result<R, (E, Framed<T, Codec>)>;
type Response = R;
type Error = (E, Framed<T, Codec>);
type Future = Either<FutureResult<R, (E, Framed<T, Codec>)>, SendErrorFut<T, R, E>>;
@ -54,7 +52,7 @@ where
Ok(Async::Ready(()))
}
fn call(&mut self, req: Self::Request) -> Self::Future {
fn call(&mut self, req: Result<R, (E, Framed<T, Codec>)>) -> Self::Future {
match req {
Ok(r) => Either::A(ok(r)),
Err((e, framed)) => {
@ -131,12 +129,11 @@ where
}
}
impl<T, B> NewService for SendResponse<T, B>
impl<T, B> NewService<(Response<B>, Framed<T, Codec>)> for SendResponse<T, B>
where
T: AsyncRead + AsyncWrite,
B: MessageBody,
{
type Request = (Response<B>, Framed<T, Codec>);
type Response = Framed<T, Codec>;
type Error = Error;
type InitError = ();
@ -148,12 +145,11 @@ where
}
}
impl<T, B> Service for SendResponse<T, B>
impl<T, B> Service<(Response<B>, Framed<T, Codec>)> for SendResponse<T, B>
where
T: AsyncRead + AsyncWrite,
B: MessageBody,
{
type Request = (Response<B>, Framed<T, Codec>);
type Response = Framed<T, Codec>;
type Error = Error;
type Future = SendResponseFut<T, B>;
@ -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<B>, Framed<T, Codec>)) -> Self::Future {
let (res, body) = res.replace_body(());
SendResponseFut {
res: Some(Message::Item((res, body.length()))),

View file

@ -306,8 +306,7 @@ impl TestServer {
pub fn with_factory<F: StreamServiceFactory>(
factory: F,
) -> TestServerRuntime<
impl Service<Request = Connect, Response = impl Connection, Error = ConnectorError>
+ Clone,
impl Service<Connect, Response = impl Connection, Error = ConnectorError> + Clone,
> {
let (tx, rx) = mpsc::channel();
@ -339,8 +338,8 @@ impl TestServer {
}
fn new_connector(
) -> impl Service<Request = Connect, Response = impl Connection, Error = ConnectorError>
+ Clone {
) -> impl Service<Connect, Response = impl Connection, Error = ConnectorError> + Clone
{
#[cfg(feature = "ssl")]
{
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
@ -441,7 +440,7 @@ impl<T> TestServerRuntime<T> {
impl<T> TestServerRuntime<T>
where
T: Service<Request = Connect, Error = ConnectorError> + Clone,
T: Service<Connect, Error = ConnectorError> + Clone,
T::Response: Connection,
{
/// Connect to websocket server at a given path

View file

@ -26,7 +26,7 @@ pub type DefaultClient = Client<DefaultConnector>;
/// WebSocket's client
pub struct Client<T>
where
T: Service<Error = ConnectorError>,
T: Service<TcpConnect, Error = ConnectorError>,
T::Response: AsyncRead + AsyncWrite,
{
connector: T,
@ -34,7 +34,7 @@ where
impl<T> Client<T>
where
T: Service<Request = TcpConnect, Error = ConnectorError>,
T: Service<TcpConnect, Error = ConnectorError>,
T::Response: AsyncRead + AsyncWrite,
{
/// Create new websocket's client factory
@ -51,7 +51,7 @@ impl Default for Client<DefaultConnector> {
impl<T> Clone for Client<T>
where
T: Service<Request = TcpConnect, Error = ConnectorError> + Clone,
T: Service<TcpConnect, Error = ConnectorError> + Clone,
T::Response: AsyncRead + AsyncWrite,
{
fn clone(&self) -> Self {
@ -61,13 +61,12 @@ where
}
}
impl<T> Service for Client<T>
impl<T> Service<Connect> for Client<T>
where
T: Service<Request = TcpConnect, Error = ConnectorError>,
T: Service<TcpConnect, Error = ConnectorError>,
T::Response: AsyncRead + AsyncWrite + 'static,
T::Future: 'static,
{
type Request = Connect;
type Response = Framed<T::Response, Codec>;
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() {

View file

@ -20,8 +20,7 @@ impl<T> Default for VerifyWebSockets<T> {
}
}
impl<T> NewService for VerifyWebSockets<T> {
type Request = (Request, Framed<T, Codec>);
impl<T> NewService<(Request, Framed<T, Codec>)> for VerifyWebSockets<T> {
type Response = (Request, Framed<T, Codec>);
type Error = (HandshakeError, Framed<T, Codec>);
type InitError = ();
@ -33,8 +32,7 @@ impl<T> NewService for VerifyWebSockets<T> {
}
}
impl<T> Service for VerifyWebSockets<T> {
type Request = (Request, Framed<T, Codec>);
impl<T> Service<(Request, Framed<T, Codec>)> for VerifyWebSockets<T> {
type Response = (Request, Framed<T, Codec>);
type Error = (HandshakeError, Framed<T, Codec>);
type Future = FutureResult<Self::Response, Self::Error>;
@ -43,7 +41,7 @@ impl<T> Service for VerifyWebSockets<T> {
Ok(Async::Ready(()))
}
fn call(&mut self, (req, framed): Self::Request) -> Self::Future {
fn call(&mut self, (req, framed): (Request, Framed<T, Codec>)) -> Self::Future {
match verify_handshake(&req) {
Err(e) => Err((e, framed)).into_future(),
Ok(_) => Ok((req, framed)).into_future(),

View file

@ -8,7 +8,7 @@ use super::{Codec, Frame, Message};
pub struct Transport<S, T>
where
S: Service,
S: Service<Frame, Response = Message>,
T: AsyncRead + AsyncWrite,
{
inner: FramedTransport<S, T, Codec>,
@ -17,17 +17,17 @@ where
impl<S, T> Transport<S, T>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Frame, Response = Message>,
S: Service<Frame, Response = Message>,
S::Future: 'static,
S::Error: 'static,
{
pub fn new<F: IntoService<S>>(io: T, service: F) -> Self {
pub fn new<F: IntoService<S, Frame>>(io: T, service: F) -> Self {
Transport {
inner: FramedTransport::new(Framed::new(io, Codec::new()), service),
}
}
pub fn with<F: IntoService<S>>(framed: Framed<T, Codec>, service: F) -> Self {
pub fn with<F: IntoService<S, Frame>>(framed: Framed<T, Codec>, service: F) -> Self {
Transport {
inner: FramedTransport::new(framed, service),
}
@ -37,7 +37,7 @@ where
impl<S, T> Future for Transport<S, T>
where
T: AsyncRead + AsyncWrite,
S: Service<Request = Frame, Response = Message>,
S: Service<Frame, Response = Message>,
S::Future: 'static,
S::Error: 'static,
{