2018-08-30 18:17:17 +02:00
|
|
|
use std::marker::PhantomData;
|
|
|
|
|
|
|
|
use futures::{Async, Future, Poll};
|
2018-08-30 18:38:09 +02:00
|
|
|
use {NewService, Service, IntoNewService};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
/// `ApplyService` service combinator
|
|
|
|
pub struct ApplyService<T, F, R, Req, Resp, Err> {
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
|
|
|
r: PhantomData<R>,
|
|
|
|
r1: PhantomData<Req>,
|
|
|
|
r2: PhantomData<Resp>,
|
|
|
|
e: PhantomData<Err>,
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> ApplyService<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: Service,
|
2018-08-31 00:12:01 +02:00
|
|
|
T::Error: Into<Err>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
|
|
|
pub fn new(f: F, service: T) -> Self {
|
|
|
|
Self {
|
|
|
|
service,
|
|
|
|
f,
|
|
|
|
r: PhantomData,
|
|
|
|
r1: PhantomData,
|
|
|
|
r2: PhantomData,
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> Service for ApplyService<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: Service,
|
|
|
|
T::Error: Into<Err>,
|
|
|
|
F: Fn(Req, &mut T) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Resp;
|
|
|
|
type Error = Err;
|
|
|
|
type Future = R;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
(self.f)(req, &mut self.service)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
/// `Apply` new service combinator
|
|
|
|
pub struct Apply<T, F, R, Req, Resp, Err> {
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
|
|
|
r: PhantomData<R>,
|
|
|
|
r1: PhantomData<Req>,
|
|
|
|
r2: PhantomData<Resp>,
|
|
|
|
e: PhantomData<Err>,
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> Apply<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
/// Create new `Partial` new service instance
|
2018-08-30 18:38:09 +02:00
|
|
|
pub fn new<F1: IntoNewService<T>>(f: F, service: F1) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
|
|
|
f,
|
2018-08-30 18:38:09 +02:00
|
|
|
service: service.into_new_service(),
|
2018-08-30 18:17:17 +02:00
|
|
|
r: PhantomData,
|
|
|
|
r1: PhantomData,
|
|
|
|
r2: PhantomData,
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> Clone for Apply<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService + Clone,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
service: self.service.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
r: PhantomData,
|
|
|
|
r1: PhantomData,
|
|
|
|
r2: PhantomData,
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> NewService for Apply<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
T::Error: Into<Err>,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R + Clone,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
type Request = Req;
|
|
|
|
type Response = Resp;
|
|
|
|
type Error = Err;
|
2018-08-30 18:26:27 +02:00
|
|
|
type Service = ApplyService<T::Service, F, R, Req, Resp, Err>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
type InitError = T::InitError;
|
2018-08-30 18:26:27 +02:00
|
|
|
type Future = ApplyFuture<T, F, R, Req, Resp, Err>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2018-08-30 18:26:27 +02:00
|
|
|
ApplyFuture::new(self.service.new_service(), self.f.clone())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
pub struct ApplyFuture<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
fut: T::Future,
|
|
|
|
f: Option<F>,
|
|
|
|
r: PhantomData<R>,
|
|
|
|
r1: PhantomData<Req>,
|
|
|
|
r2: PhantomData<Resp>,
|
|
|
|
e: PhantomData<Err>,
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> ApplyFuture<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
|
|
|
fn new(fut: T::Future, f: F) -> Self {
|
2018-08-30 18:26:27 +02:00
|
|
|
ApplyFuture {
|
2018-08-30 18:17:17 +02:00
|
|
|
f: Some(f),
|
|
|
|
fut,
|
|
|
|
r: PhantomData,
|
|
|
|
r1: PhantomData,
|
|
|
|
r2: PhantomData,
|
|
|
|
e: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:26:27 +02:00
|
|
|
impl<T, F, R, Req, Resp, Err> Future for ApplyFuture<T, F, R, Req, Resp, Err>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
|
|
|
T: NewService,
|
2018-08-31 00:12:01 +02:00
|
|
|
T::Error: Into<Err>,
|
2018-08-30 18:17:17 +02:00
|
|
|
F: Fn(Req, &mut T::Service) -> R,
|
|
|
|
R: Future<Item = Resp, Error = Err>,
|
|
|
|
{
|
2018-08-30 18:26:27 +02:00
|
|
|
type Item = ApplyService<T::Service, F, R, Req, Resp, Err>;
|
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-30 18:26:27 +02:00
|
|
|
Ok(Async::Ready(ApplyService::new(
|
|
|
|
self.f.take().unwrap(),
|
|
|
|
service,
|
|
|
|
)))
|
2018-08-30 18:17:17 +02:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|