2018-08-25 18:02:14 +02:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
use tower_service::{NewService, Service};
|
|
|
|
|
2018-09-04 18:49:21 +02:00
|
|
|
use super::IntoNewService;
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
/// `AndThen` service combinator
|
|
|
|
pub struct AndThen<A, B> {
|
|
|
|
a: A,
|
|
|
|
b: Rc<RefCell<B>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> AndThen<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
/// Create new `AndThen` combinator
|
|
|
|
pub fn new(a: A, b: B) -> Self {
|
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
b: Rc::new(RefCell::new(b)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:54:15 +02:00
|
|
|
impl<A, B> Clone for AndThen<A, B>
|
|
|
|
where
|
|
|
|
A: Service + Clone,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
AndThen {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:02:14 +02:00
|
|
|
impl<A, B> Service for AndThen<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = B::Response;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::Error;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Future = AndThenFuture<A, B>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
match self.a.poll_ready() {
|
2018-09-04 21:07:13 +02:00
|
|
|
Ok(Async::Ready(_)) => self.b.borrow_mut().poll_ready().map_err(|e| e.into()),
|
2018-08-25 18:02:14 +02:00
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-09-04 21:07:13 +02:00
|
|
|
Err(err) => Err(err),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
AndThenFuture::new(self.a.call(req), self.b.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AndThenFuture<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
b: Rc<RefCell<B>>,
|
|
|
|
fut_b: Option<B::Future>,
|
|
|
|
fut_a: A::Future,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> AndThenFuture<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn new(fut_a: A::Future, b: Rc<RefCell<B>>) -> Self {
|
|
|
|
AndThenFuture {
|
|
|
|
b,
|
|
|
|
fut_a,
|
|
|
|
fut_b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Future for AndThenFuture<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
|
|
|
B::Error: Into<A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Item = B::Response;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::Error;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Some(ref mut fut) = self.fut_b {
|
2018-09-04 21:07:13 +02:00
|
|
|
return fut.poll().map_err(|e| e.into());
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
match self.fut_a.poll() {
|
|
|
|
Ok(Async::Ready(resp)) => {
|
|
|
|
self.fut_b = Some(self.b.borrow_mut().call(resp));
|
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
2018-09-04 21:07:13 +02:00
|
|
|
Err(err) => Err(err),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `AndThenNewService` new service combinator
|
|
|
|
pub struct AndThenNewService<A, B> {
|
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> AndThenNewService<A, B>
|
|
|
|
where
|
|
|
|
A: NewService,
|
|
|
|
B: NewService,
|
|
|
|
{
|
|
|
|
/// Create new `AndThen` combinator
|
2018-09-04 18:49:21 +02:00
|
|
|
pub fn new<F: IntoNewService<B>>(a: A, f: F) -> Self {
|
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
b: f.into_new_service(),
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> NewService for AndThenNewService<A, B>
|
|
|
|
where
|
2018-09-04 21:07:13 +02:00
|
|
|
A: NewService,
|
|
|
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = B::Response;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::Error;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Service = AndThen<A::Service, B::Service>;
|
|
|
|
|
2018-09-04 21:07:13 +02:00
|
|
|
type InitError = A::InitError;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Future = AndThenNewServiceFuture<A, B>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
AndThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Clone for AndThenNewService<A, B>
|
|
|
|
where
|
2018-09-04 21:07:13 +02:00
|
|
|
A: NewService + Clone,
|
|
|
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError> + Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct AndThenNewServiceFuture<A, B>
|
|
|
|
where
|
|
|
|
A: NewService,
|
|
|
|
B: NewService,
|
|
|
|
{
|
|
|
|
fut_b: B::Future,
|
|
|
|
fut_a: A::Future,
|
|
|
|
a: Option<A::Service>,
|
|
|
|
b: Option<B::Service>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> AndThenNewServiceFuture<A, B>
|
|
|
|
where
|
|
|
|
A: NewService,
|
|
|
|
B: NewService,
|
|
|
|
{
|
|
|
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
|
|
|
AndThenNewServiceFuture {
|
|
|
|
fut_a,
|
|
|
|
fut_b,
|
|
|
|
a: None,
|
|
|
|
b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Future for AndThenNewServiceFuture<A, B>
|
|
|
|
where
|
|
|
|
A: NewService,
|
2018-09-04 21:07:13 +02:00
|
|
|
B: NewService<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Item = AndThen<A::Service, B::Service>;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::InitError;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if self.a.is_none() {
|
2018-09-04 21:07:13 +02:00
|
|
|
if let Async::Ready(service) = self.fut_a.poll()? {
|
2018-08-25 18:02:14 +02:00
|
|
|
self.a = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.b.is_none() {
|
2018-09-04 21:07:13 +02:00
|
|
|
if let Async::Ready(service) = self.fut_b.poll().map_err(|e| e.into())? {
|
2018-08-25 18:02:14 +02:00
|
|
|
self.b = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.a.is_some() && self.b.is_some() {
|
|
|
|
Ok(Async::Ready(AndThen::new(
|
|
|
|
self.a.take().unwrap(),
|
|
|
|
self.b.take().unwrap(),
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|