1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-28 00:42:58 +01:00
actix-net/actix-service/src/map.rs

242 lines
5.3 KiB
Rust
Raw Normal View History

2018-11-30 03:56:15 +01:00
use std::marker::PhantomData;
2018-08-28 19:39:27 +02:00
2019-03-05 06:35:47 +01: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
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>
where
2018-11-30 03:56:15 +01:00
A: Clone,
F: Clone,
{
fn clone(&self) -> Self {
Map {
2018-09-12 22:34:53 +02:00
service: self.service.clone(),
f: self.f.clone(),
2018-11-30 03:56:15 +01:00
_t: PhantomData,
}
}
}
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
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
}
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-03-09 15:36:23 +01:00
pub 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,
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
{
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
pub struct MapNewService<A, F, Res> {
2018-08-28 19:39:27 +02:00
a: A,
f: F,
r: PhantomData<Res>,
2018-08-28 19:39:27 +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
A: NewService,
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
}
}
}
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
}
}
}
impl<A, F, Res> NewService for MapNewService<A, F, Res>
2018-08-28 19:39:27 +02:00
where
A: NewService,
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;
type Config = A::Config;
type Service = Map<A::Service, F, Res>;
2018-08-28 19:39:27 +02:00
type InitError = A::InitError;
type Future = MapNewServiceFuture<A, F, Res>;
2018-08-28 19:39:27 +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
}
}
pub struct MapNewServiceFuture<A, F, Res>
2018-08-28 19:39:27 +02:00
where
A: NewService,
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
fut: A::Future,
2018-08-28 19:39:27 +02:00
f: Option<F>,
}
impl<A, F, Res> MapNewServiceFuture<A, F, Res>
2018-08-28 19:39:27 +02:00
where
A: NewService,
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 }
}
}
impl<A, F, Res> Future for MapNewServiceFuture<A, F, Res>
2018-08-28 19:39:27 +02:00
where
A: NewService,
2019-02-22 21:44:37 +01:00
F: FnMut(A::Response) -> Res,
2018-08-28 19:39:27 +02:00
{
2019-02-22 21:44:37 +01:00
type Item = Map<A::Service, F, Res>;
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-13 03:56:39 +01:00
use crate::{IntoNewService, Service, ServiceExt};
2018-09-12 22:34:53 +02:00
struct Srv;
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 = ();
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");
2019-02-22 21:44:37 +01:00
if let Async::Ready(mut srv) = new_srv.new_service(&()).poll().unwrap() {
2018-09-18 04:21:24 +02:00
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
}