mirror of
https://github.com/actix/actix-web.git
synced 2025-01-21 22:48:07 +00:00
add NewService::from_err combinator
This commit is contained in:
parent
20cff6fc0b
commit
e0f4aac3b4
3 changed files with 115 additions and 5 deletions
|
@ -1,8 +1,8 @@
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
use futures::{Future, Poll};
|
use futures::{Async, Future, Poll};
|
||||||
|
|
||||||
use super::Service;
|
use super::{NewService, Service};
|
||||||
|
|
||||||
/// Service for the `from_err` combinator, changing the error type of a service.
|
/// Service for the `from_err` combinator, changing the error type of a service.
|
||||||
///
|
///
|
||||||
|
@ -77,12 +77,92 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// NewService for the `from_err` combinator, changing the type of a new
|
||||||
|
/// service's error.
|
||||||
|
///
|
||||||
|
/// This is created by the `NewServiceExt::from_err` method.
|
||||||
|
pub struct FromErrNewService<A, E> {
|
||||||
|
a: A,
|
||||||
|
e: PhantomData<E>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, E> FromErrNewService<A, E>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
E: From<A::Error> + From<A::InitError>,
|
||||||
|
{
|
||||||
|
/// Create new `FromErr` new service instance
|
||||||
|
pub fn new(a: A) -> Self {
|
||||||
|
Self { a, e: PhantomData }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, E> Clone for FromErrNewService<A, E>
|
||||||
|
where
|
||||||
|
A: NewService + Clone,
|
||||||
|
E: From<A::Error> + From<A::InitError>,
|
||||||
|
{
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
a: self.a.clone(),
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, E> NewService for FromErrNewService<A, E>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
E: From<A::Error> + From<A::InitError>,
|
||||||
|
{
|
||||||
|
type Request = A::Request;
|
||||||
|
type Response = A::Response;
|
||||||
|
type Error = E;
|
||||||
|
type Service = FromErr<A::Service, E>;
|
||||||
|
|
||||||
|
type InitError = E;
|
||||||
|
type Future = FromErrNewServiceFuture<A, E>;
|
||||||
|
|
||||||
|
fn new_service(&self) -> Self::Future {
|
||||||
|
FromErrNewServiceFuture {
|
||||||
|
fut: self.a.new_service(),
|
||||||
|
e: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct FromErrNewServiceFuture<A, E>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
E: From<A::Error> + From<A::InitError>,
|
||||||
|
{
|
||||||
|
fut: A::Future,
|
||||||
|
e: PhantomData<E>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<A, E> Future for FromErrNewServiceFuture<A, E>
|
||||||
|
where
|
||||||
|
A: NewService,
|
||||||
|
E: From<A::Error> + From<A::InitError>,
|
||||||
|
{
|
||||||
|
type Item = FromErr<A::Service, E>;
|
||||||
|
type Error = E;
|
||||||
|
|
||||||
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||||
|
if let Async::Ready(service) = self.fut.poll()? {
|
||||||
|
Ok(Async::Ready(FromErr::new(service)))
|
||||||
|
} else {
|
||||||
|
Ok(Async::NotReady)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use futures::future::{err, FutureResult};
|
use futures::future::{err, FutureResult};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use service::{Service, ServiceExt};
|
use service::{IntoNewService, NewServiceExt, Service, ServiceExt};
|
||||||
|
|
||||||
struct Srv;
|
struct Srv;
|
||||||
impl Service for Srv {
|
impl Service for Srv {
|
||||||
|
@ -124,4 +204,18 @@ mod tests {
|
||||||
assert!(res.is_err());
|
assert!(res.is_err());
|
||||||
assert_eq!(res.err().unwrap(), Error);
|
assert_eq!(res.err().unwrap(), Error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_new_service() {
|
||||||
|
let blank = || Ok::<_, ()>(Srv);
|
||||||
|
let new_srv = blank.into_new_service().from_err::<Error>();
|
||||||
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
||||||
|
let res = srv.call(()).poll();
|
||||||
|
assert!(res.is_err());
|
||||||
|
assert_eq!(res.err().unwrap(), Error);
|
||||||
|
} else {
|
||||||
|
panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,10 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `MapErrNewService` new service combinator
|
/// NewService for the `map_err` combinator, changing the type of a new
|
||||||
|
/// service's error.
|
||||||
|
///
|
||||||
|
/// This is created by the `NewServiceExt::map_err` method.
|
||||||
pub struct MapErrNewService<A, F, E> {
|
pub struct MapErrNewService<A, F, E> {
|
||||||
a: A,
|
a: A,
|
||||||
f: F,
|
f: F,
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub use self::and_then::{AndThen, AndThenNewService};
|
||||||
pub use self::apply::{Apply, ApplyNewService};
|
pub use self::apply::{Apply, ApplyNewService};
|
||||||
pub use self::fn_service::{FnNewService, FnService};
|
pub use self::fn_service::{FnNewService, FnService};
|
||||||
pub use self::fn_state_service::{FnStateNewService, FnStateService};
|
pub use self::fn_state_service::{FnStateNewService, FnStateService};
|
||||||
pub use self::from_err::FromErr;
|
pub use self::from_err::{FromErr, FromErrNewService};
|
||||||
pub use self::map::{Map, MapNewService};
|
pub use self::map::{Map, MapNewService};
|
||||||
pub use self::map_err::{MapErr, MapErrNewService};
|
pub use self::map_err::{MapErr, MapErrNewService};
|
||||||
pub use self::map_init_err::MapInitErr;
|
pub use self::map_init_err::MapInitErr;
|
||||||
|
@ -133,6 +133,19 @@ pub trait NewServiceExt: NewService {
|
||||||
AndThenNewService::new(self, new_service)
|
AndThenNewService::new(self, new_service)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Map this service's error and new service's init error to any error
|
||||||
|
/// implementing `From` for this service`s `Error`.
|
||||||
|
///
|
||||||
|
/// Note that this function consumes the receiving new service and returns a
|
||||||
|
/// wrapped version of it.
|
||||||
|
fn from_err<E>(self) -> FromErrNewService<Self, E>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
E: From<Self::Error> + From<Self::InitError>,
|
||||||
|
{
|
||||||
|
FromErrNewService::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
|
fn map<F, R>(self, f: F) -> MapNewService<Self, F, R>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
|
|
Loading…
Reference in a new issue