2020-12-27 15:15:42 +01:00
|
|
|
use core::{
|
|
|
|
future::Future,
|
|
|
|
marker::PhantomData,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
|
|
|
use pin_project_lite::pin_project;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use super::{Service, ServiceFactory};
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2018-09-12 22:34:53 +02:00
|
|
|
/// Service for the `map` combinator, changing the type of a service's response.
|
|
|
|
///
|
|
|
|
/// This is created by the `ServiceExt::map` method.
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct Map<A, F, Req, Res> {
|
2018-09-12 22:34:53 +02:00
|
|
|
service: A,
|
2018-08-28 19:39:27 +02:00
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
_t: PhantomData<(Req, Res)>,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Map<A, F, Req, Res> {
|
2018-08-28 19:39:27 +02:00
|
|
|
/// Create new `Map` combinator
|
2019-11-18 09:30:04 +01:00
|
|
|
pub(crate) fn new(service: A, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Clone for Map<A, F, Req, Res>
|
2018-09-05 22:54:15 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
F: Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Map {
|
2018-09-12 22:34:53 +02:00
|
|
|
service: self.service.clone(),
|
2018-09-05 22:54:15 +02:00
|
|
|
f: self.f.clone(),
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData,
|
2018-09-05 22:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Service<Req> for Map<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Response = Res;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = MapFuture<A, F, Req, Res>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
crate::forward_ready!(service);
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
fn call(&mut self, req: Req) -> Self::Future {
|
2018-09-12 22:34:53 +02:00
|
|
|
MapFuture::new(self.service.call(req), self.f.clone())
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
pub struct MapFuture<A, F, Req, Res>
|
|
|
|
where
|
|
|
|
A: Service<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res,
|
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
#[pin]
|
|
|
|
fut: A::Future,
|
|
|
|
}
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> MapFuture<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Future for MapFuture<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: Service<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Output = Result<Res, A::Error>;
|
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-19 09:51:40 +01:00
|
|
|
match this.fut.poll(cx) {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))),
|
|
|
|
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `MapNewService` new service combinator
|
2020-12-27 05:28:00 +01:00
|
|
|
pub struct MapServiceFactory<A, F, Req, Res> {
|
2018-08-28 19:39:27 +02:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2020-12-27 05:28:00 +01:00
|
|
|
r: PhantomData<(Res, Req)>,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> MapServiceFactory<A, F, Req, Res> {
|
2018-08-28 19:39:27 +02:00
|
|
|
/// Create new `Map` new service instance
|
2019-11-18 09:30:04 +01:00
|
|
|
pub(crate) fn new(a: A, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
2018-08-28 19:39:27 +02:00
|
|
|
Self {
|
|
|
|
a,
|
|
|
|
f,
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Clone for MapServiceFactory<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Clone,
|
|
|
|
F: Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
a: self.a.clone(),
|
|
|
|
f: self.f.clone(),
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> ServiceFactory<Req> for MapServiceFactory<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(A::Response) -> Res + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-02-22 21:44:37 +01:00
|
|
|
type Response = Res;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
type Config = A::Config;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Service = Map<A::Service, F, Req, Res>;
|
2018-08-28 19:39:27 +02:00
|
|
|
type InitError = A::InitError;
|
2020-12-27 05:28:00 +01:00
|
|
|
type Future = MapServiceFuture<A, F, Req, Res>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2019-12-02 16:27:48 +01:00
|
|
|
fn new_service(&self, cfg: A::Config) -> Self::Future {
|
2019-11-18 09:30:04 +01:00
|
|
|
MapServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
pin_project! {
|
|
|
|
pub struct MapServiceFuture<A, F, Req, Res>
|
|
|
|
where
|
|
|
|
A: ServiceFactory<Req>,
|
|
|
|
F: FnMut(A::Response) -> Res,
|
|
|
|
{
|
|
|
|
#[pin]
|
|
|
|
fut: A::Future,
|
|
|
|
f: Option<F>,
|
|
|
|
}
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> MapServiceFuture<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-03-05 06:35:47 +01:00
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
2019-11-18 09:30:04 +01:00
|
|
|
MapServiceFuture { f: Some(f), fut }
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl<A, F, Req, Res> Future for MapServiceFuture<A, F, Req, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2020-12-27 05:28:00 +01:00
|
|
|
A: ServiceFactory<Req>,
|
2019-11-19 09:51:40 +01:00
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2020-12-27 05:28:00 +01:00
|
|
|
type Output = Result<Map<A::Service, F, Req, Res>, A::InitError>;
|
2018-08-28 19:39:27 +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-19 09:51:40 +01:00
|
|
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap())))
|
2018-08-28 19:39:27 +02:00
|
|
|
} else {
|
2019-11-14 13:38:24 +01:00
|
|
|
Poll::Pending
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-12-05 07:37:26 +01:00
|
|
|
use futures_util::future::{lazy, ok, Ready};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
use super::*;
|
2019-11-18 09:30:04 +01:00
|
|
|
use crate::{IntoServiceFactory, Service, ServiceFactory};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv;
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2020-12-27 05:28:00 +01:00
|
|
|
impl Service<()> for Srv {
|
2018-09-12 22:34:53 +02:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
2019-11-14 13:38:24 +01:00
|
|
|
type Future = Ready<Result<(), ()>>;
|
2018-09-12 22:34:53 +02:00
|
|
|
|
2020-12-27 15:15:42 +01:00
|
|
|
crate::always_ready!();
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_poll_ready() {
|
2019-11-18 09:30:04 +01:00
|
|
|
let mut srv = Srv.map(|_| "ok");
|
2019-11-14 13:38:24 +01:00
|
|
|
let res = lazy(|cx| srv.poll_ready(cx)).await;
|
|
|
|
assert_eq!(res, Poll::Ready(Ok(())));
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:49:11 +01:00
|
|
|
#[actix_rt::test]
|
2019-11-14 13:38:24 +01:00
|
|
|
async fn test_call() {
|
2019-11-18 09:30:04 +01:00
|
|
|
let mut srv = Srv.map(|_| "ok");
|
2019-11-14 13:38:24 +01:00
|
|
|
let res = srv.call(()).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(), "ok");
|
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() {
|
2019-11-18 09:30:04 +01:00
|
|
|
let new_srv = (|| ok::<_, ()>(Srv)).into_factory().map(|_| "ok");
|
2019-11-14 13:38:24 +01:00
|
|
|
let mut srv = new_srv.new_service(&()).await.unwrap();
|
|
|
|
let res = srv.call(()).await;
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), ("ok"));
|
2018-09-18 04:21:24 +02:00
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|