2019-11-14 13:38:24 +01:00
|
|
|
use std::future::Future;
|
2018-11-30 03:56:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2019-11-14 13:38:24 +01:00
|
|
|
use std::pin::Pin;
|
|
|
|
use std::task::{Context, Poll};
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
use pin_project::pin_project;
|
2018-09-11 18:30:22 +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.
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) struct Map<A, F, Response> {
|
2018-09-12 22:34:53 +02:00
|
|
|
service: A,
|
2018-08-28 19:39:27 +02:00
|
|
|
f: F,
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData<Response>,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Response> Map<A, F, Response> {
|
2018-08-28 19:39:27 +02:00
|
|
|
/// Create new `Map` combinator
|
2019-03-09 15:36:23 +01:00
|
|
|
pub fn new(service: A, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2019-01-17 00:33:33 +01:00
|
|
|
F: FnMut(A::Response) -> Response,
|
2018-11-30 03:56:15 +01:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
_t: PhantomData,
|
|
|
|
}
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Response> Clone for Map<A, F, Response>
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<A, F, Response> Service for Map<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2019-01-17 00:33:33 +01:00
|
|
|
F: FnMut(A::Response) -> Response + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Response = Response;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
2019-03-09 15:36:23 +01:00
|
|
|
type Future = MapFuture<A, F, Response>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, ctx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
self.service.poll_ready(ctx)
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
fn call(&mut self, req: A::Request) -> 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project]
|
|
|
|
pub(crate) struct MapFuture<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2019-01-17 00:33:33 +01:00
|
|
|
F: FnMut(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
f: F,
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin]
|
2018-08-28 19:39:27 +02:00
|
|
|
fut: A::Future,
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<A, F, Response> MapFuture<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2019-01-17 00:33:33 +01:00
|
|
|
F: FnMut(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl<A, F, Response> Future for MapFuture<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-03-09 15:36:23 +01:00
|
|
|
A: Service,
|
2019-01-17 00:33:33 +01:00
|
|
|
F: FnMut(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<Response, A::Error>;
|
|
|
|
|
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
let this = self.project();
|
|
|
|
match this.fut.poll(cx) {
|
|
|
|
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
|
2019-11-14 13:38:24 +01:00
|
|
|
pub(crate) struct MapNewService<A, F, Res> {
|
2018-08-28 19:39:27 +02:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2019-05-12 15:03:50 +02:00
|
|
|
r: PhantomData<Res>,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, Res> MapNewService<A, F, Res> {
|
2018-08-28 19:39:27 +02:00
|
|
|
/// Create new `Map` new service instance
|
2019-03-09 15:36:23 +01:00
|
|
|
pub fn new(a: A, f: F) -> Self
|
2018-11-30 03:56:15 +01:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-02-22 21:44:37 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, Res> Clone for MapNewService<A, F, 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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
impl<A, F, Res> ServiceFactory for MapNewService<A, F, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-02-22 21:44:37 +01:00
|
|
|
F: FnMut(A::Response) -> Res + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-03-09 15:36:23 +01:00
|
|
|
type Request = A::Request;
|
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;
|
|
|
|
type Service = Map<A::Service, F, Res>;
|
2018-08-28 19:39:27 +02:00
|
|
|
type InitError = A::InitError;
|
2019-05-12 15:03:50 +02:00
|
|
|
type Future = MapNewServiceFuture<A, F, Res>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
fn new_service(&self, cfg: &A::Config) -> Self::Future {
|
2019-03-05 06:35:47 +01:00
|
|
|
MapNewServiceFuture::new(self.a.new_service(cfg), self.f.clone())
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin_project]
|
|
|
|
pub(crate) struct MapNewServiceFuture<A, F, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-02-22 21:44:37 +01:00
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
#[pin]
|
2019-03-05 06:35:47 +01:00
|
|
|
fut: A::Future,
|
2018-08-28 19:39:27 +02:00
|
|
|
f: Option<F>,
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, Res> MapNewServiceFuture<A, F, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-02-22 21:44:37 +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 {
|
2018-08-28 19:39:27 +02:00
|
|
|
MapNewServiceFuture { f: Some(f), fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-12 15:03:50 +02:00
|
|
|
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2019-11-14 13:38:24 +01:00
|
|
|
A: ServiceFactory,
|
2019-02-22 21:44:37 +01:00
|
|
|
F: FnMut(A::Response) -> Res,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2019-11-14 13:38:24 +01:00
|
|
|
type Output = Result<Map<A::Service, F, 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> {
|
|
|
|
let this = self.project();
|
|
|
|
if let Poll::Ready(svc) = this.fut.poll(cx)? {
|
|
|
|
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-11-14 13:38:24 +01:00
|
|
|
use futures::future::{lazy, ok, Ready};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
use super::*;
|
2019-11-14 13:38:24 +01:00
|
|
|
use crate::{into_factory, into_service, Service};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv;
|
2019-11-14 13:38:24 +01:00
|
|
|
|
2019-03-09 15:36:23 +01:00
|
|
|
impl Service for Srv {
|
|
|
|
type Request = ();
|
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
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
fn poll_ready(&mut self, _: &mut Context) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Poll::Ready(Ok(()))
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_poll_ready() {
|
|
|
|
let mut srv = into_service(Srv).map(|_| "ok");
|
|
|
|
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-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_call() {
|
|
|
|
let mut srv = into_service(Srv).map(|_| "ok");
|
|
|
|
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-14 13:38:24 +01:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_new_service() {
|
|
|
|
let new_srv = into_factory(|| ok::<_, ()>(Srv)).map(|_| "ok");
|
|
|
|
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
|
|
|
}
|