mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-31 05:06:59 +02:00
service trait takes request type parameter (#232)
This commit is contained in:
@@ -8,18 +8,18 @@ use super::{Service, ServiceFactory};
|
||||
/// Service for the `map` combinator, changing the type of a service's response.
|
||||
///
|
||||
/// This is created by the `ServiceExt::map` method.
|
||||
pub struct Map<A, F, Response> {
|
||||
pub struct Map<A, F, Req, Res> {
|
||||
service: A,
|
||||
f: F,
|
||||
_t: PhantomData<Response>,
|
||||
_t: PhantomData<(Req, Res)>,
|
||||
}
|
||||
|
||||
impl<A, F, Response> Map<A, F, Response> {
|
||||
impl<A, F, Req, Res> Map<A, F, Req, Res> {
|
||||
/// Create new `Map` combinator
|
||||
pub(crate) fn new(service: A, f: F) -> Self
|
||||
where
|
||||
A: Service,
|
||||
F: FnMut(A::Response) -> Response,
|
||||
A: Service<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
Self {
|
||||
service,
|
||||
@@ -29,7 +29,7 @@ impl<A, F, Response> Map<A, F, Response> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Response> Clone for Map<A, F, Response>
|
||||
impl<A, F, Req, Res> Clone for Map<A, F, Req, Res>
|
||||
where
|
||||
A: Clone,
|
||||
F: Clone,
|
||||
@@ -43,52 +43,51 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Response> Service for Map<A, F, Response>
|
||||
impl<A, F, Req, Res> Service<Req> for Map<A, F, Req, Res>
|
||||
where
|
||||
A: Service,
|
||||
F: FnMut(A::Response) -> Response + Clone,
|
||||
A: Service<Req>,
|
||||
F: FnMut(A::Response) -> Res + Clone,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = Response;
|
||||
type Response = Res;
|
||||
type Error = A::Error;
|
||||
type Future = MapFuture<A, F, Response>;
|
||||
type Future = MapFuture<A, F, Req, Res>;
|
||||
|
||||
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
self.service.poll_ready(ctx)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
fn call(&mut self, req: Req) -> Self::Future {
|
||||
MapFuture::new(self.service.call(req), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct MapFuture<A, F, Response>
|
||||
pub struct MapFuture<A, F, Req, Res>
|
||||
where
|
||||
A: Service,
|
||||
F: FnMut(A::Response) -> Response,
|
||||
A: Service<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
f: F,
|
||||
#[pin]
|
||||
fut: A::Future,
|
||||
}
|
||||
|
||||
impl<A, F, Response> MapFuture<A, F, Response>
|
||||
impl<A, F, Req, Res> MapFuture<A, F, Req, Res>
|
||||
where
|
||||
A: Service,
|
||||
F: FnMut(A::Response) -> Response,
|
||||
A: Service<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
MapFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Response> Future for MapFuture<A, F, Response>
|
||||
impl<A, F, Req, Res> Future for MapFuture<A, F, Req, Res>
|
||||
where
|
||||
A: Service,
|
||||
F: FnMut(A::Response) -> Response,
|
||||
A: Service<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
type Output = Result<Response, A::Error>;
|
||||
type Output = Result<Res, A::Error>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
@@ -102,17 +101,17 @@ where
|
||||
}
|
||||
|
||||
/// `MapNewService` new service combinator
|
||||
pub struct MapServiceFactory<A, F, Res> {
|
||||
pub struct MapServiceFactory<A, F, Req, Res> {
|
||||
a: A,
|
||||
f: F,
|
||||
r: PhantomData<Res>,
|
||||
r: PhantomData<(Res, Req)>,
|
||||
}
|
||||
|
||||
impl<A, F, Res> MapServiceFactory<A, F, Res> {
|
||||
impl<A, F, Req, Res> MapServiceFactory<A, F, Req, Res> {
|
||||
/// Create new `Map` new service instance
|
||||
pub(crate) fn new(a: A, f: F) -> Self
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A: ServiceFactory<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
Self {
|
||||
@@ -123,7 +122,7 @@ impl<A, F, Res> MapServiceFactory<A, F, Res> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Res> Clone for MapServiceFactory<A, F, Res>
|
||||
impl<A, F, Req, Res> Clone for MapServiceFactory<A, F, Req, Res>
|
||||
where
|
||||
A: Clone,
|
||||
F: Clone,
|
||||
@@ -137,19 +136,18 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Res> ServiceFactory for MapServiceFactory<A, F, Res>
|
||||
impl<A, F, Req, Res> ServiceFactory<Req> for MapServiceFactory<A, F, Req, Res>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A: ServiceFactory<Req>,
|
||||
F: FnMut(A::Response) -> Res + Clone,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = Res;
|
||||
type Error = A::Error;
|
||||
|
||||
type Config = A::Config;
|
||||
type Service = Map<A::Service, F, Res>;
|
||||
type Service = Map<A::Service, F, Req, Res>;
|
||||
type InitError = A::InitError;
|
||||
type Future = MapServiceFuture<A, F, Res>;
|
||||
type Future = MapServiceFuture<A, F, Req, Res>;
|
||||
|
||||
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
||||
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
@@ -157,9 +155,9 @@ where
|
||||
}
|
||||
|
||||
#[pin_project::pin_project]
|
||||
pub struct MapServiceFuture<A, F, Res>
|
||||
pub struct MapServiceFuture<A, F, Req, Res>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A: ServiceFactory<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
#[pin]
|
||||
@@ -167,9 +165,9 @@ where
|
||||
f: Option<F>,
|
||||
}
|
||||
|
||||
impl<A, F, Res> MapServiceFuture<A, F, Res>
|
||||
impl<A, F, Req, Res> MapServiceFuture<A, F, Req, Res>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A: ServiceFactory<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
@@ -177,12 +175,12 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, Res> Future for MapServiceFuture<A, F, Res>
|
||||
impl<A, F, Req, Res> Future for MapServiceFuture<A, F, Req, Res>
|
||||
where
|
||||
A: ServiceFactory,
|
||||
A: ServiceFactory<Req>,
|
||||
F: FnMut(A::Response) -> Res,
|
||||
{
|
||||
type Output = Result<Map<A::Service, F, Res>, A::InitError>;
|
||||
type Output = Result<Map<A::Service, F, Req, Res>, A::InitError>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
let this = self.project();
|
||||
@@ -204,8 +202,7 @@ mod tests {
|
||||
|
||||
struct Srv;
|
||||
|
||||
impl Service for Srv {
|
||||
type Request = ();
|
||||
impl Service<()> for Srv {
|
||||
type Response = ();
|
||||
type Error = ();
|
||||
type Future = Ready<Result<(), ()>>;
|
||||
|
Reference in New Issue
Block a user