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-11 18:30:22 +02:00
|
|
|
|
2018-09-18 01:16:42 +02:00
|
|
|
use super::{IntoNewService, IntoService, NewService, Service};
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2018-08-31 21:51:26 +02:00
|
|
|
/// `Apply` service combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub struct Apply<T, F, In, Out, Request>
|
|
|
|
where
|
|
|
|
T: Service<Request>,
|
|
|
|
{
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData<(In, Out, Request)>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> Apply<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: Service<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-01-14 07:58:23 +01:00
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
/// Create new `Apply` combinator
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<I: IntoService<T, Request>>(service: I, f: F) -> Self {
|
2018-08-30 18:17:17 +02:00
|
|
|
Self {
|
2018-09-18 01:16:42 +02:00
|
|
|
service: service.into_service(),
|
2018-08-30 18:17:17 +02:00
|
|
|
f,
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-05 22:54:15 +02:00
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> Clone for Apply<T, F, In, Out, Request>
|
2018-09-05 22:54:15 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: Service<Request> + Clone,
|
|
|
|
F: Clone,
|
2018-09-05 22:54:15 +02:00
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Apply {
|
|
|
|
service: self.service.clone(),
|
|
|
|
f: self.f.clone(),
|
|
|
|
r: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-30 18:17:17 +02:00
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> Service<In> for Apply<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-01-14 07:58:23 +01:00
|
|
|
T: Service<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-01-14 07:58:23 +01:00
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-12-09 18:56:23 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = Out::Future;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
2019-01-14 07:58:23 +01:00
|
|
|
self.service.poll_ready().map_err(|e| e.into())
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
fn call(&mut self, req: In) -> 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-11-30 03:56:15 +01:00
|
|
|
pub struct ApplyNewService<T, F, In, Out, Request>
|
|
|
|
where
|
|
|
|
T: NewService<Request>,
|
|
|
|
{
|
2018-08-30 18:17:17 +02:00
|
|
|
service: T,
|
|
|
|
f: F,
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData<(In, Out, Request)>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> ApplyNewService<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-01-14 07:58:23 +01:00
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-08-31 02:54:59 +02:00
|
|
|
/// Create new `ApplyNewService` new service instance
|
2018-11-30 03:56:15 +01:00
|
|
|
pub fn new<F1: IntoNewService<T, Request>>(service: F1, f: F) -> 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-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> Clone for ApplyNewService<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request> + Clone,
|
2019-01-14 07:58:23 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out + Clone,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: 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-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> NewService<In> for ApplyNewService<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2019-01-14 07:58:23 +01:00
|
|
|
T: NewService<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out + Clone,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-01-14 07:58:23 +01:00
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-12-09 18:56:23 +01:00
|
|
|
type Response = Out::Item;
|
|
|
|
type Error = Out::Error;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Service = Apply<T::Service, F, In, Out, Request>;
|
2018-08-30 18:17:17 +02:00
|
|
|
|
|
|
|
type InitError = T::InitError;
|
2018-11-30 03:56:15 +01:00
|
|
|
type Future = ApplyNewServiceFuture<T, F, In, Out, Request>;
|
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-11-30 03:56:15 +01:00
|
|
|
pub struct ApplyNewServiceFuture<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
|
|
|
fut: T::Future,
|
|
|
|
f: Option<F>,
|
2018-11-30 03:56:15 +01:00
|
|
|
r: PhantomData<(In, Out)>,
|
2018-08-30 18:17:17 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> ApplyNewServiceFuture<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: 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-11-30 03:56:15 +01:00
|
|
|
impl<T, F, In, Out, Request> Future for ApplyNewServiceFuture<T, F, In, Out, Request>
|
2018-08-30 18:17:17 +02:00
|
|
|
where
|
2018-11-30 03:56:15 +01:00
|
|
|
T: NewService<Request>,
|
2018-12-06 23:53:55 +01:00
|
|
|
F: Fn(In, &mut T::Service) -> Out,
|
2018-11-30 03:56:15 +01:00
|
|
|
Out: IntoFuture,
|
2019-01-14 07:58:23 +01:00
|
|
|
Out::Error: From<T::Error>,
|
2018-08-30 18:17:17 +02:00
|
|
|
{
|
2018-11-30 03:56:15 +01:00
|
|
|
type Item = Apply<T::Service, F, In, Out, Request>;
|
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-09-18 00:53:41 +02:00
|
|
|
Ok(Async::Ready(Apply::new(service, self.f.take().unwrap())))
|
2018-08-30 18:17:17 +02:00
|
|
|
} else {
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use futures::future::{ok, FutureResult};
|
|
|
|
use futures::{Async, Future, Poll};
|
|
|
|
|
2018-12-13 03:56:39 +01:00
|
|
|
use crate::{IntoNewService, IntoService, NewService, Service, ServiceExt};
|
2018-09-12 22:34:53 +02:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
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_call() {
|
2018-09-18 01:50:35 +02:00
|
|
|
let blank = |req| Ok(req);
|
|
|
|
|
|
|
|
let mut srv = blank.into_service().apply(Srv, |req: &'static str, srv| {
|
|
|
|
srv.call(()).map(move |res| (req, res))
|
|
|
|
});
|
|
|
|
assert!(srv.poll_ready().is_ok());
|
2018-09-12 22:34:53 +02:00
|
|
|
let res = srv.call("srv").poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
|
|
}
|
2018-09-18 04:21:24 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_new_service() {
|
|
|
|
let blank = || Ok::<_, ()>((|req| Ok(req)).into_service());
|
|
|
|
|
|
|
|
let new_srv = blank.into_new_service().apply(
|
|
|
|
|| Ok(Srv),
|
|
|
|
|req: &'static str, srv| srv.call(()).map(move |res| (req, res)),
|
|
|
|
);
|
|
|
|
if let Async::Ready(mut srv) = new_srv.new_service().poll().unwrap() {
|
|
|
|
assert!(srv.poll_ready().is_ok());
|
|
|
|
let res = srv.call("srv").poll();
|
|
|
|
assert!(res.is_ok());
|
|
|
|
assert_eq!(res.unwrap(), Async::Ready(("srv", ())));
|
|
|
|
} else {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
2018-09-12 22:34:53 +02:00
|
|
|
}
|