2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2020-01-17 01:58:11 +01:00
|
|
|
use std::rc::Rc;
|
2019-11-14 13:38:24 +01:00
|
|
|
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.
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) 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
|
|
|
{
|
2020-01-17 01:58:11 +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 {
|
2020-01-17 01:58:11 +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>> {
|
2020-01-17 01:58:11 +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 {
|
2020-01-17 01:58:11 +01:00
|
|
|
state: State::A(self.0.get_mut().0.call(req), Some(self.0.clone())),
|
2019-12-05 07:37:26 +01:00
|
|
|
}
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenServiceResponse<A, B>
|
2019-12-05 07:37:26 +01:00
|
|
|
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
|
|
|
{
|
2020-01-17 01:58:11 +01:00
|
|
|
A(#[pin] A::Future, Option<Cell<(A, B)>>),
|
2019-12-05 07:37:26 +01:00
|
|
|
B(#[pin] B::Future),
|
2019-12-06 05:34:44 +01:00
|
|
|
Empty,
|
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) => {
|
2019-12-06 05:34:44 +01:00
|
|
|
let mut b = b.take().unwrap();
|
|
|
|
this.state.set(State::Empty); // drop fut A
|
2020-01-17 01:58:11 +01:00
|
|
|
let fut = b.get_mut().1.call(res);
|
2019-12-05 07:37:26 +01:00
|
|
|
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,
|
|
|
|
},
|
2019-12-06 05:34:44 +01:00
|
|
|
State::B(fut) => fut.poll(cx).map(|r| {
|
|
|
|
this.state.set(State::Empty);
|
|
|
|
r
|
|
|
|
}),
|
|
|
|
State::Empty => panic!("future must not be polled after it returned `Poll::Ready`"),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 09:30:04 +01:00
|
|
|
/// `.and_then()` service factory combinator
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) 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
|
|
|
{
|
2020-01-17 01:58:11 +01:00
|
|
|
inner: Rc<(A, B)>,
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2020-01-17 01:58:11 +01:00
|
|
|
Self {
|
|
|
|
inner: Rc::new((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 {
|
2020-01-17 01:58:11 +01:00
|
|
|
let inner = &*self.inner;
|
2019-12-02 16:27:48 +01:00
|
|
|
AndThenServiceFactoryResponse::new(
|
2020-01-17 01:58:11 +01:00
|
|
|
inner.0.new_service(cfg.clone()),
|
|
|
|
inner.1.new_service(cfg),
|
2019-12-02 16:27:48 +01:00
|
|
|
)
|
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
|
2020-01-17 01:58:11 +01:00
|
|
|
A: ServiceFactory,
|
2019-12-03 13:32:02 +01:00
|
|
|
A::Config: Clone,
|
|
|
|
B: ServiceFactory<
|
2020-01-17 01:58:11 +01:00
|
|
|
Config = A::Config,
|
|
|
|
Request = A::Response,
|
|
|
|
Error = A::Error,
|
|
|
|
InitError = A::InitError,
|
|
|
|
>,
|
2018-08-25 18:02:14 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2020-01-17 01:58:11 +01:00
|
|
|
inner: self.inner.clone(),
|
2018-08-25 18:02:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenServiceFactoryResponse<A, B>
|
2019-12-05 07:37:26 +01:00
|
|
|
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-12-08 14:05:05 +01:00
|
|
|
use crate::{fn_factory, 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());
|
2020-01-28 12:27:33 +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 =
|
2019-12-08 14:05:05 +01:00
|
|
|
pipeline_factory(fn_factory(move || ready(Ok::<_, ()>(Srv1(cnt2.clone())))))
|
2019-11-14 13:38:24 +01:00
|
|
|
.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
|
|
|
}
|