2018-08-30 18:17:17 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
use futures::{Async, Future, IntoFuture, Poll};
|
2018-09-04 18:49:21 +02:00
|
|
|
use {IntoNewService, NewService, Service};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
/// `Apply` service combinator
|
|
|
|
pub struct Apply<T, F, R, Req> {
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2018-08-31 21:51:26 +02:00
|
|
|
r: PhantomData<Fn(Req) -> R>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> Apply<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: Service,
|
2018-08-31 21:51:26 +02:00
|
|
|
T::Error: Into<<R::Future as Future>::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
|
|
|
pub fn new(f: F, service: T) -> Self {
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> Service for Apply<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: Service,
|
2018-08-31 21:51:26 +02:00
|
|
|
T::Error: Into<<R::Future as Future>::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
type Request = Req;
|
2018-08-31 21:51:26 +02:00
|
|
|
type Response = <R::Future as Future>::Item;
|
|
|
|
type Error = <R::Future as Future>::Error;
|
|
|
|
type Future = R::Future;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.poll_ready().map_err(|e| e.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Self::Request) -> Self::Future {
|
2018-08-31 21:51:26 +02:00
|
|
|
(self.f)(req, &mut self.service).into_future()
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 02:54:59 +02:00
|
|
|
/// `ApplyNewService` new service combinator
|
2018-08-31 21:51:26 +02:00
|
|
|
pub struct ApplyNewService<T, F, R, Req> {
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2018-08-31 21:51:26 +02:00
|
|
|
r: PhantomData<Fn(Req) -> R>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> ApplyNewService<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
/// Create new `ApplyNewService` new service instance
|
2018-09-04 18:49:21 +02:00
|
|
|
pub fn new<F1: IntoNewService<T>>(f: F, service: F1) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
|
|
|
f,
|
2018-09-04 18:49:21 +02:00
|
|
|
service: service.into_new_service(),
|
2018-08-30 18:17:17 +02:00
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> Clone for ApplyNewService<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService + Clone,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
service: self.service.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> NewService for ApplyNewService<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
2018-08-31 21:51:26 +02:00
|
|
|
T::Error: Into<<R::Future as Future>::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
type Request = Req;
|
2018-08-31 21:51:26 +02:00
|
|
|
type Response = <R::Future as Future>::Item;
|
|
|
|
type Error = <R::Future as Future>::Error;
|
|
|
|
type Service = Apply<T::Service, F, R, Req>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
type InitError = T::InitError;
|
2018-08-31 21:51:26 +02:00
|
|
|
type Future = ApplyNewServiceFuture<T, F, R, Req>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2018-08-31 02:54:59 +02:00
|
|
|
ApplyNewServiceFuture::new(self.service.new_service(), self.f.clone())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
pub struct ApplyNewServiceFuture<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fut: T::Future,
|
|
|
|
f: Option<F>,
|
2018-08-31 21:51:26 +02:00
|
|
|
r: PhantomData<Fn(Req) -> R>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> ApplyNewServiceFuture<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fn new(fut: T::Future, f: F) -> Self {
|
2018-08-31 02:54:59 +02:00
|
|
|
ApplyNewServiceFuture {
|
2018-08-30 18:17:17 +02:00
|
|
|
f: Some(f),
|
|
|
|
fut,
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
impl<T, F, R, Req> Future for ApplyNewServiceFuture<T, F, R, Req>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
2018-08-31 21:51:26 +02:00
|
|
|
T::Error: Into<<R::Future as Future>::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
2018-08-31 21:51:26 +02:00
|
|
|
R: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-08-31 21:51:26 +02:00
|
|
|
type Item = Apply<T::Service, F, R, Req>;
|
2018-08-30 18:17:17 +02:00
|
|
|
type Error = T::InitError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
if let Async::Ready(service) = self.fut.poll()? {
|
2018-08-31 02:54:59 +02:00
|
|
|
Ok(Async::Ready(Apply::new(self.f.take().unwrap(), service)))
|
2018-08-30 18:17:17 +02:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|