2019-12-03 14:59:28 +01:00
|
|
|
use std::future::Future;
|
|
|
|
use std::marker::PhantomData;
|
|
|
|
use std::pin::Pin;
|
2020-01-17 01:58:11 +01:00
|
|
|
use std::rc::Rc;
|
2019-12-03 14:59:28 +01:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
|
|
|
use crate::cell::Cell;
|
|
|
|
use crate::{Service, ServiceFactory};
|
|
|
|
|
|
|
|
/// `Apply` service combinator
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenApplyFn<A, B, F, Fut, Res, Err>
|
2019-12-03 14:59:28 +01:00
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: Cell<(A, B, F)>,
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData<(Fut, Res, Err)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
|
|
|
pub(crate) fn new(a: A, b: B, f: F) -> Self {
|
|
|
|
Self {
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: Cell::new((a, b, f)),
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> Clone for AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
2020-01-17 01:58:11 +01:00
|
|
|
A: Service,
|
2019-12-03 14:59:28 +01:00
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
AndThenApplyFn {
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: self.srv.clone(),
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> Service for AndThenApplyFn<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type Future = AndThenApplyFnFuture<A, B, F, Fut, Res, Err>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-01-17 01:58:11 +01:00
|
|
|
let inner = self.srv.get_mut();
|
|
|
|
let not_ready = inner.0.poll_ready(cx)?.is_pending();
|
|
|
|
if inner.1.poll_ready(cx)?.is_pending() || not_ready {
|
2019-12-03 14:59:28 +01:00
|
|
|
Poll::Pending
|
|
|
|
} else {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: A::Request) -> Self::Future {
|
2020-01-17 01:58:11 +01:00
|
|
|
let fut = self.srv.get_mut().0.call(req);
|
2019-12-03 14:59:28 +01:00
|
|
|
AndThenApplyFnFuture {
|
2020-01-17 01:58:11 +01:00
|
|
|
state: State::A(fut, Some(self.srv.clone())),
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
|
2019-12-05 07:37:26 +01:00
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error>,
|
|
|
|
Err: From<B::Error>,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
state: State<A, B, F, Fut, Res, Err>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pin_project::pin_project]
|
|
|
|
enum State<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error>,
|
|
|
|
Err: From<B::Error>,
|
|
|
|
{
|
2020-01-17 01:58:11 +01:00
|
|
|
A(#[pin] A::Future, Option<Cell<(A, B, F)>>),
|
2019-12-05 07:37:26 +01:00
|
|
|
B(#[pin] Fut),
|
2019-12-06 05:34:44 +01:00
|
|
|
Empty,
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFuture<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: Service,
|
|
|
|
B: Service,
|
|
|
|
F: FnMut(A::Response, &mut B) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
type Output = Result<Res, Err>;
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::project]
|
2019-12-03 14:59:28 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let mut this = self.as_mut().project();
|
|
|
|
|
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);
|
2019-12-05 07:37:26 +01:00
|
|
|
let b = b.get_mut();
|
2020-01-17 01:58:11 +01:00
|
|
|
let fut = (&mut b.2)(res, &mut b.1);
|
2019-12-05 07:37:26 +01:00
|
|
|
this.state.set(State::B(fut));
|
|
|
|
self.poll(cx)
|
|
|
|
}
|
|
|
|
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`"),
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `AndThenApplyFn` service factory
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenApplyFnFactory<A, B, F, Fut, Res, Err> {
|
|
|
|
srv: Rc<(A, B, F)>,
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData<(Fut, Res, Err)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> AndThenApplyFnFactory<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
2019-12-05 07:37:26 +01:00
|
|
|
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
2019-12-03 14:59:28 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
/// Create new `ApplyNewService` new service instance
|
|
|
|
pub(crate) fn new(a: A, b: B, f: F) -> Self {
|
|
|
|
Self {
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: Rc::new((a, b, f)),
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 01:58:11 +01:00
|
|
|
impl<A, B, F, Fut, Res, Err> Clone for AndThenApplyFnFactory<A, B, F, Fut, Res, Err> {
|
2019-12-03 14:59:28 +01:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: self.srv.clone(),
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> ServiceFactory for AndThenApplyFnFactory<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: ServiceFactory,
|
|
|
|
A::Config: Clone,
|
|
|
|
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
2019-12-05 07:37:26 +01:00
|
|
|
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
2019-12-03 14:59:28 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
type Request = A::Request;
|
|
|
|
type Response = Res;
|
|
|
|
type Error = Err;
|
|
|
|
type Service = AndThenApplyFn<A::Service, B::Service, F, Fut, Res, Err>;
|
|
|
|
type Config = A::Config;
|
|
|
|
type InitError = A::InitError;
|
|
|
|
type Future = AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>;
|
|
|
|
|
|
|
|
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
2020-01-17 01:58:11 +01:00
|
|
|
let srv = &*self.srv;
|
2019-12-03 14:59:28 +01:00
|
|
|
AndThenApplyFnFactoryResponse {
|
|
|
|
a: None,
|
|
|
|
b: None,
|
2020-01-17 01:58:11 +01:00
|
|
|
f: srv.2.clone(),
|
|
|
|
fut_a: srv.0.new_service(cfg.clone()),
|
|
|
|
fut_b: srv.1.new_service(cfg),
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
#[pin_project::pin_project]
|
2020-01-17 01:58:11 +01:00
|
|
|
pub(crate) struct AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
|
2019-12-05 07:37:26 +01:00
|
|
|
where
|
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
|
|
|
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error>,
|
|
|
|
Err: From<B::Error>,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
fut_b: B::Future,
|
|
|
|
#[pin]
|
|
|
|
fut_a: A::Future,
|
|
|
|
f: F,
|
|
|
|
a: Option<A::Service>,
|
|
|
|
b: Option<B::Service>,
|
2019-12-03 14:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A, B, F, Fut, Res, Err> Future for AndThenApplyFnFactoryResponse<A, B, F, Fut, Res, Err>
|
|
|
|
where
|
|
|
|
A: ServiceFactory,
|
|
|
|
B: ServiceFactory<Config = A::Config, InitError = A::InitError>,
|
2019-12-05 07:37:26 +01:00
|
|
|
F: FnMut(A::Response, &mut B::Service) -> Fut + Clone,
|
2019-12-03 14:59:28 +01:00
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
|
|
|
Err: From<A::Error> + From<B::Error>,
|
|
|
|
{
|
|
|
|
type Output =
|
|
|
|
Result<AndThenApplyFn<A::Service, B::Service, F, Fut, Res, Err>, A::InitError>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
if this.a.is_none() {
|
|
|
|
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
|
|
|
|
*this.a = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.b.is_none() {
|
|
|
|
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
|
|
|
|
*this.b = Some(service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if this.a.is_some() && this.b.is_some() {
|
|
|
|
Poll::Ready(Ok(AndThenApplyFn {
|
2020-01-17 01:58:11 +01:00
|
|
|
srv: Cell::new((
|
|
|
|
this.a.take().unwrap(),
|
|
|
|
this.b.take().unwrap(),
|
|
|
|
this.f.clone(),
|
|
|
|
)),
|
2019-12-03 14:59:28 +01:00
|
|
|
r: PhantomData,
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2019-12-05 07:37:26 +01:00
|
|
|
use futures_util::future::{lazy, ok, Ready, TryFutureExt};
|
2019-12-03 14:59:28 +01:00
|
|
|
|
2019-12-08 14:05:05 +01:00
|
|
|
use crate::{fn_service, pipeline, pipeline_factory, Service, ServiceFactory};
|
2019-12-03 14:59:28 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv;
|
|
|
|
impl Service for Srv {
|
|
|
|
type Request = ();
|
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
|
|
|
type Future = Ready<Result<(), ()>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
|
|
|
ok(req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_service() {
|
|
|
|
let mut srv = pipeline(|r: &'static str| ok(r))
|
|
|
|
.and_then_apply_fn(Srv, |req: &'static str, s| {
|
|
|
|
s.call(()).map_ok(move |res| (req, res))
|
|
|
|
});
|
|
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
|
|
assert_eq!(res, Poll::Ready(Ok(())));
|
|
|
|
|
|
|
|
let res = srv.call("srv").await;
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), ("srv", ()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn test_service_factory() {
|
2019-12-08 14:05:05 +01:00
|
|
|
let new_srv = pipeline_factory(|| ok::<_, ()>(fn_service(|r: &'static str| ok(r))))
|
2019-12-03 14:59:28 +01:00
|
|
|
.and_then_apply_fn(
|
|
|
|
|| ok(Srv),
|
|
|
|
|req: &'static str, s| s.call(()).map_ok(move |res| (req, res)),
|
|
|
|
);
|
|
|
|
let mut srv = new_srv.new_service(()).await.unwrap();
|
|
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
|
|
assert_eq!(res, Poll::Ready(Ok(())));
|
|
|
|
|
|
|
|
let res = srv.call("srv").await;
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), ("srv", ()));
|
|
|
|
}
|
|
|
|
}
|