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