mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-23 20:51:06 +01:00
add .map_err() service combinator
This commit is contained in:
parent
1a496f4fda
commit
8faf373a99
172
src/service.rs
172
src/service.rs
@ -15,6 +15,11 @@ pub trait NewServiceExt: NewService {
|
||||
Error = Self::Error,
|
||||
InitError = Self::InitError,
|
||||
>;
|
||||
|
||||
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
|
||||
where
|
||||
Self: Sized,
|
||||
F: Fn(Self::Error) -> E;
|
||||
}
|
||||
|
||||
impl<T> NewServiceExt for T
|
||||
@ -32,6 +37,13 @@ where
|
||||
{
|
||||
AndThenNewService::new(self, new_service)
|
||||
}
|
||||
|
||||
fn map_err<F, E>(self, f: F) -> MapErrNewService<Self, F, E>
|
||||
where
|
||||
F: Fn(Self::Error) -> E
|
||||
{
|
||||
MapErrNewService::new(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Trait for types that can be converted to a Service
|
||||
@ -519,3 +531,163 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// `MapErr` service combinator
|
||||
pub struct MapErr<A, F, E> {
|
||||
a: A,
|
||||
f: F,
|
||||
e: marker::PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<A, F, E> MapErr<A, F, E>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
/// Create new `MapErr` combinator
|
||||
pub fn new(a: A, f: F) -> Self {
|
||||
Self { a, f, e: marker::PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> Service for MapErr<A, F, E>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
type Request = A::Request;
|
||||
type Response = A::Response;
|
||||
type Error = E;
|
||||
type Future = MapErrFuture<A, F, E>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.a.poll_ready().map_err(|e| (self.f)(e))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
MapErrFuture::new(self.a.call(req), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapErrFuture<A, F, E>
|
||||
where
|
||||
A: Service,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
f: F,
|
||||
fut: A::Future,
|
||||
}
|
||||
|
||||
impl<A, F, E> MapErrFuture<A, F, E>
|
||||
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,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
type Item = A::Response;
|
||||
type Error = E;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
self.fut.poll().map_err(|e| (self.f)(e))
|
||||
}
|
||||
}
|
||||
|
||||
/// `MapErrNewService` new service combinator
|
||||
pub struct MapErrNewService<A, F, E> {
|
||||
a: A,
|
||||
f: F,
|
||||
e: marker::PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<A, F, E> MapErrNewService<A, F, E>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
/// Create new `MapErr` new service instance
|
||||
pub fn new(a: A, f: F) -> Self {
|
||||
Self { a, f, e: marker::PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> NewService for MapErrNewService<A, F, E>
|
||||
where
|
||||
A: NewService + Clone,
|
||||
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>;
|
||||
|
||||
fn new_service(&self) -> Self::Future {
|
||||
MapErrNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> Clone for MapErrNewService<A, F, E>
|
||||
where
|
||||
A: NewService + Clone,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
a: self.a.clone(),
|
||||
f: self.f.clone(),
|
||||
e: marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapErrNewServiceFuture<A, F, E>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fut: A::Future,
|
||||
a: Option<A::Service>,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<A, F, E> MapErrNewServiceFuture<A, F, E>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(A::Error) -> E,
|
||||
{
|
||||
fn new(fut: A::Future, f: F) -> Self {
|
||||
MapErrNewServiceFuture {
|
||||
f,
|
||||
fut,
|
||||
a: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<A, F, E> Future for MapErrNewServiceFuture<A, F, E>
|
||||
where
|
||||
A: NewService,
|
||||
F: Fn(A::Error) -> E + Clone,
|
||||
{
|
||||
type Item = MapErr<A::Service, F, E>;
|
||||
type Error = A::InitError;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Async::Ready(service) = self.fut.poll()? {
|
||||
Ok(Async::Ready(MapErr::new(service, self.f.clone())))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user