2018-12-06 23:04:42 +01:00
|
|
|
use futures::{try_ready, Async, Future, Poll};
|
2018-10-03 06:47:50 +02:00
|
|
|
|
|
|
|
use super::{IntoNewService, NewService, Service};
|
2018-12-06 23:04:42 +01:00
|
|
|
use crate::cell::Cell;
|
2018-10-03 06:47:50 +02:00
|
|
|
|
|
|
|
/// Service for the `then` combinator, chaining a computation onto the end of
|
|
|
|
/// another service.
|
|
|
|
///
|
|
|
|
/// This is created by the `ServiceExt::then` method.
|
|
|
|
pub struct Then<A, B> {
|
|
|
|
a: A,
|
2018-10-03 07:18:07 +02:00
|
|
|
b: Cell<B>,
|
2018-10-03 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B> Then<A, B> {
|
2018-10-03 06:47:50 +02:00
|
|
|
/// Create new `Then` combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request>(a: A, b: B) -> Then<A, B>
|
|
|
|
where
|
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
|
|
|
|
{
|
2018-10-03 07:18:07 +02:00
|
|
|
Then { a, b: Cell::new(b) }
|
2018-10-03 06:47:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Clone for Then<A, B>
|
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Then {
|
|
|
|
a: self.a.clone(),
|
|
|
|
b: self.b.clone(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Service<Request> for Then<A, B>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<Result<A::Response, A::Error>, Error = A::Error>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
type Response = B::Response;
|
|
|
|
type Error = B::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = ThenFuture<A, B, Request>;
|
2018-10-03 06:47:50 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-12-06 23:04:42 +01:00
|
|
|
try_ready!(self.a.poll_ready());
|
2018-12-09 18:56:23 +01:00
|
|
|
self.b.get_mut().poll_ready()
|
2018-10-03 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2018-10-03 06:47:50 +02:00
|
|
|
ThenFuture::new(self.a.call(req), self.b.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct ThenFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<Result<A::Response, A::Error>>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
2018-10-03 07:18:07 +02:00
|
|
|
b: Cell<B>,
|
2018-10-03 06:47:50 +02:00
|
|
|
fut_b: Option<B::Future>,
|
2018-12-13 03:00:35 +01:00
|
|
|
fut_a: Option<A::Future>,
|
2018-10-03 06:47:50 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> ThenFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<Result<A::Response, A::Error>>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
2018-12-13 03:00:35 +01:00
|
|
|
fn new(a: A::Future, b: Cell<B>) -> Self {
|
2018-10-03 06:47:50 +02:00
|
|
|
ThenFuture {
|
|
|
|
b,
|
2018-12-13 03:00:35 +01:00
|
|
|
fut_a: Some(a),
|
2018-10-03 06:47:50 +02:00
|
|
|
fut_b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Future for ThenFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
B: Service<Result<A::Response, A::Error>>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
type Item = B::Response;
|
|
|
|
type Error = B::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Some(ref mut fut) = self.fut_b {
|
|
|
|
return fut.poll();
|
|
|
|
}
|
|
|
|
|
2018-12-13 03:00:35 +01:00
|
|
|
match self.fut_a.as_mut().expect("bug in actix-service").poll() {
|
2018-10-03 06:47:50 +02:00
|
|
|
Ok(Async::Ready(resp)) => {
|
2018-12-13 03:00:35 +01:00
|
|
|
let _ = self.fut_a.take();
|
2018-12-09 18:56:23 +01:00
|
|
|
self.fut_b = Some(self.b.get_mut().call(Ok(resp)));
|
2018-10-03 06:47:50 +02:00
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
Err(err) => {
|
2018-12-13 03:00:35 +01:00
|
|
|
let _ = self.fut_a.take();
|
2018-12-09 18:56:23 +01:00
|
|
|
self.fut_b = Some(self.b.get_mut().call(Err(err)));
|
2018-10-03 06:47:50 +02:00
|
|
|
self.poll()
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `ThenNewService` new service combinator
|
|
|
|
pub struct ThenNewService<A, B> {
|
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B> ThenNewService<A, B> {
|
2018-10-03 06:47:50 +02:00
|
|
|
/// Create new `AndThen` combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<F, Request>(a: A, f: F) -> Self
|
|
|
|
where
|
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<
|
|
|
|
Result<A::Response, A::Error>,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
|
|
|
F: IntoNewService<B, Result<A::Response, A::Error>>,
|
|
|
|
{
|
2018-10-03 06:47:50 +02:00
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
b: f.into_new_service(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> NewService<Request> for ThenNewService<A, B>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
type Response = B::Response;
|
|
|
|
type Error = A::Error;
|
|
|
|
type Service = Then<A::Service, B::Service>;
|
|
|
|
|
|
|
|
type InitError = A::InitError;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = ThenNewServiceFuture<A, B, Request>;
|
2018-10-03 06:47:50 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
ThenNewServiceFuture::new(self.a.new_service(), self.b.new_service())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B> Clone for ThenNewService<A, B>
|
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
B: Clone,
|
2018-10-03 06:47:50 +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 ThenNewServiceFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
2018-10-03 06:47:50 +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> ThenNewServiceFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
|
|
|
ThenNewServiceFuture {
|
|
|
|
fut_a,
|
|
|
|
fut_b,
|
|
|
|
a: None,
|
|
|
|
b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, B, Request> Future for ThenNewServiceFuture<A, B, Request>
|
2018-10-03 06:47:50 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
B: NewService<Result<A::Response, A::Error>, Error = A::Error, InitError = A::InitError>,
|
2018-10-03 06:47:50 +02:00
|
|
|
{
|
|
|
|
type Item = Then<A::Service, B::Service>;
|
|
|
|
type Error = A::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if self.a.is_none() {
|
|
|
|
if let Async::Ready(service) = self.fut_a.poll()? {
|
|
|
|
self.a = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.b.is_none() {
|
|
|
|
if let Async::Ready(service) = self.fut_b.poll()? {
|
|
|
|
self.b = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.a.is_some() && self.b.is_some() {
|
|
|
|
Ok(Async::Ready(Then::new(
|
|
|
|
self.a.take().unwrap(),
|
|
|
|
self.b.take().unwrap(),
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{err, ok, FutureResult};
|
2018-12-13 03:56:39 +01:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-10-03 06:47:50 +02:00
|
|
|
use std::cell::Cell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2018-12-13 03:56:39 +01:00
|
|
|
use crate::{IntoNewService, NewService, Service, ServiceExt};
|
2018-10-03 06:47:50 +02:00
|
|
|
|
2018-10-03 07:18:07 +02:00
|
|
|
#[derive(Clone)]
|
2018-10-03 06:47:50 +02:00
|
|
|
struct Srv1(Rc<Cell<usize>>);
|
2018-11-30 04:17:02 +01:00
|
|
|
impl Service<Result<&'static str, &'static str>> for Srv1 {
|
2018-10-03 06:47:50 +02:00
|
|
|
type Response = &'static str;
|
|
|
|
type Error = ();
|
|
|
|
type Future = FutureResult<Self::Response, Self::Error>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.0.set(self.0.get() + 1);
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 04:17:02 +01:00
|
|
|
fn call(&mut self, req: Result<&'static str, &'static str>) -> Self::Future {
|
2018-10-03 06:47:50 +02:00
|
|
|
match req {
|
|
|
|
Ok(msg) => ok(msg),
|
|
|
|
Err(_) => err(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Srv2(Rc<Cell<usize>>);
|
|
|
|
|
2018-11-30 04:17:02 +01:00
|
|
|
impl Service<Result<&'static str, ()>> for Srv2 {
|
2018-10-03 06:47:50 +02:00
|
|
|
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(()))
|
|
|
|
}
|
|
|
|
|
2018-11-30 04:17:02 +01:00
|
|
|
fn call(&mut self, req: Result<&'static str, ()>) -> Self::Future {
|
2018-10-03 06:47:50 +02:00
|
|
|
match req {
|
|
|
|
Ok(msg) => ok((msg, "ok")),
|
|
|
|
Err(()) => ok(("srv2", "err")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_ready() {
|
|
|
|
let cnt = Rc::new(Cell::new(0));
|
|
|
|
let mut srv = Srv1(cnt.clone()).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));
|
2018-10-03 07:18:07 +02:00
|
|
|
let mut srv = Srv1(cnt.clone()).then(Srv2(cnt)).clone();
|
2018-10-03 06:47:50 +02:00
|
|
|
|
|
|
|
let res = srv.call(Ok("srv1")).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok")));
|
|
|
|
|
|
|
|
let res = srv.call(Err("srv")).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv2", "err")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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().then(move || Ok(Srv2(cnt.clone())));
|
2018-10-03 07:18:07 +02:00
|
|
|
if let Async::Ready(mut srv) = new_srv.clone().new_service().poll().unwrap() {
|
2018-10-03 06:47:50 +02:00
|
|
|
let res = srv.call(Ok("srv1")).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv1", "ok")));
|
|
|
|
|
|
|
|
let res = srv.call(Err("srv")).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv2", "err")));
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|