1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 22:00:07 +01:00

127 lines
3.7 KiB
Rust
Raw Normal View History

2018-09-27 17:05:48 -07:00
//! Contains `Either` service and related types and functions.
2018-12-09 10:15:49 -08:00
use actix_service::{NewService, Service};
2019-03-04 20:37:03 -08:00
use futures::{future, try_ready, Async, Future, IntoFuture, Poll};
2018-09-27 17:05:48 -07:00
/// Combine two different service types into a single type.
///
/// Both services must be of the same request, response, and error types.
/// `EitherService` is useful for handling conditional branching in service
/// middleware to different inner service types.
pub enum EitherService<A, B> {
A(A),
B(B),
}
2019-01-16 15:00:08 -08:00
impl<A: Clone, B: Clone> Clone for EitherService<A, B> {
fn clone(&self) -> Self {
match self {
EitherService::A(srv) => EitherService::A(srv.clone()),
EitherService::B(srv) => EitherService::B(srv.clone()),
}
}
}
2019-03-05 07:35:26 -08:00
impl<A, B, R> Service<R> for EitherService<A, B>
2018-09-27 17:05:48 -07:00
where
2019-03-05 07:35:26 -08:00
A: Service<R>,
B: Service<R, Response = A::Response, Error = A::Error>,
2018-09-27 17:05:48 -07:00
{
type Response = A::Response;
type Error = A::Error;
type Future = future::Either<A::Future, B::Future>;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
match self {
EitherService::A(ref mut inner) => inner.poll_ready(),
EitherService::B(ref mut inner) => inner.poll_ready(),
}
}
2019-03-05 07:35:26 -08:00
fn call(&mut self, req: R) -> Self::Future {
2018-09-27 17:05:48 -07:00
match self {
EitherService::A(ref mut inner) => future::Either::A(inner.call(req)),
EitherService::B(ref mut inner) => future::Either::B(inner.call(req)),
}
}
}
/// Combine two different new service types into a single type.
pub enum Either<A, B> {
A(A),
B(B),
}
impl<A, B> Either<A, B> {
2019-03-05 07:35:26 -08:00
pub fn new_a<R, C>(srv: A) -> Self
where
2019-03-05 07:35:26 -08:00
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
Either::A(srv)
}
2019-03-05 07:35:26 -08:00
pub fn new_b<R, C>(srv: B) -> Self
where
2019-03-05 07:35:26 -08:00
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
{
Either::B(srv)
}
}
2019-03-05 07:35:26 -08:00
impl<A, B, R, C> NewService<R, C> for Either<A, B>
2018-09-27 17:05:48 -07:00
where
2019-03-05 07:35:26 -08:00
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
2018-09-27 17:05:48 -07:00
{
type Response = A::Response;
type Error = A::Error;
type InitError = A::InitError;
type Service = EitherService<A::Service, B::Service>;
2019-03-05 07:35:26 -08:00
type Future = EitherNewService<A, B, R, C>;
2018-09-27 17:05:48 -07:00
2019-02-22 12:44:37 -08:00
fn new_service(&self, cfg: &C) -> Self::Future {
2018-09-27 17:05:48 -07:00
match self {
2019-03-04 21:35:47 -08:00
Either::A(ref inner) => EitherNewService::A(inner.new_service(cfg)),
Either::B(ref inner) => EitherNewService::B(inner.new_service(cfg)),
2018-09-27 17:05:48 -07:00
}
}
}
2019-01-16 15:00:08 -08:00
impl<A: Clone, B: Clone> Clone for Either<A, B> {
fn clone(&self) -> Self {
match self {
Either::A(srv) => Either::A(srv.clone()),
Either::B(srv) => Either::B(srv.clone()),
}
}
}
2018-09-27 17:05:48 -07:00
#[doc(hidden)]
2019-03-05 07:35:26 -08:00
pub enum EitherNewService<A: NewService<R, C>, B: NewService<R, C>, R, C> {
2019-03-04 20:37:03 -08:00
A(<A::Future as IntoFuture>::Future),
B(<B::Future as IntoFuture>::Future),
2018-09-27 17:05:48 -07:00
}
2019-03-05 07:35:26 -08:00
impl<A, B, R, C> Future for EitherNewService<A, B, R, C>
2018-09-27 17:05:48 -07:00
where
2019-03-05 07:35:26 -08:00
A: NewService<R, C>,
B: NewService<R, C, Response = A::Response, Error = A::Error, InitError = A::InitError>,
2018-09-27 17:05:48 -07:00
{
type Item = EitherService<A::Service, B::Service>;
type Error = A::InitError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self {
EitherNewService::A(ref mut fut) => {
let service = try_ready!(fut.poll());
Ok(Async::Ready(EitherService::A(service)))
}
EitherNewService::B(ref mut fut) => {
let service = try_ready!(fut.poll());
Ok(Async::Ready(EitherService::B(service)))
}
}
}
}