2018-08-25 18:02:14 +02:00
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
|
2018-09-11 18:30:22 +02:00
|
|
|
use super::{IntoNewService, NewService, Service};
|
2018-10-03 07:18:07 +02:00
|
|
|
use cell::Cell;
|
2018-09-04 18:49:21 +02:00
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Service for the `and_then` combinator, chaining a computation onto the end
|
|
|
|
/// of another service which completes successfully.
|
|
|
|
///
|
|
|
|
/// This is created by the `ServiceExt::and_then` method.
|
2018-08-25 18:02:14 +02:00
|
|
|
pub struct AndThen<A, B> {
|
|
|
|
a: A,
|
2018-10-03 07:18:07 +02:00
|
|
|
b: Cell<B>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B> AndThen<A, B> {
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `AndThen` combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request>(a: A, b: B) -> Self
|
|
|
|
where
|
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
|
|
|
{
|
2018-10-03 07:18:07 +02:00
|
|
|
Self { a, b: Cell::new(b) }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 22:54:15 +02:00
|
|
|
impl<A, B> Clone for AndThen<A, B>
|
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
B: Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
AndThen {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Service<Request> for AndThen<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
type Response = B::Response;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = AndThenFuture<A, B, Request>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-10-03 06:47:50 +02:00
|
|
|
let _ = try_ready!(self.a.poll_ready());
|
|
|
|
self.b.borrow_mut().poll_ready()
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2018-08-25 18:02:14 +02:00
|
|
|
AndThenFuture::new(self.a.call(req), self.b.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct AndThenFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2018-10-03 07:18:07 +02:00
|
|
|
b: Cell<B>,
|
2018-08-25 18:02:14 +02:00
|
|
|
fut_b: Option<B::Future>,
|
|
|
|
fut_a: A::Future,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> AndThenFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2018-10-03 07:18:07 +02:00
|
|
|
fn new(fut_a: A::Future, b: Cell<B>) -> Self {
|
2018-08-25 18:02:14 +02:00
|
|
|
AndThenFuture {
|
|
|
|
b,
|
|
|
|
fut_a,
|
|
|
|
fut_b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Future for AndThenFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<A::Response, Error = A::Error>,
|
2018-09-04 21:07:13 +02:00
|
|
|
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-07 23:34:31 +02:00
|
|
|
return fut.poll();
|
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,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B> AndThenNewService<A, B> {
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `AndThen` combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request, F: IntoNewService<B, A::Response>>(a: A, f: F) -> Self
|
|
|
|
where
|
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
|
|
|
|
{
|
2018-09-04 18:49:21 +02:00
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
b: f.into_new_service(),
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> NewService<Request> for AndThenNewService<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<A::Response, Error = A::Error, InitError = A::InitError>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
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-11-30 03:56:15 +01:00
|
|
|
type Future = AndThenNewServiceFuture<A, B, Request>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
|
|
|
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-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
B: Clone,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct AndThenNewServiceFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<A::Response>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fut_b: B::Future,
|
|
|
|
fut_a: A::Future,
|
|
|
|
a: Option<A::Service>,
|
|
|
|
b: Option<B::Service>,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> AndThenNewServiceFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<A::Response>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
|
|
|
AndThenNewServiceFuture {
|
|
|
|
fut_a,
|
|
|
|
fut_b,
|
|
|
|
a: None,
|
|
|
|
b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Future for AndThenNewServiceFuture<A, B, Request>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<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-07 23:34:31 +02:00
|
|
|
if let Async::Ready(service) = self.fut_b.poll()? {
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Async, Poll};
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
use super::*;
|
2018-09-18 04:21:24 +02:00
|
|
|
use service::{NewServiceExt, Service, ServiceExt};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv1(Rc<Cell<usize>>);
|
|
|
|
impl Service for Srv1 {
|
|
|
|
type Request = &'static str;
|
|
|
|
type Response = &'static str;
|
|
|
|
type Error = ();
|
|
|
|
type Future = FutureResult<Self::Response, ()>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.0.set(self.0.get() + 1);
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
ok(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv2(Rc<Cell<usize>>);
|
|
|
|
|
|
|
|
impl Service for Srv2 {
|
|
|
|
type Request = &'static str;
|
|
|
|
type Response = (&'static str, &'static str);
|
|
|
|
type Error = ();
|
|
|
|
type Future = FutureResult<Self::Response, ()>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.0.set(self.0.get() + 1);
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
ok((req, "srv2"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_ready() {
|
|
|
|
let cnt = Rc::new(Cell::new(0));
|
|
|
|
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt.clone()));
|
|
|
|
let res = srv.poll_ready();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(()));
|
|
|
|
assert_eq!(cnt.get(), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call() {
|
|
|
|
let cnt = Rc::new(Cell::new(0));
|
|
|
|
let mut srv = Srv1(cnt.clone()).and_then(Srv2(cnt));
|
|
|
|
let res = srv.call("srv1").poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2")));
|
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
|
|
|
let cnt = Rc::new(Cell::new(0));
|
|
|
|
let cnt2 = cnt.clone();
|
|
|
|
let blank = move || Ok::<_, ()>(Srv1(cnt2.clone()));
|
|
|
|
let new_srv = blank
|
|
|
|
.into_new_service()
|
|
|
|
.and_then(move || Ok(Srv2(cnt.clone())));
|
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
|
|
|
let res = srv.call("srv1").poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv1", "srv2")));
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|