2018-11-30 03:56:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
|
|
|
use futures::{Async, Future, Poll};
|
2018-09-11 18:30:22 +02:00
|
|
|
|
|
|
|
use super::{NewService, Service};
|
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.
|
2018-11-30 03:56:15 +01:00
|
|
|
pub 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
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request>(service: A, f: F) -> Self
|
|
|
|
where
|
|
|
|
A: Service<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> Service<Request> for Map<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
F: Fn(A::Response) -> Response + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
type Response = Response;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = MapFuture<A, F, Request, Response>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2018-09-12 22:34:53 +02:00
|
|
|
self.service.poll_ready()
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct MapFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
f: F,
|
|
|
|
fut: A::Future,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> MapFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapFuture { f, fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> Future for MapFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: Service<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
type Item = Response;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
match self.fut.poll()? {
|
|
|
|
Async::Ready(resp) => Ok(Async::Ready((self.f)(resp))),
|
|
|
|
Async::NotReady => Ok(Async::NotReady),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// `MapNewService` new service combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct MapNewService<A, F, Response> {
|
2018-08-28 19:39:27 +02:00
|
|
|
a: A,
|
|
|
|
f: F,
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData<Response>,
|
2018-08-28 19:39:27 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Response> MapNewService<A, F, Response> {
|
2018-08-28 19:39:27 +02:00
|
|
|
/// Create new `Map` new service instance
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<Request>(a: A, f: F) -> Self
|
|
|
|
where
|
|
|
|
A: NewService<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Response> Clone for MapNewService<A, F, Response>
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> NewService<Request> for MapNewService<A, F, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
F: Fn(A::Response) -> Response + Clone,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
type Response = Response;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Service = Map<A::Service, F, Response>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
|
|
|
type InitError = A::InitError;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = MapNewServiceFuture<A, F, Request, Response>;
|
2018-08-28 19:39:27 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
|
|
|
MapNewServiceFuture::new(self.a.new_service(), self.f.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct MapNewServiceFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fut: A::Future,
|
|
|
|
f: Option<F>,
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> MapNewServiceFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
|
|
|
fn new(fut: A::Future, f: F) -> Self {
|
|
|
|
MapNewServiceFuture { f: Some(f), fut }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<A, F, Request, Response> Future for MapNewServiceFuture<A, F, Request, Response>
|
2018-08-28 19:39:27 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
A: NewService<Request>,
|
|
|
|
F: Fn(A::Response) -> Response,
|
2018-08-28 19:39:27 +02:00
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
type Item = Map<A::Service, F, Response>;
|
2018-08-28 19:39:27 +02:00
|
|
|
type Error = A::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Async::Ready(service) = self.fut.poll()? {
|
|
|
|
Ok(Async::Ready(Map::new(service, self.f.take().unwrap())))
|
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
|
|
|
|
use super::*;
|
2018-12-06 23:53:55 +01:00
|
|
|
use crate::service::{IntoNewService, NewServiceExt, Service, ServiceExt};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
struct Srv;
|
2018-11-30 04:17:02 +01:00
|
|
|
impl Service<()> for Srv {
|
2018-09-12 22:34:53 +02:00
|
|
|
type Response = ();
|
|
|
|
type Error = ();
|
|
|
|
type Future = FutureResult<(), ()>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_poll_ready() {
|
|
|
|
let mut srv = Srv.map(|_| "ok");
|
|
|
|
let res = srv.poll_ready();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_call() {
|
|
|
|
let mut srv = Srv.map(|_| "ok");
|
|
|
|
let res = srv.call(()).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready("ok"));
|
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
|
|
|
let blank = || Ok::<_, ()>(Srv);
|
|
|
|
let new_srv = blank.into_new_service().map(|_| "ok");
|
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
|
|
|
let res = srv.call(()).poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready("ok"));
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|