2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use super::{Service, ServiceFactory};
|
2018-12-06 23:04:42 +01:00
|
|
|
use crate::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.
|
2019-12-05 07:37:26 +01:00
|
|
|
pub struct AndThenService<A, B>(Cell<(A, B)>);
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> AndThenService<A, B> {
|
2018-08-25 18:02:14 +02:00
|
|
|
/// Create new `AndThen` combinator
|
2019-11-18 09:30:04 +01:00
|
|
|
pub(crate) fn new(a: A, b: B) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
2019-12-05 07:37:26 +01:00
|
|
|
Self(Cell::new((a, b)))
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
impl<A, B> Clone for AndThenService<A, B> {
|
2018-09-05 22:54:15 +02:00
|
|
|
fn clone(&self) -> Self {
|
2019-12-05 07:37:26 +01:00
|
|
|
AndThenService(self.0.clone())
|
2018-09-05 22:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> Service for AndThenService<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
2018-08-25 18:02:14 +02:00
|
|
|
type Response = B::Response;
|
2018-09-04 21:07:13 +02:00
|
|
|
type Error = A::Error;
|
2019-11-18 09:30:04 +01:00
|
|
|
type Future = AndThenServiceResponse<A, B>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-12-05 07:37:26 +01:00
|
|
|
let srv = self.0.get_mut();
|
|
|
|
|
|
|
|
let not_ready = !srv.0.poll_ready(cx)?.is_ready();
|
|
|
|
if !srv.1.poll_ready(cx)?.is_ready() || not_ready {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending
|
2019-03-12 23:15:14 +01:00
|
|
|
} else {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(()))
|
2019-03-12 23:15:14 +01:00
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
2019-12-05 07:37:26 +01:00
|
|
|
AndThenServiceResponse {
|
|
|
|
state: State::A(self.0.get_mut().0.call(req), self.0.clone()),
|
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
|
|
|
pub struct AndThenServiceResponse<A, B>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
state: State<A, B>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
|
|
|
enum State<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-12-05 07:37:26 +01:00
|
|
|
A(#[pin] A::Future, Cell<(A, B)>),
|
|
|
|
B(#[pin] B::Future),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> Future for AndThenServiceResponse<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
|
|
|
B: Service<Request = A::Response, Error = A::Error>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<B::Response, A::Error>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::project]
|
2019-11-19 09:51:40 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[project]
|
|
|
|
match this.state.as_mut().project() {
|
|
|
|
State::A(fut, b) => match fut.poll(cx)? {
|
|
|
|
Poll::Ready(res) => {
|
|
|
|
let fut = b.get_mut().1.call(res);
|
|
|
|
this.state.set(State::B(fut));
|
|
|
|
self.poll(cx)
|
2019-11-14 13:38:24 +01:00
|
|
|
}
|
2019-12-05 07:37:26 +01:00
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
},
|
|
|
|
State::B(fut) => fut.poll(cx),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
/// `.and_then()` service factory combinator
|
|
|
|
pub struct AndThenServiceFactory<A, B>
|
2019-03-12 21:50:14 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-03 13:32:02 +01:00
|
|
|
A::Config: Clone,
|
|
|
|
B: ServiceFactory<
|
|
|
|
Config = A::Config,
|
|
|
|
Request = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2019-03-12 21:50:14 +01:00
|
|
|
{
|
2018-08-25 18:02:14 +02:00
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> AndThenServiceFactory<A, B>
|
2019-03-12 21:50:14 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-02 16:27:48 +01:00
|
|
|
A::Config: Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
B: ServiceFactory<
|
2019-05-12 15:03:50 +02:00
|
|
|
Config = A::Config,
|
|
|
|
Request = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2019-03-12 21:50:14 +01:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
/// Create new `AndThenFactory` combinator
|
2019-12-02 16:27:48 +01:00
|
|
|
pub(crate) fn new(a: A, b: B) -> Self {
|
2019-11-14 13:38:24 +01:00
|
|
|
Self { a, b }
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> ServiceFactory for AndThenServiceFactory<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-02 16:27:48 +01:00
|
|
|
A::Config: Clone,
|
2019-11-14 13:38:24 +01:00
|
|
|
B: ServiceFactory<
|
2019-05-12 15:03:50 +02:00
|
|
|
Config = A::Config,
|
|
|
|
Request = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
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
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = A::Config;
|
2019-11-18 09:30:04 +01:00
|
|
|
type Service = AndThenService<A::Service, B::Service>;
|
2018-09-04 21:07:13 +02:00
|
|
|
type InitError = A::InitError;
|
2019-11-18 09:30:04 +01:00
|
|
|
type Future = AndThenServiceFactoryResponse<A, B>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
|
|
|
AndThenServiceFactoryResponse::new(
|
|
|
|
self.a.new_service(cfg.clone()),
|
|
|
|
self.b.new_service(cfg),
|
|
|
|
)
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> Clone for AndThenServiceFactory<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory + Clone,
|
2019-12-03 13:32:02 +01:00
|
|
|
A::Config: Clone,
|
|
|
|
B: ServiceFactory<
|
|
|
|
Config = A::Config,
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
|
|
|
pub struct AndThenServiceFactoryResponse<A, B>
|
|
|
|
where
|
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Request = A::Response>,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
fut_a: A::Future,
|
|
|
|
#[pin]
|
|
|
|
fut_b: B::Future,
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
a: Option<A::Service>,
|
|
|
|
b: Option<B::Service>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> AndThenServiceFactoryResponse<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Request = A::Response>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-03-05 06:35:47 +01:00
|
|
|
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
|
2019-11-18 09:30:04 +01:00
|
|
|
AndThenServiceFactoryResponse {
|
2018-08-25 18:02:14 +02:00
|
|
|
fut_a,
|
|
|
|
fut_b,
|
|
|
|
a: None,
|
|
|
|
b: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
impl<A, B> Future for AndThenServiceFactoryResponse<A, B>
|
2018-08-25 18:02:14 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
2019-11-18 09:30:04 +01:00
|
|
|
type Output = Result<AndThenService<A::Service, B::Service>, A::InitError>;
|
2018-08-25 18:02:14 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-19 09:51:40 +01:00
|
|
|
let this = self.project();
|
2019-11-18 09:30:04 +01:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
if this.a.is_none() {
|
2019-11-19 09:51:40 +01:00
|
|
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
|
|
|
*this.a = Some(service);
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
if this.b.is_none() {
|
2019-11-19 09:51:40 +01:00
|
|
|
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
|
|
|
|
*this.b = Some(service);
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 13:38:24 +01:00
|
|
|
if this.a.is_some() && this.b.is_some() {
|
2019-11-18 09:30:04 +01:00
|
|
|
Poll::Ready(Ok(AndThenService::new(
|
2019-11-14 13:38:24 +01:00
|
|
|
this.a.take().unwrap(),
|
|
|
|
this.b.take().unwrap(),
|
2018-08-25 18:02:14 +02:00
|
|
|
)))
|
|
|
|
} else {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::cell::Cell;
|
|
|
|
use std::rc::Rc;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
use futures_util::future::{lazy, ok, ready, Ready};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{factory_fn, pipeline, pipeline_factory, Service, ServiceFactory};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv1(Rc<Cell<usize>>);
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl Service for Srv1 {
|
|
|
|
type Request = &'static str;
|
2018-09-12 22:34:53 +02:00
|
|
|
type Response = &'static str;
|
|
|
|
type Error = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<Self::Response, ()>>;
|
2018-09-12 22:34:53 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2018-09-12 22:34:53 +02:00
|
|
|
self.0.set(self.0.get() + 1);
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(()))
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 04:17:02 +01:00
|
|
|
fn call(&mut self, req: &'static str) -> Self::Future {
|
2018-09-12 22:34:53 +02:00
|
|
|
ok(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv2(Rc<Cell<usize>>);
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl Service for Srv2 {
|
|
|
|
type Request = &'static str;
|
2018-09-12 22:34:53 +02:00
|
|
|
type Response = (&'static str, &'static str);
|
|
|
|
type Error = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<Self::Response, ()>>;
|
2018-09-12 22:34:53 +02:00
|
|
|
|
2019-12-02 17:30:09 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2018-09-12 22:34:53 +02:00
|
|
|
self.0.set(self.0.get() + 1);
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(()))
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 04:17:02 +01:00
|
|
|
fn call(&mut self, req: &'static str) -> Self::Future {
|
2018-09-12 22:34:53 +02:00
|
|
|
ok((req, "srv2"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_poll_ready() {
|
2018-09-12 22:34:53 +02:00
|
|
|
let cnt = Rc::new(Cell::new(0));
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt.clone()));
|
|
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
|
|
assert_eq!(res, Poll::Ready(Ok(())));
|
2018-09-12 22:34:53 +02:00
|
|
|
assert_eq!(cnt.get(), 2);
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_call() {
|
2018-09-12 22:34:53 +02:00
|
|
|
let cnt = Rc::new(Cell::new(0));
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut srv = pipeline(Srv1(cnt.clone())).and_then(Srv2(cnt));
|
|
|
|
let res = srv.call("srv1").await;
|
2018-09-12 22:34:53 +02:00
|
|
|
assert!(res.is_ok());
|
2019-11-14 13:38:24 +01:00
|
|
|
assert_eq!(res.unwrap(), (("srv1", "srv2")));
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_new_service() {
|
2018-09-18 04:21:24 +02:00
|
|
|
let cnt = Rc::new(Cell::new(0));
|
|
|
|
let cnt2 = cnt.clone();
|
2019-11-14 13:38:24 +01:00
|
|
|
let new_srv =
|
|
|
|
pipeline_factory(factory_fn(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
|
|
|
|
.and_then(move || ready(Ok(Srv2(cnt.clone()))));
|
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
let mut srv = new_srv.new_service(()).await.unwrap();
|
2019-11-14 13:38:24 +01:00
|
|
|
let res = srv.call("srv1").await;
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), ("srv1", "srv2"));
|
2018-09-18 04:21:24 +02:00
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|