mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-19 21:15:38 +02:00
make service Request type generic
This commit is contained in:
@@ -16,9 +16,9 @@ pub struct MapErr<A, F, E> {
|
||||
|
||||
impl<A, F, E> MapErr<A, F, E> {
|
||||
/// Create new `MapErr` combinator
|
||||
pub fn new(service: A, f: F) -> Self
|
||||
pub fn new<R>(service: A, f: F) -> Self
|
||||
where
|
||||
A: Service,
|
||||
A: Service<R>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
Self {
|
||||
@@ -43,47 +43,39 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> Service for MapErr<A, F, E>
|
||||
impl<A, F, E, R> Service<R> for MapErr<A, F, E>
|
||||
where
|
||||
A: Service,
|
||||
A: Service<R>,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = A::Response;
|
||||
type Error = E;
|
||||
type Future = MapErrFuture<A, F, E>;
|
||||
type Future = MapErrFuture<A, F, E, R>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.service.poll_ready().map_err(&self.f)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: A::Request) -> Self::Future {
|
||||
MapErrFuture::new(self.service.call(req), self.f.clone())
|
||||
fn call(&mut self, req: R) -> Self::Future {
|
||||
MapErrFuture {
|
||||
fut: self.service.call(req),
|
||||
f: self.f.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapErrFuture<A, F, E>
|
||||
pub struct MapErrFuture<A, F, E, R>
|
||||
where
|
||||
A: Service,
|
||||
A: Service<R>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
f: F,
|
||||
fut: A::Future,
|
||||
}
|
||||
|
||||
impl<A, F, E> MapErrFuture<A, F, E>
|
||||
impl<A, F, E, R> Future for MapErrFuture<A, F, E, R>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
MapErrFuture { f, fut }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> Future for MapErrFuture<A, F, E>
|
||||
where
|
||||
A: Service,
|
||||
A: Service<R>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
type Item = A::Response;
|
||||
@@ -106,9 +98,9 @@ pub struct MapErrNewService<A, F, E, C> {
|
||||
|
||||
impl<A, F, E, C> MapErrNewService<A, F, E, C> {
|
||||
/// Create new `MapErr` new service instance
|
||||
pub fn new(a: A, f: F) -> Self
|
||||
pub fn new<R>(a: A, f: F) -> Self
|
||||
where
|
||||
A: NewService<C>,
|
||||
A: NewService<R, C>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
Self {
|
||||
@@ -133,36 +125,35 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E, C> NewService<C> for MapErrNewService<A, F, E, C>
|
||||
impl<A, F, E, R, C> NewService<R, C> for MapErrNewService<A, F, E, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
A: NewService<R, C>,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = A::Response;
|
||||
type Error = E;
|
||||
type Service = MapErr<A::Service, F, E>;
|
||||
|
||||
type InitError = A::InitError;
|
||||
type Future = MapErrNewServiceFuture<A, F, E, C>;
|
||||
type Future = MapErrNewServiceFuture<A, F, E, R, C>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
MapErrNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapErrNewServiceFuture<A, F, E, C>
|
||||
pub struct MapErrNewServiceFuture<A, F, E, R, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
A: NewService<R, C>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fut: A::Future,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<A, F, E, C> MapErrNewServiceFuture<A, F, E, C>
|
||||
impl<A, F, E, R, C> MapErrNewServiceFuture<A, F, E, R, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
A: NewService<R, C>,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
@@ -170,9 +161,9 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E, C> Future for MapErrNewServiceFuture<A, F, E, C>
|
||||
impl<A, F, E, R, C> Future for MapErrNewServiceFuture<A, F, E, R, C>
|
||||
where
|
||||
A: NewService<C>,
|
||||
A: NewService<R, C>,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
type Item = MapErr<A::Service, F, E>;
|
||||
@@ -196,8 +187,7 @@ mod tests {
|
||||
|
||||
struct Srv;
|
||||
|
||||
impl Service for Srv {
|
||||
type Request = ();
|
||||
impl Service<()> for Srv {
|
||||
type Response = ();
|
||||
type Error = ();
|
||||
type Future = FutureResult<(), ()>;
|
||||
|
Reference in New Issue
Block a user