mirror of
https://github.com/actix/actix-web.git
synced 2024-12-21 07:36:43 +00:00
better FnNewService definition
This commit is contained in:
parent
b8c8dbc90a
commit
0f8cd0f44d
2 changed files with 13 additions and 17 deletions
|
@ -68,19 +68,13 @@ fn main() {
|
||||||
SslAcceptorExt::accept_async(&acceptor, stream)
|
SslAcceptorExt::accept_async(&acceptor, stream)
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
|
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
|
||||||
})
|
})
|
||||||
// convert closure to a `NewService` and convert init error
|
// convert closure to a `NewService`
|
||||||
.into_new_service().map_init_err(|_| io::Error::new(io::ErrorKind::Other, ""))
|
.into_new_service()
|
||||||
|
|
||||||
// .and_then() combinator uses other service to convert incoming `Request` to a `Response`
|
// .and_then() combinator uses other service to convert incoming `Request` to a `Response`
|
||||||
// and then uses that response as an input for next service.
|
// and then uses that response as an input for next service.
|
||||||
// in this case, on success we use `logger` service
|
// in this case, on success we use `logger` service
|
||||||
.and_then(
|
.and_then(logger)
|
||||||
// convert function to a Service and related NewService impl
|
|
||||||
logger.into_new_service()
|
|
||||||
// if service is a function actix-net generate NewService impl with InitError=(),
|
|
||||||
// but actix_net::Server requires io::Error as InitError,
|
|
||||||
// we need to convert it to a `io::Error`
|
|
||||||
.map_init_err(|_| io::Error::new(io::ErrorKind::Other, "")))
|
|
||||||
|
|
||||||
// next service uses two components, service state and service function
|
// next service uses two components, service state and service function
|
||||||
// actix-net generates `NewService` impl that creates `ServiceState` instance for each new service
|
// actix-net generates `NewService` impl that creates `ServiceState` instance for each new service
|
||||||
|
|
|
@ -163,7 +163,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FnNewService<F, Req, Resp, Err, Fut, Cfg>
|
pub struct FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg>
|
||||||
where
|
where
|
||||||
F: Fn(Req) -> Fut,
|
F: Fn(Req) -> Fut,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
|
@ -172,10 +172,11 @@ where
|
||||||
req: marker::PhantomData<Req>,
|
req: marker::PhantomData<Req>,
|
||||||
resp: marker::PhantomData<Resp>,
|
resp: marker::PhantomData<Resp>,
|
||||||
err: marker::PhantomData<Err>,
|
err: marker::PhantomData<Err>,
|
||||||
|
ierr: marker::PhantomData<IErr>,
|
||||||
cfg: marker::PhantomData<Cfg>,
|
cfg: marker::PhantomData<Cfg>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut, Cfg> FnNewService<F, Req, Resp, Err, Fut, Cfg>
|
impl<F, Req, Resp, Err, IErr, Fut, Cfg> FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg>
|
||||||
where
|
where
|
||||||
F: Fn(Req) -> Fut + Clone,
|
F: Fn(Req) -> Fut + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
|
@ -186,12 +187,13 @@ where
|
||||||
req: marker::PhantomData,
|
req: marker::PhantomData,
|
||||||
resp: marker::PhantomData,
|
resp: marker::PhantomData,
|
||||||
err: marker::PhantomData,
|
err: marker::PhantomData,
|
||||||
|
ierr: marker::PhantomData,
|
||||||
cfg: marker::PhantomData,
|
cfg: marker::PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut, Cfg> NewService for FnNewService<F, Req, Resp, Err, Fut, Cfg>
|
impl<F, Req, Resp, Err, IErr, Fut, Cfg> NewService for FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg>
|
||||||
where
|
where
|
||||||
F: Fn(Req) -> Fut + Clone,
|
F: Fn(Req) -> Fut + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
|
@ -202,27 +204,27 @@ where
|
||||||
type Error = Err;
|
type Error = Err;
|
||||||
type Service = FnService<F, Req, Resp, Err, Fut>;
|
type Service = FnService<F, Req, Resp, Err, Fut>;
|
||||||
type Config = Cfg;
|
type Config = Cfg;
|
||||||
type InitError = ();
|
type InitError = IErr;
|
||||||
type Future = FutureResult<Self::Service, ()>;
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
||||||
|
|
||||||
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
fn new_service(&self, cfg: Cfg) -> Self::Future {
|
||||||
future::ok(FnService::new(self.f.clone()))
|
future::ok(FnService::new(self.f.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut, Cfg> IntoNewService<FnNewService<F, Req, Resp, Err, Fut, Cfg>>
|
impl<F, Req, Resp, Err, IErr, Fut, Cfg> IntoNewService<FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg>>
|
||||||
for F
|
for F
|
||||||
where
|
where
|
||||||
F: Fn(Req) -> Fut + Clone + 'static,
|
F: Fn(Req) -> Fut + Clone + 'static,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
Cfg: Clone,
|
Cfg: Clone,
|
||||||
{
|
{
|
||||||
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, Fut, Cfg> {
|
fn into_new_service(self) -> FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg> {
|
||||||
FnNewService::new(self)
|
FnNewService::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F, Req, Resp, Err, Fut, Cfg> Clone for FnNewService<F, Req, Resp, Err, Fut, Cfg>
|
impl<F, Req, Resp, Err, IErr, Fut, Cfg> Clone for FnNewService<F, Req, Resp, Err, IErr, Fut, Cfg>
|
||||||
where
|
where
|
||||||
F: Fn(Req) -> Fut + Clone,
|
F: Fn(Req) -> Fut + Clone,
|
||||||
Fut: IntoFuture<Item = Resp, Error = Err>,
|
Fut: IntoFuture<Item = Resp, Error = Err>,
|
||||||
|
|
Loading…
Reference in a new issue