2020-12-27 04:28:00 +00:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use futures_util::ready;
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
use super::{IntoService, IntoServiceFactory, Service, ServiceFactory};
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2020-09-19 22:12:41 +08:00
|
|
|
/// Apply transform function to a service.
|
2020-12-27 04:28:00 +00:00
|
|
|
///
|
|
|
|
/// The In and Out type params refer to the request and response types for the wrapped service.
|
|
|
|
pub fn apply_fn<I, S, F, Fut, Req, In, Res, Err>(
|
|
|
|
service: I,
|
|
|
|
wrap_fn: F,
|
|
|
|
) -> Apply<S, F, Req, In, Res, Err>
|
2019-03-12 12:53:08 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
I: IntoService<S, In>,
|
|
|
|
S: Service<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut S) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-03-12 12:53:08 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
Apply::new(service.into_service(), wrap_fn)
|
2019-03-12 12:53:08 -07:00
|
|
|
}
|
|
|
|
|
2020-09-19 22:12:41 +08:00
|
|
|
/// Service factory that produces `apply_fn` service.
|
2020-12-27 04:28:00 +00:00
|
|
|
///
|
|
|
|
/// The In and Out type params refer to the request and response types for the wrapped service.
|
|
|
|
pub fn apply_fn_factory<I, SF, F, Fut, Req, In, Res, Err>(
|
|
|
|
service: I,
|
2019-11-14 18:38:24 +06:00
|
|
|
f: F,
|
2020-12-27 04:28:00 +00:00
|
|
|
) -> ApplyFactory<SF, F, Req, In, Res, Err>
|
2019-03-12 12:53:08 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
I: IntoServiceFactory<SF, In>,
|
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-03-12 12:53:08 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
ApplyFactory::new(service.into_factory(), f)
|
2019-03-12 12:53:08 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
/// `Apply` service combinator.
|
|
|
|
///
|
|
|
|
/// The In and Out type params refer to the request and response types for the wrapped service.
|
|
|
|
pub struct Apply<S, F, Req, In, Res, Err>
|
2019-03-09 06:36:23 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<In, Error = Err>,
|
2019-03-09 06:36:23 -08:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
service: S,
|
|
|
|
wrap_fn: F,
|
|
|
|
_phantom: PhantomData<(Req, In, Res, Err)>,
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, F, Fut, Req, In, Res, Err> Apply<S, F, Req, In, Res, Err>
|
2018-08-30 09:17:17 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut S) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-30 09:17:17 -07:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
2020-12-27 04:28:00 +00:00
|
|
|
fn new(service: S, wrap_fn: F) -> Self {
|
2018-08-30 09:17:17 -07:00
|
|
|
Self {
|
2019-11-14 18:38:24 +06:00
|
|
|
service,
|
2020-12-27 04:28:00 +00:00
|
|
|
wrap_fn,
|
|
|
|
_phantom: PhantomData,
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 13:54:15 -07:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, F, Fut, Req, In, Res, Err> Clone for Apply<S, F, Req, In, Res, Err>
|
2019-12-09 14:07:20 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<In, Error = Err> + Clone,
|
|
|
|
F: FnMut(Req, &mut S) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-12-09 14:07:20 +06:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Apply {
|
|
|
|
service: self.service.clone(),
|
2020-12-27 04:28:00 +00:00
|
|
|
wrap_fn: self.wrap_fn.clone(),
|
|
|
|
_phantom: PhantomData,
|
2019-12-09 14:07:20 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<S, F, Fut, Req, In, Res, Err> Service<Req> for Apply<S, F, Req, In, Res, Err>
|
2018-09-05 13:54:15 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
S: Service<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut S) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-30 09:17:17 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
type Response = Res;
|
2019-11-14 18:38:24 +06:00
|
|
|
type Error = Err;
|
2020-12-27 04:28:00 +00:00
|
|
|
type Future = Fut;
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2019-12-05 12:37:26 +06:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-12-27 04:28:00 +00:00
|
|
|
Poll::Ready(ready!(self.service.poll_ready(cx)))
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
|
|
|
(self.wrap_fn)(req, &mut self.service)
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
/// `ApplyFactory` service factory combinator.
|
|
|
|
pub struct ApplyFactory<SF, F, Req, In, Res, Err> {
|
|
|
|
factory: SF,
|
|
|
|
wrap_fn: F,
|
|
|
|
_phantom: PhantomData<(Req, In, Res, Err)>,
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, F, Fut, Req, In, Res, Err> ApplyFactory<SF, F, Req, In, Res, Err>
|
2019-03-09 06:36:23 -08:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-03-09 06:36:23 -08:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
/// Create new `ApplyFactory` new service instance
|
|
|
|
fn new(factory: SF, wrap_fn: F) -> Self {
|
2018-08-30 09:17:17 -07:00
|
|
|
Self {
|
2020-12-27 04:28:00 +00:00
|
|
|
factory,
|
|
|
|
wrap_fn,
|
|
|
|
_phantom: PhantomData,
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, F, Fut, Req, In, Res, Err> Clone for ApplyFactory<SF, F, Req, In, Res, Err>
|
2019-12-03 16:15:06 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err> + Clone,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-12-03 16:15:06 +06:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
2020-12-27 04:28:00 +00:00
|
|
|
factory: self.factory.clone(),
|
|
|
|
wrap_fn: self.wrap_fn.clone(),
|
|
|
|
_phantom: PhantomData,
|
2019-12-03 16:15:06 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, F, Fut, Req, In, Res, Err> ServiceFactory<Req>
|
|
|
|
for ApplyFactory<SF, F, Req, In, Res, Err>
|
2018-08-30 09:17:17 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut + Clone,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-30 09:17:17 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
type Response = Res;
|
2019-11-14 18:38:24 +06:00
|
|
|
type Error = Err;
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
type Config = SF::Config;
|
|
|
|
type Service = Apply<SF::Service, F, Req, In, Res, Err>;
|
|
|
|
type InitError = SF::InitError;
|
|
|
|
type Future = ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>;
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
fn new_service(&self, cfg: SF::Config) -> Self::Future {
|
|
|
|
let svc = self.factory.new_service(cfg);
|
|
|
|
ApplyServiceFactoryResponse::new(svc, self.wrap_fn.clone())
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:37:26 +06:00
|
|
|
#[pin_project::pin_project]
|
2020-12-27 04:28:00 +00:00
|
|
|
pub struct ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
|
2019-12-05 12:37:26 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2019-12-05 12:37:26 +06:00
|
|
|
{
|
|
|
|
#[pin]
|
2020-12-27 04:28:00 +00:00
|
|
|
fut: SF::Future,
|
|
|
|
wrap_fn: Option<F>,
|
|
|
|
_phantom: PhantomData<(Req, Res)>,
|
2019-03-04 19:38:11 -08:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, F, Fut, Req, In, Res, Err> ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
|
2018-08-30 09:17:17 -07:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-30 09:17:17 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
fn new(fut: SF::Future, wrap_fn: F) -> Self {
|
2019-11-18 14:30:04 +06:00
|
|
|
Self {
|
2019-03-04 19:38:11 -08:00
|
|
|
fut,
|
2020-12-27 04:28:00 +00:00
|
|
|
wrap_fn: Some(wrap_fn),
|
|
|
|
_phantom: PhantomData,
|
2019-03-04 19:38:11 -08:00
|
|
|
}
|
|
|
|
}
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl<SF, F, Fut, Req, In, Res, Err> Future
|
|
|
|
for ApplyServiceFactoryResponse<SF, F, Fut, Req, In, Res, Err>
|
2019-11-18 14:30:04 +06:00
|
|
|
where
|
2020-12-27 04:28:00 +00:00
|
|
|
SF: ServiceFactory<In, Error = Err>,
|
|
|
|
F: FnMut(Req, &mut SF::Service) -> Fut,
|
|
|
|
Fut: Future<Output = Result<Res, Err>>,
|
2018-08-30 09:17:17 -07:00
|
|
|
{
|
2020-12-27 04:28:00 +00:00
|
|
|
type Output = Result<Apply<SF::Service, F, Req, In, Res, Err>, SF::InitError>;
|
2018-08-30 09:17:17 -07:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-11-19 14:51:40 +06:00
|
|
|
let this = self.project();
|
2019-11-18 14:30:04 +06:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
let svc = ready!(this.fut.poll(cx))?;
|
|
|
|
Poll::Ready(Ok(Apply::new(svc, Option::take(this.wrap_fn).unwrap())))
|
2018-08-30 09:17:17 -07:00
|
|
|
}
|
|
|
|
}
|
2018-09-12 13:34:53 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-11-14 18:38:24 +06:00
|
|
|
use std::task::{Context, Poll};
|
|
|
|
|
2019-12-05 12:37:26 +06:00
|
|
|
use futures_util::future::{lazy, ok, Ready};
|
2018-09-12 13:34:53 -07:00
|
|
|
|
2019-02-03 10:42:27 -08:00
|
|
|
use super::*;
|
2019-11-14 18:38:24 +06:00
|
|
|
use crate::{pipeline, pipeline_factory, Service, ServiceFactory};
|
2018-09-12 13:34:53 -07:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Srv;
|
2019-11-14 18:38:24 +06:00
|
|
|
|
2020-12-27 04:28:00 +00:00
|
|
|
impl Service<()> for Srv {
|
2018-09-12 13:34:53 -07:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
2019-11-14 18:38:24 +06:00
|
|
|
type Future = Ready<Result<(), ()>>;
|
2018-09-12 13:34:53 -07:00
|
|
|
|
2019-12-02 22:30:09 +06:00
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2019-11-14 18:38:24 +06:00
|
|
|
Poll::Ready(Ok(()))
|
2018-09-12 13:34:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 21:49:11 +06:00
|
|
|
#[actix_rt::test]
|
2019-11-14 18:38:24 +06:00
|
|
|
async fn test_call() {
|
|
|
|
let mut srv = pipeline(apply_fn(Srv, |req: &'static str, srv| {
|
|
|
|
let fut = srv.call(());
|
|
|
|
async move {
|
2020-01-28 20:27:33 +09:00
|
|
|
fut.await.unwrap();
|
|
|
|
Ok((req, ()))
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
|
|
|
}));
|
2019-03-04 19:38:11 -08:00
|
|
|
|
2019-11-14 18:38:24 +06:00
|
|
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
|
|
|
|
|
|
|
let res = srv.call("srv").await;
|
2018-09-12 13:34:53 -07:00
|
|
|
assert!(res.is_ok());
|
2020-01-28 20:27:33 +09:00
|
|
|
assert_eq!(res.unwrap(), ("srv", ()));
|
2018-09-12 13:34:53 -07:00
|
|
|
}
|
2018-09-17 19:21:24 -07:00
|
|
|
|
2019-11-25 21:49:11 +06:00
|
|
|
#[actix_rt::test]
|
2019-11-14 18:38:24 +06:00
|
|
|
async fn test_new_service() {
|
|
|
|
let new_srv = pipeline_factory(apply_fn_factory(
|
|
|
|
|| ok::<_, ()>(Srv),
|
|
|
|
|req: &'static str, srv| {
|
|
|
|
let fut = srv.call(());
|
|
|
|
async move {
|
2020-01-28 20:27:33 +09:00
|
|
|
fut.await.unwrap();
|
|
|
|
Ok((req, ()))
|
2019-11-14 18:38:24 +06:00
|
|
|
}
|
|
|
|
},
|
|
|
|
));
|
|
|
|
|
2019-12-02 21:27:48 +06:00
|
|
|
let mut srv = new_srv.new_service(()).await.unwrap();
|
2019-11-14 18:38:24 +06:00
|
|
|
|
|
|
|
assert_eq!(lazy(|cx| srv.poll_ready(cx)).await, Poll::Ready(Ok(())));
|
|
|
|
|
|
|
|
let res = srv.call("srv").await;
|
|
|
|
assert!(res.is_ok());
|
2020-01-28 20:27:33 +09:00
|
|
|
assert_eq!(res.unwrap(), ("srv", ()));
|
2019-02-03 11:48:11 -08:00
|
|
|
}
|
2018-09-12 13:34:53 -07:00
|
|
|
}
|